@@ -11,161 +11,161 @@ |
||
| 11 | 11 | class Hydrator |
| 12 | 12 | { |
| 13 | 13 | |
| 14 | - /** |
|
| 15 | - * The model to hydrate |
|
| 16 | - * |
|
| 17 | - * @var class-string<T> |
|
| 18 | - */ |
|
| 19 | - protected $model; |
|
| 20 | - |
|
| 21 | - /** |
|
| 22 | - * The arguments used to create the new instance. |
|
| 23 | - * |
|
| 24 | - * @var array<string, mixed> |
|
| 25 | - */ |
|
| 26 | - protected $constructorArgs; |
|
| 27 | - |
|
| 28 | - public function __construct(string $model = \stdClass::class, array $constructorArgs = []) |
|
| 29 | - { |
|
| 30 | - $this->model = $model; |
|
| 31 | - $this->constructorArgs = $constructorArgs; |
|
| 32 | - } |
|
| 33 | - |
|
| 34 | - /** |
|
| 35 | - * Map many models |
|
| 36 | - * |
|
| 37 | - * @param array<int, object|mixed[]> $sources |
|
| 38 | - * @return T[] |
|
| 39 | - */ |
|
| 40 | - public function fromMany(array $sources): array |
|
| 41 | - { |
|
| 42 | - return array_map([$this, 'from'], $sources); |
|
| 43 | - } |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * Map a single model |
|
| 47 | - * |
|
| 48 | - * @param object|mixed[] $source |
|
| 49 | - * @return T |
|
| 50 | - */ |
|
| 51 | - public function from($source) |
|
| 52 | - { |
|
| 53 | - switch (true) { |
|
| 54 | - case is_array($source): |
|
| 55 | - return $this->fromArray($source); |
|
| 56 | - |
|
| 57 | - case \is_object($source): |
|
| 58 | - return $this->fromObject($source); |
|
| 59 | - |
|
| 60 | - default: |
|
| 61 | - throw new \Exception("Models can only be mapped from arrays or objects.", 1); |
|
| 62 | - } |
|
| 63 | - } |
|
| 64 | - |
|
| 65 | - /** |
|
| 66 | - * Maps the model from an array of data. |
|
| 67 | - * |
|
| 68 | - * @param array $source |
|
| 69 | - * @return T |
|
| 70 | - */ |
|
| 71 | - protected function fromArray(array $source) |
|
| 72 | - { |
|
| 73 | - $model = $this->newInstance(); |
|
| 74 | - foreach ($source as $key => $value) { |
|
| 75 | - $this->setProperty($model, $key, $value); |
|
| 76 | - } |
|
| 77 | - return $model; |
|
| 78 | - } |
|
| 79 | - |
|
| 80 | - /** |
|
| 81 | - * Maps a model from an Object of data |
|
| 82 | - * |
|
| 83 | - * @param object $source |
|
| 84 | - * @return T |
|
| 85 | - */ |
|
| 86 | - protected function fromObject($source) |
|
| 87 | - { |
|
| 88 | - $vars = get_object_vars($source); |
|
| 89 | - return $this->fromArray($vars); |
|
| 90 | - } |
|
| 91 | - |
|
| 92 | - /** |
|
| 93 | - * Construct an instance of the model |
|
| 94 | - * |
|
| 95 | - * @return void |
|
| 96 | - */ |
|
| 97 | - protected function newInstance() |
|
| 98 | - { |
|
| 99 | - $class = $this->model; |
|
| 100 | - try { |
|
| 101 | - $instance = empty($this->constructorArgs) |
|
| 102 | - ? new $class() |
|
| 103 | - : new $class(...$this->constructorArgs); |
|
| 104 | - } catch (\Throwable $th) { |
|
| 105 | - throw new \Exception("Failed to construct model, {$th->getMessage()}", 1); |
|
| 106 | - } |
|
| 107 | - |
|
| 108 | - return $instance; |
|
| 109 | - } |
|
| 110 | - |
|
| 111 | - /** |
|
| 112 | - * Sets a property to the current model |
|
| 113 | - * |
|
| 114 | - * @param T $model |
|
| 115 | - * @param string $property |
|
| 116 | - * @param mixed $value |
|
| 117 | - * @return T |
|
| 118 | - */ |
|
| 119 | - protected function setProperty($model, string $property, $value) |
|
| 120 | - { |
|
| 121 | - $property = $this->normaliseProperty($property); |
|
| 122 | - |
|
| 123 | - // Attempt to set. |
|
| 124 | - try { |
|
| 125 | - switch (true) { |
|
| 126 | - case \method_exists($model, $this->generateSetterMethod($property)): |
|
| 127 | - $method = $this->generateSetterMethod($property); |
|
| 128 | - $model->$method($value); |
|
| 129 | - break; |
|
| 130 | - |
|
| 131 | - case \method_exists($model, $this->generateSetterMethod($property, true)): |
|
| 132 | - $method = $this->generateSetterMethod($property, true); |
|
| 133 | - $model->$method($value); |
|
| 134 | - break; |
|
| 135 | - |
|
| 136 | - default: |
|
| 137 | - $model->$property = $value; |
|
| 138 | - break; |
|
| 139 | - } |
|
| 140 | - } catch (\Throwable $th) { |
|
| 141 | - throw new \Exception(sprintf("Failed to set %s of %s model, %s", $property, get_class($model), $th->getMessage()), 1); |
|
| 142 | - } |
|
| 143 | - |
|
| 144 | - return $model; |
|
| 145 | - } |
|
| 146 | - |
|
| 147 | - /** |
|
| 148 | - * Normalises a property |
|
| 149 | - * |
|
| 150 | - * @param string $property |
|
| 151 | - * @return string |
|
| 152 | - */ |
|
| 153 | - protected function normaliseProperty(string $property): string |
|
| 154 | - { |
|
| 155 | - return \trim(preg_replace('/[^a-z0-9]+/', '_', strtolower($property))); |
|
| 156 | - } |
|
| 157 | - |
|
| 158 | - /** |
|
| 159 | - * Generates a generic setter method using either underscore [set_property()] or PSR2 style [setProperty()] |
|
| 160 | - * |
|
| 161 | - * @param string $property |
|
| 162 | - * @param bool $underscore |
|
| 163 | - * @return string |
|
| 164 | - */ |
|
| 165 | - protected function generateSetterMethod(string $property, bool $underscore = false): string |
|
| 166 | - { |
|
| 167 | - return $underscore |
|
| 168 | - ? "set_{$property}" |
|
| 169 | - : "set" . \ucfirst($property); |
|
| 170 | - } |
|
| 14 | + /** |
|
| 15 | + * The model to hydrate |
|
| 16 | + * |
|
| 17 | + * @var class-string<T> |
|
| 18 | + */ |
|
| 19 | + protected $model; |
|
| 20 | + |
|
| 21 | + /** |
|
| 22 | + * The arguments used to create the new instance. |
|
| 23 | + * |
|
| 24 | + * @var array<string, mixed> |
|
| 25 | + */ |
|
| 26 | + protected $constructorArgs; |
|
| 27 | + |
|
| 28 | + public function __construct(string $model = \stdClass::class, array $constructorArgs = []) |
|
| 29 | + { |
|
| 30 | + $this->model = $model; |
|
| 31 | + $this->constructorArgs = $constructorArgs; |
|
| 32 | + } |
|
| 33 | + |
|
| 34 | + /** |
|
| 35 | + * Map many models |
|
| 36 | + * |
|
| 37 | + * @param array<int, object|mixed[]> $sources |
|
| 38 | + * @return T[] |
|
| 39 | + */ |
|
| 40 | + public function fromMany(array $sources): array |
|
| 41 | + { |
|
| 42 | + return array_map([$this, 'from'], $sources); |
|
| 43 | + } |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * Map a single model |
|
| 47 | + * |
|
| 48 | + * @param object|mixed[] $source |
|
| 49 | + * @return T |
|
| 50 | + */ |
|
| 51 | + public function from($source) |
|
| 52 | + { |
|
| 53 | + switch (true) { |
|
| 54 | + case is_array($source): |
|
| 55 | + return $this->fromArray($source); |
|
| 56 | + |
|
| 57 | + case \is_object($source): |
|
| 58 | + return $this->fromObject($source); |
|
| 59 | + |
|
| 60 | + default: |
|
| 61 | + throw new \Exception("Models can only be mapped from arrays or objects.", 1); |
|
| 62 | + } |
|
| 63 | + } |
|
| 64 | + |
|
| 65 | + /** |
|
| 66 | + * Maps the model from an array of data. |
|
| 67 | + * |
|
| 68 | + * @param array $source |
|
| 69 | + * @return T |
|
| 70 | + */ |
|
| 71 | + protected function fromArray(array $source) |
|
| 72 | + { |
|
| 73 | + $model = $this->newInstance(); |
|
| 74 | + foreach ($source as $key => $value) { |
|
| 75 | + $this->setProperty($model, $key, $value); |
|
| 76 | + } |
|
| 77 | + return $model; |
|
| 78 | + } |
|
| 79 | + |
|
| 80 | + /** |
|
| 81 | + * Maps a model from an Object of data |
|
| 82 | + * |
|
| 83 | + * @param object $source |
|
| 84 | + * @return T |
|
| 85 | + */ |
|
| 86 | + protected function fromObject($source) |
|
| 87 | + { |
|
| 88 | + $vars = get_object_vars($source); |
|
| 89 | + return $this->fromArray($vars); |
|
| 90 | + } |
|
| 91 | + |
|
| 92 | + /** |
|
| 93 | + * Construct an instance of the model |
|
| 94 | + * |
|
| 95 | + * @return void |
|
| 96 | + */ |
|
| 97 | + protected function newInstance() |
|
| 98 | + { |
|
| 99 | + $class = $this->model; |
|
| 100 | + try { |
|
| 101 | + $instance = empty($this->constructorArgs) |
|
| 102 | + ? new $class() |
|
| 103 | + : new $class(...$this->constructorArgs); |
|
| 104 | + } catch (\Throwable $th) { |
|
| 105 | + throw new \Exception("Failed to construct model, {$th->getMessage()}", 1); |
|
| 106 | + } |
|
| 107 | + |
|
| 108 | + return $instance; |
|
| 109 | + } |
|
| 110 | + |
|
| 111 | + /** |
|
| 112 | + * Sets a property to the current model |
|
| 113 | + * |
|
| 114 | + * @param T $model |
|
| 115 | + * @param string $property |
|
| 116 | + * @param mixed $value |
|
| 117 | + * @return T |
|
| 118 | + */ |
|
| 119 | + protected function setProperty($model, string $property, $value) |
|
| 120 | + { |
|
| 121 | + $property = $this->normaliseProperty($property); |
|
| 122 | + |
|
| 123 | + // Attempt to set. |
|
| 124 | + try { |
|
| 125 | + switch (true) { |
|
| 126 | + case \method_exists($model, $this->generateSetterMethod($property)): |
|
| 127 | + $method = $this->generateSetterMethod($property); |
|
| 128 | + $model->$method($value); |
|
| 129 | + break; |
|
| 130 | + |
|
| 131 | + case \method_exists($model, $this->generateSetterMethod($property, true)): |
|
| 132 | + $method = $this->generateSetterMethod($property, true); |
|
| 133 | + $model->$method($value); |
|
| 134 | + break; |
|
| 135 | + |
|
| 136 | + default: |
|
| 137 | + $model->$property = $value; |
|
| 138 | + break; |
|
| 139 | + } |
|
| 140 | + } catch (\Throwable $th) { |
|
| 141 | + throw new \Exception(sprintf("Failed to set %s of %s model, %s", $property, get_class($model), $th->getMessage()), 1); |
|
| 142 | + } |
|
| 143 | + |
|
| 144 | + return $model; |
|
| 145 | + } |
|
| 146 | + |
|
| 147 | + /** |
|
| 148 | + * Normalises a property |
|
| 149 | + * |
|
| 150 | + * @param string $property |
|
| 151 | + * @return string |
|
| 152 | + */ |
|
| 153 | + protected function normaliseProperty(string $property): string |
|
| 154 | + { |
|
| 155 | + return \trim(preg_replace('/[^a-z0-9]+/', '_', strtolower($property))); |
|
| 156 | + } |
|
| 157 | + |
|
| 158 | + /** |
|
| 159 | + * Generates a generic setter method using either underscore [set_property()] or PSR2 style [setProperty()] |
|
| 160 | + * |
|
| 161 | + * @param string $property |
|
| 162 | + * @param bool $underscore |
|
| 163 | + * @return string |
|
| 164 | + */ |
|
| 165 | + protected function generateSetterMethod(string $property, bool $underscore = false): string |
|
| 166 | + { |
|
| 167 | + return $underscore |
|
| 168 | + ? "set_{$property}" |
|
| 169 | + : "set" . \ucfirst($property); |
|
| 170 | + } |
|
| 171 | 171 | } |
@@ -166,6 +166,6 @@ |
||
| 166 | 166 | { |
| 167 | 167 | return $underscore |
| 168 | 168 | ? "set_{$property}" |
| 169 | - : "set" . \ucfirst($property); |
|
| 169 | + : "set".\ucfirst($property); |
|
| 170 | 170 | } |
| 171 | 171 | } |