1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of RussianPost SDK package. |
5
|
|
|
* |
6
|
|
|
* © Appwilio (http://appwilio.com), greabock (https://github.com/greabock), JhaoDa (https://github.com/jhaoda) |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace Appwilio\RussianPostSDK\Dispatching; |
15
|
|
|
|
16
|
|
|
use Appwilio\RussianPostSDK\Core\ArrayOf; |
17
|
|
|
|
18
|
|
|
class Instantiator |
19
|
|
|
{ |
20
|
|
|
/** @var string */ |
21
|
|
|
private $class; |
22
|
|
|
|
23
|
|
|
/** @var bool */ |
24
|
|
|
private $array = false; |
25
|
|
|
|
26
|
|
|
/** @var \ReflectionClass */ |
27
|
|
|
private $reflector; |
28
|
|
|
|
29
|
29 |
|
public function __construct($class) |
30
|
|
|
{ |
31
|
29 |
|
if ($class instanceof ArrayOf) { |
32
|
6 |
|
$this->array = true; |
33
|
6 |
|
$this->class = $class->getClass(); |
34
|
|
|
} else { |
35
|
25 |
|
$this->class = $class; |
36
|
|
|
} |
37
|
|
|
|
38
|
29 |
|
$this->reflector = new \ReflectionClass($this->class); |
39
|
29 |
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param ArrayOf|string $class |
43
|
|
|
* @param mixed $data |
44
|
|
|
* |
45
|
|
|
* @return mixed |
46
|
|
|
*/ |
47
|
28 |
|
public static function instantiate($class, $data) |
48
|
|
|
{ |
49
|
28 |
|
if (null === $data) { |
50
|
1 |
|
return null; |
51
|
|
|
} |
52
|
|
|
|
53
|
27 |
|
return (new self($class))->fill($data); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param string $class |
58
|
|
|
* @param object $source |
59
|
|
|
* @param array $unset |
60
|
|
|
* |
61
|
|
|
* @throws \ReflectionException |
62
|
|
|
* |
63
|
|
|
* @return mixed |
64
|
|
|
*/ |
65
|
1 |
|
public static function instantiateFrom(string $class, $source, array $unset = []) |
66
|
|
|
{ |
67
|
1 |
|
$sourceReflector = (new \ReflectionClass($source))->getProperty('data'); |
68
|
1 |
|
$sourceReflector->setAccessible(true); |
69
|
|
|
|
70
|
1 |
|
$destination = (new \ReflectionClass($class))->newInstanceWithoutConstructor(); |
71
|
|
|
|
72
|
1 |
|
$destinationReflector = (new \ReflectionClass($destination))->getProperty('data'); |
73
|
1 |
|
$destinationReflector->setAccessible(true); |
74
|
|
|
|
75
|
1 |
|
$data = $sourceReflector->getValue($source); |
76
|
|
|
|
77
|
1 |
|
foreach ($unset as $item) { |
78
|
1 |
|
unset($data[$item]); |
79
|
|
|
} |
80
|
|
|
|
81
|
1 |
|
$destinationReflector->setValue($destination, $data); |
82
|
|
|
|
83
|
1 |
|
return $destination; |
84
|
|
|
} |
85
|
|
|
|
86
|
28 |
|
public function fill($data) |
87
|
|
|
{ |
88
|
28 |
|
if (null === $data) { |
89
|
1 |
|
return null; |
90
|
|
|
} |
91
|
|
|
|
92
|
27 |
|
if ($this->array) { |
93
|
5 |
|
$objects = []; |
94
|
|
|
|
95
|
5 |
|
foreach ($data as $item) { |
96
|
5 |
|
$objects[] = $this->build($this->class, $item); |
97
|
|
|
} |
98
|
|
|
|
99
|
5 |
|
return $objects; |
100
|
|
|
} |
101
|
|
|
|
102
|
23 |
|
return $this->build($this->class, $data); |
103
|
|
|
} |
104
|
|
|
|
105
|
27 |
|
private function build(string $class, array $data) |
106
|
|
|
{ |
107
|
27 |
|
$object = (new \ReflectionClass($class))->newInstanceWithoutConstructor(); |
108
|
|
|
|
109
|
27 |
|
$property = $this->reflector->getProperty('data'); |
110
|
|
|
|
111
|
26 |
|
$property->setAccessible(true); |
112
|
|
|
|
113
|
26 |
|
$property->setValue($object, $data); |
114
|
|
|
|
115
|
26 |
|
return $object; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|