UrlAnalyzer.php 1.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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 (array_key_exists($ref_id_position, $preg_result))
  23. {
  24. return array(
  25. Element::DATA_TYPE => $type,
  26. Element::DATA_REF_ID => $preg_result[$ref_id_position]
  27. );
  28. }
  29. }
  30. }
  31. }
  32. return false;
  33. }
  34. public function haveMatch()
  35. {
  36. if ($this->getMatchRules() !== false)
  37. {
  38. return true;
  39. }
  40. return false;
  41. }
  42. public function getRefId()
  43. {
  44. if (($match_rules = $this->getMatchRules()))
  45. {
  46. return $match_rules[Element::DATA_REF_ID];
  47. }
  48. return null;
  49. }
  50. public function getType()
  51. {
  52. if (($match_rules = $this->getMatchRules()))
  53. {
  54. return $match_rules[Element::DATA_TYPE];
  55. }
  56. return null;
  57. }
  58. }