1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Audiens\DoubleclickClient\entity; |
4
|
|
|
|
5
|
|
|
use Audiens\DoubleclickClient\exceptions\ClientException; |
6
|
|
|
use Doctrine\Common\Annotations\AnnotationReader; |
7
|
|
|
use Doctrine\Common\Annotations\AnnotationRegistry; |
8
|
|
|
use Exception; |
9
|
|
|
use GiacomoFurlan\ObjectTransmapperValidator\Transmapper; |
10
|
|
|
use RuntimeException; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Trait TransmapHydratable |
14
|
|
|
*/ |
15
|
|
|
trait TransmapHydratable |
16
|
|
|
{ |
17
|
|
|
private static $transmapper; |
18
|
|
|
|
19
|
|
|
private static function getTransmapper(): Transmapper |
20
|
|
|
{ |
21
|
|
|
if (!static::$transmapper) { |
|
|
|
|
22
|
|
|
$loader = include __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php'; |
23
|
|
|
AnnotationRegistry::registerLoader([$loader, 'loadClass']); |
|
|
|
|
24
|
|
|
|
25
|
|
|
static::$transmapper = new Transmapper(new AnnotationReader()); |
|
|
|
|
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
return static::$transmapper; |
|
|
|
|
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param array $objectArray |
33
|
|
|
* @return array |
34
|
|
|
*/ |
35
|
|
|
protected static function hydratePreprocess(array $objectArray): array |
36
|
|
|
{ |
37
|
|
|
return $objectArray; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param array $objectArray |
42
|
|
|
* |
43
|
|
|
* @return static |
44
|
|
|
* @throws \Audiens\DoubleclickClient\exceptions\ClientException |
45
|
|
|
*/ |
46
|
|
|
public static function fromArray(array $objectArray) |
47
|
|
|
{ |
48
|
|
|
$object = (object) static::hydratePreprocess($objectArray); |
49
|
|
|
try { |
50
|
|
|
return static::getTransmapper()->map($object, static::class); |
51
|
|
|
} catch (Exception $exception) { |
52
|
|
|
throw new ClientException($exception->getMessage(), $exception->getCode(), $exception); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @return array |
58
|
|
|
* @throws \RuntimeException |
59
|
|
|
*/ |
60
|
|
|
public function toArray() |
61
|
|
|
{ |
62
|
|
|
throw new RuntimeException('Not implemented yet'); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
Let’s assume you have a class which uses late-static binding:
The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the
getSomeVariable()
on that sub-class, you will receive a runtime error:In the case above, it makes sense to update
SomeClass
to useself
instead: