AdapterChain.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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\CoreBundle\Model\Adapter;
  11. class AdapterChain implements AdapterInterface
  12. {
  13. protected $adapters = array();
  14. /**
  15. * @param AdapterInterface $adapter
  16. */
  17. public function addAdapter(AdapterInterface $adapter)
  18. {
  19. $this->adapters[] = $adapter;
  20. }
  21. /**
  22. * {@inheritdoc}
  23. */
  24. public function getNormalizedIdentifier($model)
  25. {
  26. foreach ($this->adapters as $adapter) {
  27. $identifier = $adapter->getNormalizedIdentifier($model);
  28. if ($identifier) {
  29. return $identifier;
  30. }
  31. }
  32. return;
  33. }
  34. /**
  35. * {@inheritdoc}
  36. */
  37. public function getUrlsafeIdentifier($model)
  38. {
  39. foreach ($this->adapters as $adapter) {
  40. $safeIdentifier = $adapter->getUrlsafeIdentifier($model);
  41. if ($safeIdentifier) {
  42. return $safeIdentifier;
  43. }
  44. }
  45. return;
  46. }
  47. }