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