3 namespace Drupal\drupalmoduleupgrader\Routing;
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;
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.
14 class ParameterBinding {
17 * @var PathUtilityInterface
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
38 const NO_ARGUMENT = -1;
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;
49 * The original parameter node.
51 * @return \Pharborist\Functions\ParameterNode
53 public function getParameter() {
54 return $this->parameter;
58 * Returns if the parameter is explicitly represented in the path.
62 public function inPath() {
63 return ($this->isPathPosition() && sizeof($this->path) > $this->getArgument());
67 * Returns if this binding has an explicit argument.
71 public function hasArgument() {
72 return ($this->getArgument() !== self::NO_ARGUMENT);
76 * Returns the argument.
80 public function getArgument() {
81 return $this->argument;
85 * Whether or not the argument is a path position (integer greater
86 * than or equal to 0).
90 public function isPathPosition() {
91 return ($this->hasArgument() && is_integer($this->getArgument()));
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.
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('%');
110 return $this->getArgument();
114 $value = $this->getParameter()->getValue();
116 if ($value instanceof ScalarNode) {
117 return $value->toValue();