ChildrenVoter.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. /*
  3. * This file is part of the Sonata Project package.
  4. *
  5. * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Sonata\AdminBundle\Menu\Matcher\Voter;
  11. use Knp\Menu\ItemInterface;
  12. use Knp\Menu\Matcher\MatcherInterface;
  13. use Knp\Menu\Matcher\Voter\VoterInterface;
  14. /**
  15. * Children menu voter based on children items.
  16. *
  17. * @author Samusev Andrey <andrey.simfi@ya.ru>
  18. */
  19. class ChildrenVoter implements VoterInterface
  20. {
  21. /**
  22. * @var MatcherInterface
  23. */
  24. private $matcher;
  25. /**
  26. * ChildrenVoter constructor.
  27. *
  28. * @param MatcherInterface $matcher
  29. */
  30. public function __construct(MatcherInterface $matcher)
  31. {
  32. $this->matcher = $matcher;
  33. }
  34. /**
  35. * {@inheritdoc}
  36. */
  37. public function matchItem(ItemInterface $item)
  38. {
  39. if (!$item->getExtra('sonata_admin', false)) {
  40. return;
  41. }
  42. $children = $item->getChildren();
  43. $match = null;
  44. foreach ($children as $child) {
  45. if ($this->matcher->isCurrent($child)) {
  46. $match = true;
  47. break;
  48. }
  49. }
  50. return $match;
  51. }
  52. }