Drupal investigation

FieldMapEnhancer.php 1.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 can fill one field with the result of a hashmap lookup of
  14. * another field. If the target field is already set, it does nothing.
  15. *
  16. * @author David Buchmann
  17. */
  18. class FieldMapEnhancer implements RouteEnhancerInterface
  19. {
  20. /**
  21. * @var string field for key in hashmap lookup
  22. */
  23. protected $source;
  24. /**
  25. * @var string field to write hashmap lookup result into
  26. */
  27. protected $target;
  28. /**
  29. * @var array containing the mapping between the source field value and target field value
  30. */
  31. protected $hashmap;
  32. /**
  33. * @param string $source the field to read
  34. * @param string $target the field to write the result of the lookup into
  35. * @param array $hashmap for looking up value from source and get value for target
  36. */
  37. public function __construct($source, $target, array $hashmap)
  38. {
  39. $this->source = $source;
  40. $this->target = $target;
  41. $this->hashmap = $hashmap;
  42. }
  43. /**
  44. * If the target field is not set but the source field is, map the field.
  45. *
  46. * {@inheritdoc}
  47. */
  48. public function enhance(array $defaults, Request $request)
  49. {
  50. if (isset($defaults[$this->target])) {
  51. return $defaults;
  52. }
  53. if (!isset($defaults[$this->source])) {
  54. return $defaults;
  55. }
  56. if (!isset($this->hashmap[$defaults[$this->source]])) {
  57. return $defaults;
  58. }
  59. $defaults[$this->target] = $this->hashmap[$defaults[$this->source]];
  60. return $defaults;
  61. }
  62. }