|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace Igni\Storage\Mapping\Strategy; |
|
4
|
|
|
|
|
5
|
|
|
use Igni\Storage\EntityManager; |
|
6
|
|
|
use Igni\Storage\Exception\HydratorException; |
|
7
|
|
|
use Igni\Storage\Mapping\MappingStrategy; |
|
8
|
|
|
|
|
9
|
|
|
final class Embed implements MappingStrategy, DefaultAttributesProvider |
|
10
|
|
|
{ |
|
11
|
4 |
|
public static function hydrate(&$value, array $attributes = [], EntityManager $manager = null): void |
|
12
|
|
|
{ |
|
13
|
4 |
|
if (!empty($value)) { |
|
14
|
3 |
|
$value = self::deserializeValue($value, $attributes['storeAs']); |
|
15
|
3 |
|
if (!empty($value)) { |
|
16
|
3 |
|
$value = $manager->hydrate($attributes['class'], $value); |
|
|
|
|
|
|
17
|
|
|
} else { |
|
18
|
3 |
|
$value = null; |
|
19
|
|
|
} |
|
20
|
|
|
} else { |
|
21
|
1 |
|
$value = null; |
|
22
|
|
|
} |
|
23
|
4 |
|
} |
|
24
|
|
|
|
|
25
|
2 |
|
public static function extract(&$value, array $attributes = [], EntityManager $manager = null): void |
|
26
|
|
|
{ |
|
27
|
|
|
|
|
28
|
2 |
|
if ($value instanceof $attributes['class']) { |
|
29
|
1 |
|
$value = $manager->extract($value); |
|
30
|
1 |
|
$value = self::serializeValue($value, $attributes['storeAs']); |
|
31
|
|
|
} else { |
|
32
|
1 |
|
$value = null; |
|
33
|
|
|
} |
|
34
|
2 |
|
} |
|
35
|
|
|
|
|
36
|
5 |
|
public static function getDefaultAttributes(): array |
|
37
|
|
|
{ |
|
38
|
|
|
return [ |
|
39
|
5 |
|
'storeAs' => 'json', |
|
40
|
|
|
]; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
3 |
|
private static function deserializeValue($value, string $strategy) |
|
44
|
|
|
{ |
|
45
|
|
|
switch ($strategy) { |
|
46
|
3 |
|
case 'json': |
|
47
|
1 |
|
$value = json_decode($value, true); |
|
48
|
1 |
|
break; |
|
49
|
2 |
|
case 'serialized': |
|
50
|
|
|
$value = unserialize($value); |
|
51
|
|
|
break; |
|
52
|
2 |
|
case 'plain': |
|
53
|
2 |
|
break; |
|
54
|
|
|
default: |
|
55
|
|
|
throw new HydratorException("Cannot persist embed entity, invalid storeAs attribute (${strategy})"); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
return $value; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
private static function serializeValue($value, string $strategy) |
|
62
|
|
|
{ |
|
63
|
|
|
switch ($strategy) { |
|
64
|
1 |
|
case 'json': |
|
65
|
1 |
|
$value = json_encode($value, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_UNESCAPED_SLASHES | JSON_PRESERVE_ZERO_FRACTION); |
|
66
|
1 |
|
break; |
|
67
|
|
|
case 'serialized': |
|
68
|
|
|
$value = serialize($value); |
|
69
|
|
|
break; |
|
70
|
|
|
case 'plain': |
|
71
|
|
|
break; |
|
72
|
|
|
default: |
|
73
|
|
|
throw new HydratorException("Cannot hydrate embed entity, invalid storeAs attribute (${strategy})"); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
1 |
|
return $value; |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.