66
77use Closure ;
88
9+ /**
10+ * Defines a model property with its type, default value, and validation rules.
11+ *
12+ * @since 2.0.0
13+ */
914class ModelPropertyDefinition {
1015 /**
1116 * The default value of the property.
1217 *
18+ * @since 2.0.0
19+ *
1320 * @var mixed|Closure
1421 */
1522 private $ default ;
1623
1724 /**
1825 * The method to cast the property value.
1926 *
27+ * @since 2.0.0
28+ *
2029 * @var Closure|null A closure that accepts the value and property instance as parameters and returns the cast value.
2130 */
2231 private Closure $ castMethod ;
2332
2433 /**
2534 * Whether the definition is locked. Once locked, the definition cannot be changed.
35+ *
36+ * @since 2.0.0
37+ *
38+ * @var bool
2639 */
2740 private bool $ locked = false ;
2841
2942 /**
3043 * Whether the property is nullable.
44+ *
45+ * @since 2.0.0
46+ *
47+ * @var bool
3148 */
3249 private bool $ nullable = false ;
3350
3451 /**
3552 * Whether the property is required.
53+ *
54+ * @since 2.0.0
55+ *
56+ * @var bool
3657 */
3758 private bool $ required = false ;
3859
3960 /**
4061 * Whether the property is required on save.
62+ *
63+ * @since 2.0.0
64+ *
65+ * @var bool
4166 */
4267 private bool $ requiredOnSave = false ;
4368
4469 /**
4570 * The type of the property.
4671 *
72+ * @since 2.0.0
73+ *
4774 * @var string[]
4875 */
4976 private array $ type = ['string ' ];
5077
5178 /**
5279 * Set the default value of the property.
5380 *
81+ * @since 2.0.0
82+ *
5483 * @param mixed|Closure $default The default value of the property.
5584 */
5685 public function default ( $ default ): self {
@@ -63,6 +92,8 @@ public function default( $default ): self {
6392
6493 /**
6594 * Whether the property can cast the value.
95+ *
96+ * @since 2.0.0
6697 */
6798 public function canCast (): bool {
6899 return isset ( $ this ->castMethod );
@@ -71,7 +102,13 @@ public function canCast(): bool {
71102 /**
72103 * Cast the property value.
73104 *
105+ * @since 2.0.0
106+ *
74107 * @param mixed $value The value to cast.
108+ *
109+ * @return mixed
110+ *
111+ * @throws \RuntimeException When no cast method is set.
75112 */
76113 public function cast ( $ value ) {
77114 $ this ->checkLock ();
@@ -87,6 +124,8 @@ public function cast( $value ) {
87124
88125 /**
89126 * Provides a method to cast the property value.
127+ *
128+ * @since 2.0.0
90129 */
91130 public function castWith ( callable $ castMethod ): self {
92131 $ this ->checkLock ();
@@ -98,6 +137,10 @@ public function castWith( callable $castMethod ): self {
98137
99138 /**
100139 * Check if the property is locked and throw an exception if it is.
140+ *
141+ * @since 2.0.0
142+ *
143+ * @throws \RuntimeException When the property is locked.
101144 */
102145 private function checkLock (): void {
103146 if ( $ this ->locked ) {
@@ -107,7 +150,12 @@ private function checkLock(): void {
107150
108151 /**
109152 * Create a property definition from a shorthand string or array.
110- * @param string|array{0:string,1:bool} $definition
153+ *
154+ * @since 2.0.0
155+ *
156+ * @param string|array{0:string,1:mixed} $definition The shorthand definition.
157+ *
158+ * @throws \InvalidArgumentException When the definition is invalid.
111159 */
112160 public static function fromShorthand ( $ definition ): self {
113161 $ property = new self ();
@@ -130,6 +178,8 @@ public static function fromShorthand( $definition ): self {
130178 /**
131179 * Get the default value of the property.
132180 *
181+ * @since 2.0.0
182+ *
133183 * @return mixed
134184 */
135185 public function getDefault () {
@@ -145,6 +195,8 @@ public function getDefault() {
145195 /**
146196 * Get the type of the property.
147197 *
198+ * @since 2.0.0
199+ *
148200 * @return string[]
149201 */
150202 public function getType (): array {
@@ -153,6 +205,8 @@ public function getType(): array {
153205
154206 /**
155207 * Whether the property has a default value.
208+ *
209+ * @since 2.0.0
156210 */
157211 public function hasDefault (): bool {
158212 return isset ( $ this ->default );
@@ -161,35 +215,45 @@ public function hasDefault(): bool {
161215 /**
162216 * Whether the property is locked.
163217 *
164- * @return bool
218+ * @since 2.0.0
165219 */
166220 public function isLocked (): bool {
167221 return $ this ->locked ;
168222 }
169223
170224 /**
171225 * Whether the property is nullable.
226+ *
227+ * @since 2.0.0
172228 */
173229 public function isNullable (): bool {
174230 return $ this ->nullable ;
175231 }
176232
177233 /**
178234 * Whether the property is required.
235+ *
236+ * @since 2.0.0
179237 */
180238 public function isRequired (): bool {
181239 return $ this ->required ;
182240 }
183241
184242 /**
185243 * Whether the property is required on save.
244+ *
245+ * @since 2.0.0
186246 */
187247 public function isRequiredOnSave (): bool {
188248 return $ this ->requiredOnSave ;
189249 }
190250
191251 /**
192252 * Whether the property is valid for the given value.
253+ *
254+ * @since 2.0.0
255+ *
256+ * @param mixed $value The value to validate.
193257 */
194258 public function isValidValue ( $ value ): bool {
195259 $ valueType = gettype ( $ value );
@@ -222,13 +286,20 @@ public function isValidValue( $value ): bool {
222286 /**
223287 * Locks the property so it cannot be changed.
224288 * Note that once locked the property cannot be unlocked.
289+ *
290+ * @since 2.0.0
225291 */
226292 public function lock (): self {
227293 $ this ->locked = true ;
228294
229295 return $ this ;
230296 }
231297
298+ /**
299+ * Makes the property nullable.
300+ *
301+ * @since 2.0.0
302+ */
232303 public function nullable (): self {
233304 $ this ->checkLock ();
234305
@@ -239,6 +310,8 @@ public function nullable(): self {
239310
240311 /**
241312 * Makes the property required.
313+ *
314+ * @since 2.0.0
242315 */
243316 public function required (): self {
244317 $ this ->checkLock ();
@@ -250,6 +323,8 @@ public function required(): self {
250323
251324 /**
252325 * Makes the property required on save.
326+ *
327+ * @since 2.0.0
253328 */
254329 public function requiredOnSave (): self {
255330 $ this ->checkLock ();
@@ -261,6 +336,8 @@ public function requiredOnSave(): self {
261336
262337 /**
263338 * Whether the property supports the given type.
339+ *
340+ * @since 2.0.0
264341 */
265342 public function supportsType ( string $ type ): bool {
266343 return in_array ( $ type , $ this ->type );
@@ -269,7 +346,9 @@ public function supportsType( string $type ): bool {
269346 /**
270347 * Set the type of the property.
271348 *
272- * @param string[] $types The types of the property, multiple types are considered a union type.
349+ * @since 2.0.0
350+ *
351+ * @param string ...$types The types of the property, multiple types are considered a union type.
273352 */
274353 public function type ( string ...$ types ): self {
275354 $ this ->checkLock ();
0 commit comments