ArrayableTrait.php
5 years ago
BenchmarkTrait.php
2 years ago
BooleanTransientTrait.php
5 years ago
DatabaseSearchReplaceTrait.php
2 years ago
DbRowsGeneratorTrait.php
4 years ago
DeveloperTimerTrait.php
4 years ago
FileScanToCacheTrait.php
2 years ago
HydrateTrait.php
2 years ago
MaintenanceTrait.php
5 years ago
MemoryExhaustTrait.php
2 years ago
MySQLRowsGeneratorTrait.php
2 years ago
NoticesTrait.php
2 years ago
PropertyConstructor.php
5 years ago
ResourceTrait.php
2 years ago
HydrateTrait.php
136 lines
| 1 | <?php |
| 2 | |
| 3 | // TODO PHP7.x; declare(strict_types=1); |
| 4 | // TODO PHP7.x type-hints & return types |
| 5 | |
| 6 | namespace WPStaging\Framework\Traits; |
| 7 | |
| 8 | use DateTime; |
| 9 | use Exception; |
| 10 | use ReflectionClass; |
| 11 | use ReflectionException; |
| 12 | use ReflectionMethod; |
| 13 | use WPStaging\Core\WPStaging; |
| 14 | use WPStaging\Framework\Adapter\DateTimeAdapter; |
| 15 | |
| 16 | use function WPStaging\functions\debug_log; |
| 17 | |
| 18 | trait HydrateTrait |
| 19 | { |
| 20 | |
| 21 | /** |
| 22 | * @param array $data |
| 23 | * @return $this |
| 24 | * @noinspection PhpDocMissingThrowsInspection |
| 25 | */ |
| 26 | public function hydrate(array $data = []) |
| 27 | { |
| 28 | foreach ($data as $key => $value) { |
| 29 | /** @noinspection PhpUnhandledExceptionInspection */ |
| 30 | try { |
| 31 | $this->hydrateByMethod('set' . ucfirst($key), $value); |
| 32 | } catch (Exception $e) { |
| 33 | $this->debugLog($e->getMessage()); |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | return $this; |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Use to hydrate public properties |
| 42 | * @param array $data |
| 43 | * @return $this |
| 44 | * @noinspection PhpDocMissingThrowsInspection |
| 45 | */ |
| 46 | public function hydrateProperties(array $data = []) |
| 47 | { |
| 48 | foreach ($data as $key => $value) { |
| 49 | if (!property_exists($this, $key)) { |
| 50 | $this->debugLog("Trying to hydrate DTO with property that does not exist. {$key}"); |
| 51 | continue; |
| 52 | } |
| 53 | |
| 54 | $this->{$key} = $value; |
| 55 | } |
| 56 | |
| 57 | return $this; |
| 58 | } |
| 59 | |
| 60 | protected function debugLog($message) |
| 61 | { |
| 62 | if (WPStaging::areLogsSilenced()) { |
| 63 | return; |
| 64 | } |
| 65 | |
| 66 | debug_log($message); |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * @param string $method |
| 71 | * @param mixed $value |
| 72 | * |
| 73 | * @throws ReflectionException |
| 74 | */ |
| 75 | private function hydrateByMethod($method, $value) |
| 76 | { |
| 77 | if (!method_exists($this, $method)) { |
| 78 | if (!is_string($value)) { |
| 79 | $value = wp_json_encode($value, JSON_UNESCAPED_SLASHES); |
| 80 | } |
| 81 | |
| 82 | throw new Exception(sprintf("Trying to hydrate DTO with value that does not exist. %s::%s(%s)", get_class($this), $method, $value)); |
| 83 | } |
| 84 | |
| 85 | /** @noinspection CallableParameterUseCaseInTypeContextInspection */ |
| 86 | $method = new ReflectionMethod($this, $method); |
| 87 | |
| 88 | $params = $method->getParameters(); |
| 89 | |
| 90 | if (!isset($params[0]) || count($params) > 1) { |
| 91 | throw new Exception(sprintf( |
| 92 | 'Class %s setter method %s does not have a first parameter or has more than one parameter', |
| 93 | static::class, |
| 94 | $method |
| 95 | )); |
| 96 | } |
| 97 | |
| 98 | $param = $params[0]; |
| 99 | |
| 100 | if (defined('PHP_VERSION_ID') && PHP_VERSION_ID >= 80000) { |
| 101 | $class = $param->getType() && !$param->getType()->isBuiltin() ? new ReflectionClass($param->getType()->getName()) : null; |
| 102 | } else { |
| 103 | $class = $param->getClass(); |
| 104 | } |
| 105 | |
| 106 | if (!$value || !$class) { |
| 107 | $method->invoke($this, $value); |
| 108 | return; |
| 109 | } |
| 110 | |
| 111 | /** @noinspection PhpUnhandledExceptionInspection */ |
| 112 | $method->invoke($this, $this->getClassAsValue($class, $value)); |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * @param ReflectionClass $class |
| 117 | * @param mixed $value |
| 118 | * @return object |
| 119 | * @throws Exception |
| 120 | */ |
| 121 | private function getClassAsValue(ReflectionClass $class, $value) |
| 122 | { |
| 123 | $className = $class->getName(); |
| 124 | if (!$value instanceof DateTime && $className === 'DateTime') { |
| 125 | return (new DateTimeAdapter())->getDateTime($value); |
| 126 | } |
| 127 | |
| 128 | $obj = new $className(); |
| 129 | if (is_array($value) && method_exists($obj, 'hydrate')) { |
| 130 | $obj->hydrate($value); |
| 131 | } |
| 132 | |
| 133 | return $obj; |
| 134 | } |
| 135 | } |
| 136 |