Drupal investigation

FieldByClassEnhancer.php 2.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. /*
  3. * This file is part of the Symfony CMF package.
  4. *
  5. * (c) 2011-2015 Symfony CMF
  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 Symfony\Cmf\Component\Routing\Enhancer;
  11. use Symfony\Component\HttpFoundation\Request;
  12. /**
  13. * This enhancer sets a field if not yet existing from the class of an object
  14. * in another field.
  15. *
  16. * The comparison is done with instanceof to support proxy classes and such.
  17. *
  18. * Only works with RouteObjectInterface routes that can return a referenced
  19. * content.
  20. *
  21. * @author David Buchmann
  22. */
  23. class FieldByClassEnhancer implements RouteEnhancerInterface
  24. {
  25. /**
  26. * @var string field for the source class
  27. */
  28. protected $source;
  29. /**
  30. * @var string field to write hashmap lookup result into
  31. */
  32. protected $target;
  33. /**
  34. * @var array containing the mapping between a class name and the target value
  35. */
  36. protected $map;
  37. /**
  38. * @param string $source the field name of the class
  39. * @param string $target the field name to set from the map
  40. * @param array $map the map of class names to field values
  41. */
  42. public function __construct($source, $target, $map)
  43. {
  44. $this->source = $source;
  45. $this->target = $target;
  46. $this->map = $map;
  47. }
  48. /**
  49. * If the source field is instance of one of the entries in the map,
  50. * target is set to the value of that map entry.
  51. *
  52. * {@inheritdoc}
  53. */
  54. public function enhance(array $defaults, Request $request)
  55. {
  56. if (isset($defaults[$this->target])) {
  57. // no need to do anything
  58. return $defaults;
  59. }
  60. if (!isset($defaults[$this->source])) {
  61. return $defaults;
  62. }
  63. // we need to loop over the array and do instanceof in case the content
  64. // class extends the specified class
  65. // i.e. phpcr-odm generates proxy class for the content.
  66. foreach ($this->map as $class => $value) {
  67. if ($defaults[$this->source] instanceof $class) {
  68. // found a matching entry in the map
  69. $defaults[$this->target] = $value;
  70. return $defaults;
  71. }
  72. }
  73. return $defaults;
  74. }
  75. }