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