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
|
2 |
|
public static function hydrate(&$value, array $attributes = [], EntityManager $manager = null): void |
12
|
|
|
{ |
13
|
2 |
|
if (!empty($value)) { |
14
|
1 |
|
$value = self::deserializeValue($value, $attributes['storeAs']); |
15
|
1 |
|
if (!empty($value)) { |
16
|
1 |
|
$value = $manager->hydrate($attributes['class'], $value); |
|
|
|
|
17
|
|
|
} else { |
18
|
1 |
|
$value = null; |
19
|
|
|
} |
20
|
|
|
} else { |
21
|
1 |
|
$value = null; |
22
|
|
|
} |
23
|
2 |
|
} |
24
|
|
|
|
25
|
3 |
|
public static function extract(&$value, array $attributes = [], EntityManager $manager = null): void |
26
|
|
|
{ |
27
|
|
|
|
28
|
3 |
|
if ($value instanceof $attributes['class']) { |
29
|
2 |
|
$value = $manager->extract($value); |
30
|
2 |
|
$value = self::serializeValue($value, $attributes['storeAs']); |
31
|
|
|
} else { |
32
|
1 |
|
$value = null; |
33
|
|
|
} |
34
|
3 |
|
} |
35
|
|
|
|
36
|
5 |
|
public static function getDefaultAttributes(): array |
37
|
|
|
{ |
38
|
|
|
return [ |
39
|
5 |
|
'storeAs' => 'json', |
40
|
|
|
]; |
41
|
|
|
} |
42
|
|
|
|
43
|
1 |
|
private static function deserializeValue($value, string $strategy) |
44
|
|
|
{ |
45
|
|
|
switch ($strategy) { |
46
|
1 |
|
case 'json': |
47
|
1 |
|
$value = json_decode($value, true); |
48
|
1 |
|
break; |
49
|
|
|
case 'serialized': |
50
|
|
|
$value = unserialize($value); |
51
|
|
|
break; |
52
|
|
|
case 'plain': |
53
|
|
|
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
|
2 |
|
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
|
1 |
|
case 'serialized': |
68
|
|
|
$value = serialize($value); |
69
|
|
|
break; |
70
|
1 |
|
case 'plain': |
71
|
1 |
|
break; |
72
|
|
|
default: |
73
|
|
|
throw new HydratorException("Cannot hydrate embed entity, invalid storeAs attribute (${strategy})"); |
74
|
|
|
} |
75
|
|
|
|
76
|
2 |
|
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.