|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Vox\Webservice\Proxy; |
|
4
|
|
|
|
|
5
|
|
|
use ProxyManager\Factory\AccessInterceptorValueHolderFactory; |
|
6
|
|
|
use ProxyManager\Proxy\AccessInterceptorValueHolderInterface; |
|
7
|
|
|
use Vox\Metadata\PropertyMetadata; |
|
8
|
|
|
use Vox\Webservice\Mapping\BelongsTo; |
|
9
|
|
|
use Vox\Webservice\Metadata\TransferMetadata; |
|
10
|
|
|
use Vox\Webservice\TransferManagerInterface; |
|
11
|
|
|
|
|
12
|
|
|
class ProxyFactory implements ProxyFactoryInterface |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var AccessInterceptorValueHolderFactory |
|
16
|
|
|
*/ |
|
17
|
|
|
private $accessInterceptorFactory; |
|
18
|
|
|
|
|
19
|
6 |
|
public function __construct() |
|
20
|
|
|
{ |
|
21
|
6 |
|
$this->accessInterceptorFactory = new AccessInterceptorValueHolderFactory(); |
|
22
|
6 |
|
} |
|
23
|
|
|
|
|
24
|
5 |
|
public function createProxy($class, TransferManagerInterface $transferManager): AccessInterceptorValueHolderInterface |
|
25
|
|
|
{ |
|
26
|
5 |
|
$className = is_object($class) ? get_class($class) : $class; |
|
27
|
5 |
|
$metadata = $transferManager->getClassMetadata($className); |
|
28
|
5 |
|
$object = is_object($class) ? $class : new $class(); |
|
29
|
|
|
|
|
30
|
5 |
|
$interceptors = []; |
|
31
|
|
|
|
|
32
|
5 |
|
foreach ($metadata->propertyMetadata as $name => $config) { |
|
33
|
5 |
|
$getter = sprintf('get%s', ucfirst($name)); |
|
34
|
|
|
|
|
35
|
5 |
|
if (isset($metadata->methodMetadata[$getter])) { |
|
36
|
5 |
|
$interceptors[$getter] = $this->createGetterInterceptor($metadata, $name, $object, $transferManager); |
|
37
|
|
|
} |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
5 |
|
return $this->accessInterceptorFactory->createProxy($object, $interceptors); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
private function createGetterInterceptor( |
|
44
|
|
|
TransferMetadata $metadata, |
|
45
|
|
|
string $name, |
|
46
|
|
|
$object, |
|
47
|
|
|
TransferManagerInterface $transferManager |
|
48
|
|
|
): callable { |
|
49
|
5 |
|
return function () use ($metadata, $name, $object, $transferManager) { |
|
50
|
|
|
/* @var $propertyMetadata PropertyMetadata */ |
|
51
|
1 |
|
$propertyMetadata = $metadata->propertyMetadata[$name]; |
|
52
|
1 |
|
$type = $propertyMetadata->type; |
|
53
|
|
|
|
|
54
|
1 |
|
if (class_exists($type)) { |
|
55
|
1 |
|
$belongsTo = $propertyMetadata->getAnnotation(BelongsTo::class); |
|
56
|
|
|
|
|
57
|
1 |
|
if ($belongsTo instanceof BelongsTo && empty($propertyMetadata->getValue($object))) { |
|
58
|
|
|
$data = $transferManager |
|
59
|
1 |
|
->find($type, $metadata->propertyMetadata[$belongsTo->foreignField]->getValue($object)); |
|
60
|
|
|
|
|
61
|
1 |
|
$propertyMetadata->setValue($object, $data); |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
5 |
|
}; |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|