11<?php
2+ /**
3+ * The model.
4+ *
5+ * @since 1.0.0
6+ *
7+ * @package StellarWP\Models;
8+ */
29
310namespace StellarWP \Models ;
411
512use JsonSerializable ;
6- use RuntimeException ;
713use StellarWP \Models \Contracts \Arrayable ;
814use StellarWP \Models \Contracts \Model as ModelInterface ;
915use StellarWP \Models \ValueObjects \Relationship ;
10-
16+ use InvalidArgumentException ;
17+
18+ /**
19+ * The model.
20+ *
21+ * @since 1.0.0
22+ *
23+ * @package StellarWP\Models;
24+ */
1125abstract class Model implements ModelInterface, Arrayable, JsonSerializable {
26+ /**
27+ * The build mode for the model.
28+ *
29+ * @var int
30+ */
1231 public const BUILD_MODE_STRICT = 0 ;
32+
33+ /**
34+ * The build mode for the model.
35+ *
36+ * @var int
37+ */
1338 public const BUILD_MODE_IGNORE_MISSING = 1 ;
39+
40+ /**
41+ * The build mode for the model.
42+ *
43+ * @var int
44+ */
1445 public const BUILD_MODE_IGNORE_EXTRA = 2 ;
1546
1647 /**
@@ -25,21 +56,21 @@ abstract class Model implements ModelInterface, Arrayable, JsonSerializable {
2556 *
2657 * @var array<string,string|array>
2758 */
28- protected static $ properties = [];
59+ protected static array $ properties = [];
2960
3061 /**
3162 * The model relationships assigned to their relationship types.
3263 *
3364 * @var array<string,string>
3465 */
35- protected static $ relationships = [];
66+ protected static array $ relationships = [];
3667
3768 /**
3869 * Relationships that have already been loaded and don't need to be loaded again.
3970 *
4071 * @var Model[]
4172 */
42- private $ cachedRelations = [];
73+ private array $ cachedRelations = [];
4374
4475 /**
4576 * Constructor.
@@ -63,6 +94,8 @@ public function __construct( array $attributes = [] ) {
6394 * @param string $property The property being casted.
6495 *
6596 * @return mixed
97+ *
98+ * @throws InvalidArgumentException If the value is not valid for the property.
6699 */
67100 protected static function castValueForProperty ( ModelPropertyDefinition $ definition , $ value , string $ property ) {
68101 if ( $ definition ->isValidValue ( $ value ) || $ value === null ) {
@@ -75,7 +108,7 @@ protected static function castValueForProperty( ModelPropertyDefinition $definit
75108
76109 $ type = $ definition ->getType ();
77110 if ( count ( $ type ) !== 1 ) {
78- throw new \ InvalidArgumentException ( "Property ' $ property' has multiple types: " . implode ( ', ' , $ type ) . ". To support additional types, implement a custom castValueForProperty() method. " );
111+ throw new InvalidArgumentException ( "Property ' $ property' has multiple types: " . implode ( ', ' , $ type ) . ". To support additional types, implement a custom castValueForProperty() method. " );
79112 }
80113
81114 switch ( $ type [0 ] ) {
@@ -169,6 +202,8 @@ public function getAttribute( string $key, $default = null ) {
169202 * @since 1.0.0
170203 *
171204 * @return array<string,mixed>
205+ *
206+ * @throws InvalidArgumentException If the property does not exist.
172207 */
173208 public function getDirty () : array {
174209 return $ this ->propertyCollection ->getDirtyValues ();
@@ -178,7 +213,7 @@ public static function getPropertyDefinition( string $key ): ModelPropertyDefini
178213 $ definitions = static ::getPropertyDefinitions ();
179214
180215 if ( ! isset ( $ definitions [ $ key ] ) ) {
181- throw new \ InvalidArgumentException ( 'Property ' . $ key . ' does not exist. ' );
216+ throw new InvalidArgumentException ( 'Property ' . $ key . ' does not exist. ' );
182217 }
183218
184219 return $ definitions [ $ key ];
@@ -190,6 +225,8 @@ public static function getPropertyDefinition( string $key ): ModelPropertyDefini
190225 * @since 2.0.0
191226 *
192227 * @return array<string,ModelPropertyDefinition>
228+ *
229+ * @throws InvalidArgumentException If the property key is not a string.
193230 */
194231 public static function getPropertyDefinitions (): array {
195232 static $ definition = null ;
@@ -199,7 +236,7 @@ public static function getPropertyDefinitions(): array {
199236
200237 foreach ( $ definitions as $ key => $ definition ) {
201238 if ( ! is_string ( $ key ) ) {
202- throw new \ InvalidArgumentException ( 'Property key must be a string. ' );
239+ throw new InvalidArgumentException ( 'Property key must be a string. ' );
203240 }
204241
205242 if ( ! $ definition instanceof ModelPropertyDefinition ) {
@@ -224,7 +261,7 @@ public static function getPropertyDefinitions(): array {
224261 *
225262 * @return mixed|array
226263 */
227- public function getOriginal ( string $ key = null ) {
264+ public function getOriginal ( ? string $ key = null ) {
228265 return $ key ? $ this ->propertyCollection ->getOrFail ( $ key )->getOriginalValue () : $ this ->propertyCollection ->getOriginalValues ();
229266 }
230267
@@ -368,7 +405,7 @@ public static function isPropertyTypeValid( string $key, $value ) : bool {
368405 */
369406 #[\ReturnTypeWillChange]
370407 public function jsonSerialize () {
371- return get_object_vars ( $ this );
408+ return $ this -> toArray ( );
372409 }
373410
374411 /**
0 commit comments