1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace WyriHaximus\ApiClient\Transport; |
5
|
|
|
|
6
|
|
|
use GeneratedHydrator\Configuration; |
7
|
|
|
use WyriHaximus\ApiClient\Resource\ResourceInterface; |
8
|
|
|
use Zend\Hydrator\HydratorInterface; |
9
|
|
|
|
10
|
|
|
class Hydrator |
11
|
|
|
{ |
12
|
|
|
protected $options; |
13
|
|
|
protected $transport; |
14
|
|
|
protected $hydrators = []; |
15
|
|
|
|
16
|
8 |
|
public function __construct(Client $transport, array $options) |
17
|
|
|
{ |
18
|
8 |
|
$this->transport = $transport; |
19
|
8 |
|
$this->options = $options; |
20
|
8 |
|
} |
21
|
|
|
|
22
|
3 |
|
public function hydrateFQCN($class, $json): ResourceInterface |
23
|
|
|
{ |
24
|
3 |
|
$hydrator = $this->getHydrator($class); |
25
|
3 |
|
$object = $this->createObject($class); |
26
|
3 |
|
return $hydrator->hydrate($json, $object); |
27
|
|
|
} |
28
|
|
|
|
29
|
2 |
|
public function hydrate($class, $json): ResourceInterface |
30
|
|
|
{ |
31
|
2 |
|
$fullClassName = $this->options['namespace'] . '\\' . $this->options['resource_namespace'] . '\\' . $class; |
32
|
2 |
|
return $this->hydrateFQCN($fullClassName, $json); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Takes a fully qualified class name and extracts the data for that class from the given $object |
37
|
|
|
* @param $class |
38
|
|
|
* @param $object |
39
|
|
|
* @return array |
40
|
|
|
*/ |
41
|
2 |
|
public function extractFQCN($class, $object): array |
42
|
|
|
{ |
43
|
2 |
|
return $this->getHydrator($class)->extract($object); |
44
|
|
|
} |
45
|
|
|
|
46
|
1 |
|
public function extract($class, $object): array |
47
|
|
|
{ |
48
|
1 |
|
$fullClassName = $this->options['namespace'] . '\\' . $this->options['resource_namespace'] . '\\' . $class; |
49
|
1 |
|
return $this->extractFQCN($fullClassName, $object); |
50
|
|
|
} |
51
|
|
|
|
52
|
1 |
|
public function buildAsyncFromSync($resource, $object): ResourceInterface |
53
|
|
|
{ |
54
|
1 |
|
return $this->hydrateFQCN( |
55
|
1 |
|
$this->options['namespace'] . '\\Async\\' . $resource, |
56
|
1 |
|
$this->extractFQCN( |
57
|
1 |
|
$this->options['namespace'] . '\\Sync\\' . $resource, |
58
|
|
|
$object |
59
|
|
|
) |
60
|
|
|
); |
61
|
|
|
} |
62
|
|
|
|
63
|
3 |
|
protected function getHydrator($class): HydratorInterface |
64
|
|
|
{ |
65
|
3 |
|
if (isset($this->hydrators[$class])) { |
66
|
1 |
|
return $this->hydrators[$class]; |
67
|
|
|
} |
68
|
|
|
|
69
|
3 |
|
$config = new Configuration($class); |
70
|
3 |
|
if (isset($this->options['resource_hydrator_cache_dir'])) { |
71
|
3 |
|
$config->setGeneratedClassesTargetDir($this->options['resource_hydrator_cache_dir']); |
72
|
|
|
} |
73
|
3 |
|
if (isset($this->options['resource_hydrator_namespace'])) { |
74
|
3 |
|
$config->setGeneratedClassesNamespace($this->options['resource_hydrator_namespace']); |
75
|
|
|
} |
76
|
3 |
|
$hydrator = $config->createFactory()->getHydratorClass(); |
77
|
3 |
|
$this->hydrators[$class] = new $hydrator; |
78
|
|
|
|
79
|
3 |
|
return $this->hydrators[$class]; |
80
|
|
|
} |
81
|
|
|
|
82
|
3 |
|
protected function createObject($class): ResourceInterface |
83
|
|
|
{ |
84
|
3 |
|
$object = new $class(); |
85
|
3 |
|
$object->setTransport($this->transport); |
86
|
3 |
|
return $object; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|