1: <?php
2: namespace Pharborist\Functions;
3:
4: use Pharborist\Filter;
5: use Pharborist\NodeCollection;
6: use Pharborist\CommaListNode;
7:
8: 9: 10:
11: trait ParameterTrait {
12: 13: 14:
15: protected $parameters;
16:
17: 18: 19:
20: public function getParameterList() {
21: return $this->parameters;
22: }
23:
24: 25: 26:
27: public function getParameters() {
28: $parameters = $this->parameters->getItems()->toArray();
29: return new ParameterNodeCollection($parameters, FALSE);
30: }
31:
32: 33: 34:
35: public function getParameterNames() {
36: return array_map(function(ParameterNode $parameter) {
37: return $parameter->getName();
38: }, $this->getParameters()->toArray());
39: }
40:
41: 42: 43: 44:
45: public function prependParameter(ParameterNode $parameter) {
46: $this->parameters->prependItem($parameter);
47: return $this;
48: }
49:
50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61:
62: public function appendParameter($parameter) {
63: if (is_callable($parameter)) {
64: $parameter = $parameter($this);
65: }
66: if (!($parameter instanceof ParameterNode)) {
67: throw new \InvalidArgumentException();
68: }
69: $this->parameters->appendItem($parameter);
70: return $this;
71: }
72:
73: 74: 75: 76: 77: 78: 79: 80: 81:
82: public function insertParameter(ParameterNode $parameter, $index) {
83: $this->parameters->insertItem($parameter, $index);
84: return $this;
85: }
86:
87: 88: 89: 90: 91:
92: public function clearParameters() {
93: $this->parameters->clear();
94: return $this;
95: }
96:
97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107:
108: public function getParameter($key) {
109: if (is_string($key)) {
110: return $this->getParameterByName($key);
111: }
112: elseif (is_integer($key)) {
113: return $this->getParameterAtIndex($key);
114: }
115: else {
116: throw new \InvalidArgumentException("Illegal parameter index {$key}.");
117: }
118: }
119:
120: 121: 122: 123: 124: 125: 126:
127: public function getParameterAtIndex($index) {
128: return $this->getParameterList()->getItem($index);
129: }
130:
131: 132: 133: 134: 135: 136: 137: 138: 139: 140:
141: public function getParameterByName($name) {
142: $name = ltrim($name, '$');
143:
144: foreach ($this->getParameters()->reverse() as $parameter) {
145: if ($parameter->getName() === $name) {
146: return $parameter;
147: }
148: }
149: throw new \UnexpectedValueException("Parameter {$name} does not exist.");
150: }
151:
152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164:
165: public function hasParameter($parameter, $type = NULL) {
166: if (is_string($parameter)) {
167: try {
168: $parameter = $this->getParameterByName($parameter);
169: }
170: catch (\UnexpectedValueException $e) {
171: return FALSE;
172: }
173: }
174: if (!($parameter instanceof ParameterNode)) {
175: throw new \InvalidArgumentException();
176: }
177: if ($parameter->parent() !== $this->parameters) {
178: return FALSE;
179: }
180: if ($type === NULL) {
181: return TRUE;
182: }
183: else {
184: return $parameter->getTypeHint()->getText() === $type;
185: }
186: }
187:
188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198:
199: public function hasRequiredParameter($parameter, $type = NULL) {
200: return $this->hasParameter($parameter, $type) && $this->getParameterByName($parameter)->isRequired();
201: }
202:
203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213:
214: public function hasOptionalParameter($parameter, $type = NULL) {
215: return $this->hasParameter($parameter, $type) && $this->getParameterByName($parameter)->isOptional();
216: }
217:
218: 219: 220:
221: public function hasRequiredParameters() {
222: return ($this->getRequiredParameters()->count() > 0);
223: }
224:
225: 226: 227:
228: public function getRequiredParameters() {
229: return $this->parameters
230: ->children(Filter::isInstanceOf('\Pharborist\ParameterNode'))
231: ->filter(function(ParameterNode $parameter) {
232: $value = $parameter->getValue();
233: return !isset($value);
234: });
235: }
236:
237: 238: 239:
240: public function getOptionalParameters() {
241: return $this->parameters
242: ->children(Filter::isInstanceOf('\Pharborist\ParameterNode'))
243: ->filter(function(ParameterNode $parameter) {
244: $value = $parameter->getValue();
245: return isset($value);
246: });
247: }
248:
249: 250: 251: 252: 253: 254:
255: public function isVariadic() {
256: $parameters = $this->getParameters();
257: $last_parameter = $parameters[count($parameters) - 1];
258:
259:
260: return $last_parameter->getVariadic() !== NULL;
261: }
262: }
263: