PluginProbe ʕ •ᴥ•ʔ
WP STAGING – WordPress Backups, Restore, Migration & Clone / 4.11.1
WP STAGING – WordPress Backups, Restore, Migration & Clone v4.11.1
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
ApplyFiltersTrait.php 1 week ago ArrayableTrait.php 1 week ago BatchSizeCalculateTrait.php 1 week ago BearerTokenTrait.php 1 week ago BenchmarkTrait.php 1 week ago BooleanTransientTrait.php 1 week ago DatabaseSearchReplaceTrait.php 1 week ago DbRowsGeneratorTrait.php 1 week ago DebugLogTrait.php 1 week ago DeveloperTimerTrait.php 1 week ago EndOfLinePlaceholderTrait.php 1 week ago EventLoggerTrait.php 1 week ago FileScanToCacheTrait.php 1 week ago FormatTrait.php 1 week ago HttpRequestTrait.php 1 week ago HydrateTrait.php 1 week ago I18nTrait.php 1 week ago IpResolverTrait.php 1 week ago JobResponseTrait.php 1 week ago MaintenanceTrait.php 7 months ago MemoryExhaustTrait.php 1 week ago MySQLRowsGeneratorTrait.php 1 week ago NoticesTrait.php 1 week ago PagesTrait.php 1 week ago PropertyConstructor.php 1 week ago RenameTmpDirectoryTrait.php 1 week ago ResourceTrait.php 1 week ago RestRequestTrait.php 1 week ago RestoreFileExclusionTrait.php 1 week ago SafeFileInfoTrait.php 1 week ago SerializeTrait.php 6 days ago SetTimeLimitTrait.php 1 week ago SlashTrait.php 1 week ago SqlCommentTrait.php 1 week ago TablePrefixValidator.php 5 months ago UrlTrait.php 1 week ago ValueGetterTrait.php 1 week ago WindowsOsTrait.php 1 week 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
10 protected $excludeHydrate = [];
11
12
13
14
15
16
17 public function hydrate(array $data = [])
18 {
19 foreach ($data as $key => $value) {
20
21 $propertiesToExclude = array_merge($this->excludeHydrate, ['excludeHydrate']);
22 if (in_array($key, $propertiesToExclude, true)) {
23 continue;
24 }
25
26
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
41
42
43
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
61
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
78
79
80
81
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
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
120 $method->invoke($this, $this->getClassAsValue($class, $value));
121 }
122
123
124
125
126
127
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