Completed
Push — master ( 74a788...5880f8 )
by Karsten
03:03
created

KeyValuePairsMapper::extractKv()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 4
cts 4
cp 1
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 2
crap 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 (! \is_array($value) && ! $value instanceof \Traversable) {
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 (! \is_array($value) && ! $value instanceof \Traversable) {
61 12
            return $this->createAwakeResult([]);
62
        }
63
64 8
        $result    = [];
65 8
        $nested    = $this->nested;
66 8
        $keepNulls = $nested->getOptions()->keepNullValuesInCollections();
67
68 8
        foreach ($value as $k => $v) {
69
70 6
            [$keyToUse, $valToUse] = self::extractKv($k, $v);
0 ignored issues
show
Bug introduced by
The variable $keyToUse does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $valToUse does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
71
72 6
            $awoken = $nested->awake($awaker, $valToUse);
73
74
            // check if we should keep nulls
75 6
            if ($keepNulls || $awoken !== null) {
76 6
                $result[$keyToUse] = $awoken;
77
            }
78
        }
79
80
        // do we need to instantiate a collection class ?
81 8
        return $this->createAwakeResult($result);
82
    }
83
84 6
    private static function extractKv($k, $v)
85
    {
86 6
        if (isset($v['k'], $v['v'])) {
87 4
            return [(string) $v['k'], $v['v']];
88
        }
89
90
        // bit of compatibility
91 4
        return [$k, $v];
92
    }
93
}
94