PluginProbe ʕ •ᴥ•ʔ
WP STAGING – WordPress Backups, Restore, Migration & Clone / 3.3.3
WP STAGING – WordPress Backups, Restore, Migration & Clone v3.3.3
4.11.2 4.11.1 4.11.0 4.10.0 4.9.5 4.9.4 4.9.3 4.9.2 4.9.1 4.9.0 4.8.1 trunk 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.1.0 3.1.1 3.1.2 3.1.3 3.1.4 3.10.0 3.2.0 3.3.1 3.3.2 3.3.3 3.4.1 3.4.3 3.5.0 3.6.0 3.7.1 3.8.0 3.8.1 3.8.2 3.8.3 3.8.4 3.8.5 3.8.6 3.8.7 3.9.0 3.9.1 3.9.2 3.9.3 3.9.4 4.0.0 4.1.0 4.1.1 4.1.2 4.1.3 4.1.4 4.2.0 4.2.1 4.3.0 4.3.1 4.3.2 4.4.0 4.5.0 4.6.0 4.7.0 4.7.1 4.7.2 4.7.3 4.8.0
wp-staging / Framework / Traits / HydrateTrait.php
wp-staging / Framework / Traits Last commit date
ArrayableTrait.php 5 years ago BenchmarkTrait.php 3 years ago BooleanTransientTrait.php 5 years ago DatabaseSearchReplaceTrait.php 2 years ago DbRowsGeneratorTrait.php 4 years ago DeveloperTimerTrait.php 4 years ago FileScanToCacheTrait.php 3 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
138 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 TypeError;
14 use WPStaging\Core\WPStaging;
15 use WPStaging\Framework\Adapter\DateTimeAdapter;
16
17 use function WPStaging\functions\debug_log;
18
19 trait HydrateTrait
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 (TypeError $e) {
33 $this->debugLog($e->getMessage());
34 } catch (Exception $e) {
35 $this->debugLog($e->getMessage());
36 }
37 }
38
39 return $this;
40 }
41
42 /**
43 * Use to hydrate public properties
44 * @param array $data
45 * @return $this
46 * @noinspection PhpDocMissingThrowsInspection
47 */
48 public function hydrateProperties(array $data = [])
49 {
50 foreach ($data as $key => $value) {
51 if (!property_exists($this, $key)) {
52 $this->debugLog("Trying to hydrate DTO with property that does not exist. {$key}");
53 continue;
54 }
55
56 $this->{$key} = $value;
57 }
58
59 return $this;
60 }
61
62 protected function debugLog($message)
63 {
64 if (WPStaging::areLogsSilenced()) {
65 return;
66 }
67
68 debug_log($message);
69 }
70
71 /**
72 * @param string $method
73 * @param mixed $value
74 *
75 * @throws ReflectionException
76 */
77 private function hydrateByMethod($method, $value)
78 {
79 if (!method_exists($this, $method)) {
80 if (!is_string($value)) {
81 $value = wp_json_encode($value, JSON_UNESCAPED_SLASHES);
82 }
83
84 throw new Exception(sprintf("Trying to hydrate DTO with value that does not exist. %s::%s(%s)", get_class($this), $method, $value));
85 }
86
87 /** @noinspection CallableParameterUseCaseInTypeContextInspection */
88 $method = new ReflectionMethod($this, $method);
89
90 $params = $method->getParameters();
91
92 if (!isset($params[0]) || count($params) > 1) {
93 throw new Exception(sprintf(
94 'Class %s setter method %s does not have a first parameter or has more than one parameter',
95 static::class,
96 $method
97 ));
98 }
99
100 $param = $params[0];
101
102 if (defined('PHP_VERSION_ID') && PHP_VERSION_ID >= 80000) {
103 $class = $param->getType() && !$param->getType()->isBuiltin() ? new ReflectionClass($param->getType()->getName()) : null;
104 } else {
105 $class = $param->getClass();
106 }
107
108 if (!$value || !$class) {
109 $method->invoke($this, $value);
110 return;
111 }
112
113 /** @noinspection PhpUnhandledExceptionInspection */
114 $method->invoke($this, $this->getClassAsValue($class, $value));
115 }
116
117 /**
118 * @param ReflectionClass $class
119 * @param mixed $value
120 * @return object
121 * @throws Exception
122 */
123 private function getClassAsValue(ReflectionClass $class, $value)
124 {
125 $className = $class->getName();
126 if (!$value instanceof DateTime && $className === 'DateTime') {
127 return (new DateTimeAdapter())->getDateTime($value);
128 }
129
130 $obj = new $className();
131 if (is_array($value) && method_exists($obj, 'hydrate')) {
132 $obj->hydrate($value);
133 }
134
135 return $obj;
136 }
137 }
138