UrlAnalyzer.php 1.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. namespace Muzich\CoreBundle\lib\Element;
  3. use Muzich\CoreBundle\Entity\Element;
  4. class UrlAnalyzer
  5. {
  6. protected $url;
  7. protected $match_rules;
  8. // TODO: Ce serait bien que le match_rules soit dans un Element fille
  9. public function __construct(Element $element, $match_rules = array())
  10. {
  11. $this->url = $element->getCleanedUrl();
  12. $this->match_rules = $match_rules;
  13. }
  14. protected function getMatchRules()
  15. {
  16. foreach ($this->match_rules as $type => $rules)
  17. {
  18. foreach ($rules as $expression => $ref_id_position)
  19. {
  20. if (preg_match($expression, $this->url, $preg_result))
  21. {
  22. if ($type == Element::TYPE_NOTMATCH)
  23. {
  24. return false;
  25. }
  26. if (array_key_exists($ref_id_position, $preg_result))
  27. {
  28. return array(
  29. Element::DATA_TYPE => $type,
  30. Element::DATA_REF_ID => $preg_result[$ref_id_position]
  31. );
  32. }
  33. elseif ($ref_id_position === null)
  34. {
  35. return array(
  36. Element::DATA_TYPE => $type,
  37. Element::DATA_REF_ID => null
  38. );
  39. }
  40. }
  41. }
  42. }
  43. return false;
  44. }
  45. public function haveMatch()
  46. {
  47. if ($this->getMatchRules() !== false)
  48. {
  49. return true;
  50. }
  51. return false;
  52. }
  53. public function getRefId()
  54. {
  55. if (($match_rules = $this->getMatchRules()))
  56. {
  57. return $match_rules[Element::DATA_REF_ID];
  58. }
  59. return null;
  60. }
  61. public function getType()
  62. {
  63. if (($match_rules = $this->getMatchRules()))
  64. {
  65. return $match_rules[Element::DATA_TYPE];
  66. }
  67. return null;
  68. }
  69. }