Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace PhpParser\Builder;
|
Chris@0
|
4
|
Chris@0
|
5 use PhpParser;
|
Chris@0
|
6 use PhpParser\Node;
|
Chris@0
|
7
|
Chris@0
|
8 class Param extends PhpParser\BuilderAbstract
|
Chris@0
|
9 {
|
Chris@0
|
10 protected $name;
|
Chris@0
|
11
|
Chris@0
|
12 protected $default = null;
|
Chris@0
|
13
|
Chris@0
|
14 /** @var string|Node\Name|Node\NullableType|null */
|
Chris@0
|
15 protected $type = null;
|
Chris@0
|
16
|
Chris@0
|
17 protected $byRef = false;
|
Chris@0
|
18
|
Chris@0
|
19 protected $variadic = false;
|
Chris@0
|
20
|
Chris@0
|
21 /**
|
Chris@0
|
22 * Creates a parameter builder.
|
Chris@0
|
23 *
|
Chris@0
|
24 * @param string $name Name of the parameter
|
Chris@0
|
25 */
|
Chris@0
|
26 public function __construct($name) {
|
Chris@0
|
27 $this->name = $name;
|
Chris@0
|
28 }
|
Chris@0
|
29
|
Chris@0
|
30 /**
|
Chris@0
|
31 * Sets default value for the parameter.
|
Chris@0
|
32 *
|
Chris@0
|
33 * @param mixed $value Default value to use
|
Chris@0
|
34 *
|
Chris@0
|
35 * @return $this The builder instance (for fluid interface)
|
Chris@0
|
36 */
|
Chris@0
|
37 public function setDefault($value) {
|
Chris@0
|
38 $this->default = $this->normalizeValue($value);
|
Chris@0
|
39
|
Chris@0
|
40 return $this;
|
Chris@0
|
41 }
|
Chris@0
|
42
|
Chris@0
|
43 /**
|
Chris@0
|
44 * Sets type hint for the parameter.
|
Chris@0
|
45 *
|
Chris@0
|
46 * @param string|Node\Name|Node\NullableType $type Type hint to use
|
Chris@0
|
47 *
|
Chris@0
|
48 * @return $this The builder instance (for fluid interface)
|
Chris@0
|
49 */
|
Chris@0
|
50 public function setTypeHint($type) {
|
Chris@0
|
51 $this->type = $this->normalizeType($type);
|
Chris@0
|
52 if ($this->type === 'void') {
|
Chris@0
|
53 throw new \LogicException('Parameter type cannot be void');
|
Chris@0
|
54 }
|
Chris@0
|
55
|
Chris@0
|
56 return $this;
|
Chris@0
|
57 }
|
Chris@0
|
58
|
Chris@0
|
59 /**
|
Chris@0
|
60 * Make the parameter accept the value by reference.
|
Chris@0
|
61 *
|
Chris@0
|
62 * @return $this The builder instance (for fluid interface)
|
Chris@0
|
63 */
|
Chris@0
|
64 public function makeByRef() {
|
Chris@0
|
65 $this->byRef = true;
|
Chris@0
|
66
|
Chris@0
|
67 return $this;
|
Chris@0
|
68 }
|
Chris@0
|
69
|
Chris@0
|
70 /**
|
Chris@0
|
71 * Make the parameter variadic
|
Chris@0
|
72 *
|
Chris@0
|
73 * @return $this The builder instance (for fluid interface)
|
Chris@0
|
74 */
|
Chris@0
|
75 public function makeVariadic() {
|
Chris@0
|
76 $this->variadic = true;
|
Chris@0
|
77
|
Chris@0
|
78 return $this;
|
Chris@0
|
79 }
|
Chris@0
|
80
|
Chris@0
|
81 /**
|
Chris@0
|
82 * Returns the built parameter node.
|
Chris@0
|
83 *
|
Chris@0
|
84 * @return Node\Param The built parameter node
|
Chris@0
|
85 */
|
Chris@0
|
86 public function getNode() {
|
Chris@0
|
87 return new Node\Param(
|
Chris@0
|
88 $this->name, $this->default, $this->type, $this->byRef, $this->variadic
|
Chris@0
|
89 );
|
Chris@0
|
90 }
|
Chris@0
|
91 }
|