|
7 | 7 |
|
8 | 8 | class DeserializeException extends Exception |
9 | 9 | { |
10 | | - public const int GENERIC_ERROR_CODE = 0; |
11 | | - public const int INVALID_VALUE_ERROR_CODE = 1; |
12 | | - public const int NO_SERIALIZERS_ERROR_CODE = 2; |
13 | | - |
14 | | - public function __construct( |
15 | | - string $message = 'Failed to deserialize data', |
16 | | - int $code = self::GENERIC_ERROR_CODE, |
17 | | - ?Throwable $previous = null |
18 | | - ) { |
19 | | - parent::__construct($message, $code, $previous); |
| 10 | + protected const int GENERIC_ERROR_CODE = 0; |
| 11 | + protected const int INVALID_VALUE_ERROR_CODE = 1; |
| 12 | + protected const int PROPERTY_IS_NOT_NULLABLE_ERROR_CODE = 2; |
| 13 | + protected const int UNABLE_TO_DESERIALIZE_SCALAR_TYPE_ITEM_ERROR_CODE = 3; |
| 14 | + protected const int UNABLE_TO_DESERIALIZE_BACKED_ENUM_ITEM_ERROR_CODE = 4; |
| 15 | + protected const int UNABLE_TO_DESERIALIZE_DATE_TIME_ITEM_ERROR_CODE = 5; |
| 16 | + protected const int UNABLE_TO_DESERIALIZE_DATA_ITEM_ERROR_CODE = 6; |
| 17 | + protected const int UNABLE_TO_DESERIALIZE_ARRAY_ITEM_ERROR_CODE = 7; |
| 18 | + protected const int INVALID_PARAMS_PASSED_ERROR_CODE = 8; |
| 19 | + |
| 20 | + public static function generic(?Throwable $throwable = null): self |
| 21 | + { |
| 22 | + return new self( |
| 23 | + message: 'An error occurred while deserializing data', |
| 24 | + code: self::GENERIC_ERROR_CODE, |
| 25 | + previous: $throwable |
| 26 | + ); |
| 27 | + } |
| 28 | + |
| 29 | + public static function invalidValue(): self |
| 30 | + { |
| 31 | + return new self( |
| 32 | + message: 'Invalid value passed to from method', |
| 33 | + code: self::INVALID_VALUE_ERROR_CODE |
| 34 | + ); |
| 35 | + } |
| 36 | + |
| 37 | + public static function propertyIsNotNullable(): self |
| 38 | + { |
| 39 | + return new self( |
| 40 | + message: 'Property is not nullable', |
| 41 | + code: self::PROPERTY_IS_NOT_NULLABLE_ERROR_CODE |
| 42 | + ); |
| 43 | + } |
| 44 | + |
| 45 | + public static function unableToDeserializeScalarTypeItem(): self |
| 46 | + { |
| 47 | + return new self( |
| 48 | + message: 'Could not deserialize scalar type item', |
| 49 | + code: self::UNABLE_TO_DESERIALIZE_SCALAR_TYPE_ITEM_ERROR_CODE |
| 50 | + ); |
| 51 | + } |
| 52 | + |
| 53 | + public static function unableToDeserializeBackedEnumItem(): self |
| 54 | + { |
| 55 | + return new self( |
| 56 | + message: 'Could not deserialize BackedEnum item', |
| 57 | + code: self::UNABLE_TO_DESERIALIZE_BACKED_ENUM_ITEM_ERROR_CODE |
| 58 | + ); |
| 59 | + } |
| 60 | + |
| 61 | + public static function unableToDeserializeDateTimeItem(): self |
| 62 | + { |
| 63 | + return new self( |
| 64 | + message: 'Could not deserialize DateTime item', |
| 65 | + code: self::UNABLE_TO_DESERIALIZE_DATE_TIME_ITEM_ERROR_CODE |
| 66 | + ); |
| 67 | + } |
| 68 | + |
| 69 | + public static function unableToDeserializeDataItem(): self |
| 70 | + { |
| 71 | + return new self( |
| 72 | + message: 'Could not deserialize Data item', |
| 73 | + code: self::UNABLE_TO_DESERIALIZE_DATA_ITEM_ERROR_CODE |
| 74 | + ); |
| 75 | + } |
| 76 | + |
| 77 | + public static function unableToDeserializeArrayItem(): self |
| 78 | + { |
| 79 | + return new self( |
| 80 | + message: 'Could not deserialize array item', |
| 81 | + code: self::UNABLE_TO_DESERIALIZE_ARRAY_ITEM_ERROR_CODE |
| 82 | + ); |
| 83 | + } |
| 84 | + |
| 85 | + public static function invalidParamsPassed(): self |
| 86 | + { |
| 87 | + return new self( |
| 88 | + message: 'Invalid params passed', |
| 89 | + code: self::INVALID_PARAMS_PASSED_ERROR_CODE |
| 90 | + ); |
20 | 91 | } |
21 | 92 | } |
0 commit comments