PluginProbe ʕ •ᴥ•ʔ
WP STAGING – WordPress Backups, Restore, Migration & Clone / 3.0.2
WP STAGING – WordPress Backups, Restore, Migration & Clone v3.0.2
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 5 years ago DbRowsGeneratorTrait.php 4 years ago DeveloperTimerTrait.php 4 years ago FileScanToCacheTrait.php 3 years ago HydrateTrait.php 3 years ago MaintenanceTrait.php 5 years ago MySQLRowsGeneratorTrait.php 3 years ago NoticesTrait.php 3 years ago PropertyConstructor.php 5 years ago ResourceTrait.php 3 years ago
HydrateTrait.php
115 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 protected function debugLog($message)
41 {
42 if (WPStaging::areLogsSilenced()) {
43 return;
44 }
45
46 debug_log($message);
47 }
48
49 /**
50 * @param string $method
51 * @param mixed $value
52 *
53 * @throws ReflectionException
54 */
55 private function hydrateByMethod($method, $value)
56 {
57 if (!method_exists($this, $method)) {
58 if (!is_string($value)) {
59 $value = wp_json_encode($value, JSON_UNESCAPED_SLASHES);
60 }
61 throw new Exception(sprintf("Trying to hydrate DTO with value that does not exist. %s::%s(%s)", get_class($this), $method, $value));
62 }
63
64 /** @noinspection CallableParameterUseCaseInTypeContextInspection */
65 $method = new ReflectionMethod($this, $method);
66
67 $params = $method->getParameters();
68
69 if (!isset($params[0]) || count($params) > 1) {
70 throw new Exception(sprintf(
71 'Class %s setter method %s does not have a first parameter or has more than one parameter',
72 static::class,
73 $method
74 ));
75 }
76
77 $param = $params[0];
78
79 if (defined('PHP_VERSION_ID') && PHP_VERSION_ID >= 80000) {
80 $class = $param->getType() && !$param->getType()->isBuiltin() ? new ReflectionClass($param->getType()->getName()) : null;
81 } else {
82 $class = $param->getClass();
83 }
84
85 if (!$value || !$class) {
86 $method->invoke($this, $value);
87 return;
88 }
89
90 /** @noinspection PhpUnhandledExceptionInspection */
91 $method->invoke($this, $this->getClassAsValue($class, $value));
92 }
93
94 /**
95 * @param ReflectionClass $class
96 * @param mixed $value
97 * @return object
98 * @throws Exception
99 */
100 private function getClassAsValue(ReflectionClass $class, $value)
101 {
102 $className = $class->getName();
103 if (!$value instanceof DateTime && $className === 'DateTime') {
104 return (new DateTimeAdapter())->getDateTime($value);
105 }
106
107 $obj = new $className();
108 if (is_array($value) && method_exists($obj, 'hydrate')) {
109 $obj->hydrate($value);
110 }
111
112 return $obj;
113 }
114 }
115