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

KeyValuePairsMapper   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 3
dl 0
loc 76
ccs 29
cts 29
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
B slumber() 0 25 6
B awake() 0 25 6
A extractKv() 0 9 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