ChildrenVoter.php 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. $children = $item->getChildren();
  40. $match = null;
  41. foreach ($children as $child) {
  42. if ($this->matcher->isCurrent($child)) {
  43. $match = true;
  44. break;
  45. }
  46. }
  47. return $match;
  48. }
  49. }