KeyValuePairsMapper   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 12
eloc 29
dl 0
loc 76
c 0
b 0
f 0
rs 10
ccs 31
cts 31
cp 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A awake() 0 26 5
A slumber() 0 24 5
A extractKv() 0 8 2
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;
0 ignored issues
show
Unused Code introduced by
The assignment to $keyToUse is dead and can be removed.
Loading history...
65 8
        $valToUse  = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $valToUse is dead and can be removed.
Loading history...
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