1 | <?php |
||
24 | final class BindingParameter |
||
25 | { |
||
26 | /** |
||
27 | * Flag: The parameter is optional. |
||
28 | */ |
||
29 | const OPTIONAL = 0; |
||
30 | |||
31 | /** |
||
32 | * Flag: The parameter is required. |
||
33 | */ |
||
34 | const REQUIRED = 1; |
||
35 | |||
36 | /** |
||
37 | * @var string |
||
38 | */ |
||
39 | private $name; |
||
40 | |||
41 | /** |
||
42 | * @var int |
||
43 | */ |
||
44 | private $flags; |
||
45 | |||
46 | /** |
||
47 | * @var mixed |
||
48 | */ |
||
49 | private $defaultValue; |
||
50 | |||
51 | /** |
||
52 | * Creates a new parameter. |
||
53 | * |
||
54 | * @param string $name The parameter name. |
||
55 | * @param int $flags A bitwise combination of the flag constants |
||
56 | * in this class. |
||
57 | * @param mixed $defaultValue The parameter's default value. |
||
58 | */ |
||
59 | 57 | public function __construct($name, $flags = self::OPTIONAL, $defaultValue = null) |
|
60 | { |
||
61 | 57 | Assert::stringNotEmpty($name, 'The parameter name must be a non-empty string. Got: %s'); |
|
62 | 55 | Assert::startsWithLetter($name, 'The parameter name must start with a letter. Got: %s'); |
|
63 | 53 | Assert::nullOrInteger($flags, 'The parameter "$flags" must be an integer or null. Got: %s'); |
|
64 | |||
65 | 53 | if (($flags & self::REQUIRED) && null !== $defaultValue) { |
|
66 | 1 | throw new RuntimeException('Required parameters must not have default values.'); |
|
67 | } |
||
68 | |||
69 | 52 | $this->name = $name; |
|
70 | 52 | $this->flags = $flags; |
|
71 | 52 | $this->defaultValue = $defaultValue; |
|
72 | 52 | } |
|
73 | |||
74 | /** |
||
75 | * Returns the name of the parameter. |
||
76 | * |
||
77 | * @return string The parameter name. |
||
78 | */ |
||
79 | 49 | public function getName() |
|
83 | |||
84 | /** |
||
85 | * Returns the flags passed to the constructor. |
||
86 | * |
||
87 | * @return int A bitwise combination of the flag constants in this class. |
||
88 | */ |
||
89 | 19 | public function getFlags() |
|
90 | { |
||
91 | 19 | return $this->flags; |
|
92 | } |
||
93 | |||
94 | /** |
||
95 | * Returns the default value of the parameter. |
||
96 | * |
||
97 | * @return mixed The default value. |
||
98 | */ |
||
99 | 32 | public function getDefaultValue() |
|
103 | |||
104 | /** |
||
105 | * Returns whether the parameter is required. |
||
106 | * |
||
107 | * @return bool Returns `true` if the parameter is required and `false` |
||
108 | * otherwise. |
||
109 | */ |
||
110 | 39 | public function isRequired() |
|
114 | } |
||
115 |