|
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\Dispatching\Http\ArrayOf; |
|
17
|
|
|
|
|
18
|
|
|
class Instantiator |
|
19
|
|
|
{ |
|
20
|
|
|
/** @var ArrayOf|string $class */ |
|
21
|
|
|
private $class; |
|
22
|
|
|
|
|
23
|
|
|
/** @var bool */ |
|
24
|
|
|
private $array = false; |
|
25
|
|
|
|
|
26
|
|
|
/** @var \ReflectionClass */ |
|
27
|
|
|
private $reflector; |
|
28
|
|
|
|
|
29
|
|
|
public function __construct($class) |
|
30
|
|
|
{ |
|
31
|
|
|
if ($class instanceof ArrayOf) { |
|
32
|
|
|
$this->array = true; |
|
33
|
|
|
$this->class = $class->getClass(); |
|
34
|
|
|
} else { |
|
35
|
|
|
$this->class = $class; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
$this->reflector = new \ReflectionClass($this->class); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @param ArrayOf|string $class |
|
43
|
|
|
* @param mixed $data |
|
44
|
|
|
* |
|
45
|
|
|
* @return mixed |
|
46
|
|
|
*/ |
|
47
|
|
|
public static function instantiate($class, $data) |
|
48
|
|
|
{ |
|
49
|
|
|
if (null === $data) { |
|
50
|
|
|
return null; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
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
|
|
|
public static function instantiateFrom(string $class, $source, array $unset = []) |
|
66
|
|
|
{ |
|
67
|
|
|
$sourceReflector = (new \ReflectionClass($source))->getProperty('data'); |
|
68
|
|
|
$sourceReflector->setAccessible(true); |
|
69
|
|
|
|
|
70
|
|
|
$destination = (new \ReflectionClass($class))->newInstanceWithoutConstructor(); |
|
71
|
|
|
|
|
72
|
|
|
$destinationReflector = (new \ReflectionClass($destination))->getProperty('data'); |
|
73
|
|
|
$destinationReflector->setAccessible(true); |
|
74
|
|
|
|
|
75
|
|
|
$data = $sourceReflector->getValue($source); |
|
76
|
|
|
|
|
77
|
|
|
foreach ($unset as $item) { |
|
78
|
|
|
unset($data[$item]); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
$destinationReflector->setValue($destination, $data); |
|
82
|
|
|
|
|
83
|
|
|
return $destination; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
public function fill($data) |
|
87
|
|
|
{ |
|
88
|
|
|
if (null === $data) { |
|
89
|
|
|
return null; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
if ($this->array) { |
|
93
|
|
|
$objects = []; |
|
94
|
|
|
|
|
95
|
|
|
foreach ($data as $item) { |
|
96
|
|
|
$objects[] = $this->build($this->class, $item); |
|
|
|
|
|
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
return $objects; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
return $this->build($this->class, $data); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
private function build(string $class, array $data) |
|
106
|
|
|
{ |
|
107
|
|
|
$object = new $class(); |
|
108
|
|
|
|
|
109
|
|
|
$property = $this->reflector->getProperty('data'); |
|
110
|
|
|
|
|
111
|
|
|
$property->setAccessible(true); |
|
112
|
|
|
|
|
113
|
|
|
$property->setValue($object, $data); |
|
114
|
|
|
|
|
115
|
|
|
return $object; |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|