1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* File was created 30.09.2015 07:46 |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace PeekAndPoke\Component\Slumber\Core\Codec\Property; |
7
|
|
|
|
8
|
|
|
use PeekAndPoke\Component\Collections\Collection; |
9
|
|
|
use PeekAndPoke\Component\Slumber\Annotation\Slumber\AsKeyValuePairs; |
10
|
|
|
use PeekAndPoke\Component\Slumber\Core\Codec\Awaker; |
11
|
|
|
use PeekAndPoke\Component\Slumber\Core\Codec\Slumberer; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @method AsKeyValuePairs getOptions() |
15
|
|
|
* |
16
|
|
|
* @author Karsten J. Gerber <[email protected]> |
17
|
|
|
*/ |
18
|
|
|
class KeyValuePairsMapper extends AbstractCollectionMapper |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @param Slumberer $slumberer |
22
|
|
|
* @param array|\Traversable $value |
23
|
|
|
* |
24
|
|
|
* @return array|null |
25
|
|
|
*/ |
26
|
14 |
|
public function slumber(Slumberer $slumberer, $value) |
27
|
|
|
{ |
28
|
14 |
|
if (false === $this->isIterable($value)) { |
29
|
8 |
|
return null; |
30
|
|
|
} |
31
|
|
|
|
32
|
6 |
|
$result = []; |
33
|
6 |
|
$nested = $this->nested; |
34
|
6 |
|
$keepNulls = $nested->getOptions()->keepNullValuesInCollections(); |
35
|
|
|
|
36
|
6 |
|
foreach ($value as $k => $v) { |
37
|
|
|
|
38
|
5 |
|
$slumbering = $nested->slumber($slumberer, $v); |
39
|
|
|
|
40
|
|
|
// check if we should keep nulls |
41
|
5 |
|
if ($slumbering !== null || $keepNulls) { |
42
|
5 |
|
$result[] = [ |
43
|
5 |
|
'k' => (string) $k, |
44
|
5 |
|
'v' => $slumbering, |
45
|
|
|
]; |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
6 |
|
return $result; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param Awaker $awaker |
54
|
|
|
* @param array|\Traversable $value |
55
|
|
|
* |
56
|
|
|
* @return array|Collection |
57
|
|
|
*/ |
58
|
20 |
|
public function awake(Awaker $awaker, $value) |
59
|
|
|
{ |
60
|
20 |
|
if (false === $this->isIterable($value)) { |
61
|
12 |
|
return $this->createAwakeResult([]); |
62
|
|
|
} |
63
|
|
|
|
64
|
8 |
|
$keyToUse = null; |
|
|
|
|
65
|
8 |
|
$valToUse = null; |
|
|
|
|
66
|
8 |
|
$result = []; |
67
|
8 |
|
$nested = $this->nested; |
68
|
8 |
|
$keepNulls = $nested->getOptions()->keepNullValuesInCollections(); |
69
|
|
|
|
70
|
8 |
|
foreach ($value as $k => $v) { |
71
|
|
|
|
72
|
6 |
|
[$keyToUse, $valToUse] = self::extractKv($k, $v); |
73
|
|
|
|
74
|
6 |
|
$awoken = $nested->awake($awaker, $valToUse); |
75
|
|
|
|
76
|
|
|
// check if we should keep nulls |
77
|
6 |
|
if ($keepNulls || $awoken !== null) { |
78
|
6 |
|
$result[$keyToUse] = $awoken; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
// do we need to instantiate a collection class ? |
83
|
8 |
|
return $this->createAwakeResult($result); |
84
|
|
|
} |
85
|
|
|
|
86
|
6 |
|
private static function extractKv($k, $v) |
87
|
|
|
{ |
88
|
6 |
|
if (isset($v['k'], $v['v'])) { |
89
|
4 |
|
return [(string) $v['k'], $v['v']]; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
// bit of compatibility |
93
|
4 |
|
return [(string) $k, $v]; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|