Further modules included.
[yaffs-website] / web / modules / contrib / drupalmoduleupgrader / src / Routing / ParameterBinding.php
1 <?php
2
3 namespace Drupal\drupalmoduleupgrader\Routing;
4
5 use Drupal\drupalmoduleupgrader\Utility\Path\Drupal7\PathComponent;
6 use Drupal\drupalmoduleupgrader\Utility\Path\PathUtilityInterface;
7 use Pharborist\Functions\ParameterNode;
8 use Pharborist\Types\ScalarNode;
9
10 /**
11  * Represents a binding between a single callback parameter and a single
12  * path component in a Drupal 8 route path, possibly affected by an argument.
13  */
14 class ParameterBinding {
15
16   /**
17    * @var PathUtilityInterface
18    */
19   protected $path;
20
21   /**
22    * @var ParameterNode
23    */
24   protected $parameter;
25
26   /**
27    * @var mixed
28    */
29   protected $argument;
30
31   /**
32    * The trouble with Drupal 7 callback arguments is that virtually any value
33    * could be explicitly passed, including NULL and FALSE. -1 is an illegal
34    * value because it's an integer, but not a valid path position. So we'll
35    * use it here as a signal that no argument is explicitly bound to the
36    * parameter.
37    */
38   const NO_ARGUMENT = -1;
39
40   public function __construct(PathUtilityInterface $path, ParameterNode $parameter, $argument = self::NO_ARGUMENT) {
41     // Clone $path so that we have our own copy to look at. The original $path
42     // is (probably) modified by upstream code.
43     $this->path = clone $path;
44     $this->parameter = $parameter;
45     $this->argument = $argument;
46   }
47
48   /**
49    * The original parameter node.
50    *
51    * @return \Pharborist\Functions\ParameterNode
52    */
53   public function getParameter() {
54     return $this->parameter;
55   }
56
57   /**
58    * Returns if the parameter is explicitly represented in the path.
59    *
60    * @return boolean
61    */
62   public function inPath() {
63     return ($this->isPathPosition() && sizeof($this->path) > $this->getArgument());
64   }
65
66   /**
67    * Returns if this binding has an explicit argument.
68    *
69    * @return boolean
70    */
71   public function hasArgument() {
72     return ($this->getArgument() !== self::NO_ARGUMENT);
73   }
74
75   /**
76    * Returns the argument.
77    *
78    * @return mixed
79    */
80   public function getArgument() {
81     return $this->argument;
82   }
83
84   /**
85    * Whether or not the argument is a path position (integer greater
86    * than or equal to 0).
87    *
88    * @return boolean
89    */
90   public function isPathPosition() {
91     return ($this->hasArgument() && is_integer($this->getArgument()));
92   }
93
94   /**
95    * Returns the value of the binding. If the value is an instance of
96    * \Drupal\drupalmoduleupgrader\Utility\Path\PathComponentInterface,
97    * the binding expects to be physically represented in the path, although
98    * it may not yet be (this can be ascertained by the inPath() method). Any
99    * other value is used verbatim.
100    *
101    * @return mixed
102    */
103   public function getValue() {
104     if ($this->hasArgument()) {
105       if ($this->isPathPosition()) {
106         $position = $this->getArgument();
107         return $this->path->containsKey($position) ? $this->path[$position] : new PathComponent('%');
108       }
109       else {
110         return $this->getArgument();
111       }
112     }
113     else {
114       $value = $this->getParameter()->getValue();
115
116       if ($value instanceof ScalarNode) {
117         return $value->toValue();
118       }
119     }
120   }
121
122 }