Completed
Push — master ( bb0f1c...d5b4f2 )
by Karsten
04:20
created

MapMapper::awakeKeepNulls()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 11
ccs 5
cts 5
cp 1
rs 9.4285
cc 2
eloc 5
nc 2
nop 3
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\AsMap;
10
use PeekAndPoke\Component\Slumber\Core\Codec\Awaker;
11
use PeekAndPoke\Component\Slumber\Core\Codec\Mapper;
12
use PeekAndPoke\Component\Slumber\Core\Codec\Slumberer;
13
14
/**
15
 * @method AsMap getOptions()
16
 *
17
 * @author Karsten J. Gerber <[email protected]>
18
 */
19
class MapMapper extends AbstractCollectionMapper
20
{
21
    /**
22
     * @param Slumberer          $slumberer
23
     * @param array|\Traversable $value
24
     *
25
     * @return null|\stdClass We return a std class as this will ensure json-encode will create something like {"0":1}
26
     */
27 17
    public function slumber(Slumberer $slumberer, $value)
28
    {
29 17
        if (false === $this->isIterable($value)) {
30 8
            return null;
31
        }
32
33 9
        $nested = $this->nested;
34
35 9
        if ($nested->getOptions()->keepNullValuesInCollections()) {
36 9
            return $this->slumberKeepNulls($slumberer, $value, $nested);
0 ignored issues
show
Bug introduced by
It seems like $value defined by parameter $value on line 27 can also be of type object<Traversable>; however, PeekAndPoke\Component\Sl...per::slumberKeepNulls() does only seem to accept array, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
37
        }
38
39 3
        return $this->slumberFilterNulls($slumberer, $value, $nested);
0 ignored issues
show
Bug introduced by
It seems like $value defined by parameter $value on line 27 can also be of type object<Traversable>; however, PeekAndPoke\Component\Sl...r::slumberFilterNulls() does only seem to accept array, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
40
    }
41
42
    /**
43
     * @param Awaker             $awaker
44
     * @param array|\Traversable $value
45
     *
46
     * @return array|Collection
47
     */
48 20
    public function awake(Awaker $awaker, $value)
49
    {
50 20
        if (false === $this->isIterable($value)) {
51 12
            return $this->createAwakeResult([]);
52
        }
53
54 8
        $nested = $this->nested;
55
56
        // TODO: we need a test that checks that an awaken map is a collection when a collection is specified
57
        // TODO: packing things into a collection should be a wrapper around this mapper and all other collection mappers
58 8
        return $this->createAwakeResult(
59 8
            $nested->getOptions()->keepNullValuesInCollections()
60 8
                ? $this->awakeKeepNulls($awaker, $value, $nested)
0 ignored issues
show
Bug introduced by
It seems like $value defined by parameter $value on line 48 can also be of type object<Traversable>; however, PeekAndPoke\Component\Sl...apper::awakeKeepNulls() does only seem to accept array, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
61 8
                : $this->awakeFilterNulls($awaker, $value, $nested)
0 ignored issues
show
Bug introduced by
It seems like $value defined by parameter $value on line 48 can also be of type object<Traversable>; however, PeekAndPoke\Component\Sl...per::awakeFilterNulls() does only seem to accept array, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
62
        );
63
    }
64
65
    /**
66
     * @param Slumberer $slumberer
67
     * @param array     $value
68
     * @param Mapper    $nested
69
     *
70
     * @return \stdClass
71
     */
72 9
    private function slumberKeepNulls(Slumberer $slumberer, $value, Mapper $nested)
73
    {
74 9
        $result = new \stdClass();
75
76 9
        foreach ($value as $k => $v) {
77 5
            $result->$k = $nested->slumber($slumberer, $v);
78
        }
79
80 9
        return $result;
81
    }
82
83
    /**
84
     * @param Slumberer $slumberer
85
     * @param array     $value
86
     * @param Mapper    $nested
87
     *
88
     * @return \stdClass
89
     */
90 3
    private function slumberFilterNulls(Slumberer $slumberer, $value, Mapper $nested)
91
    {
92 3
        $result = new \stdClass();
93
94 3 View Code Duplication
        foreach ($value as $k => $v) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
95
96
            $slumbering = $nested->slumber($slumberer, $v);
97
98
            // check if we should keep nulls
99
            if ($slumbering !== null) {
100
                $result->$k = $slumbering;
101
            }
102
        }
103
104 3
        return $result;
105
    }
106
107
    /**
108
     * @param Awaker $awaker
109
     * @param array  $value
110
     * @param Mapper $nested
111
     *
112
     * @return array
113
     */
114 8
    private function awakeKeepNulls(Awaker $awaker, $value, Mapper $nested)
115
    {
116 8
        $result = [];
117
118 8
        foreach ($value as $k => $v) {
119 6
            $result[$k] = $nested->awake($awaker, $v);
120
        }
121
122 8
        return $result;
123
124
    }
125
126
    /**
127
     * @param Awaker $awaker
128
     * @param array  $value
129
     * @param Mapper $nested
130
     *
131
     * @return array
132
     */
133
    private function awakeFilterNulls(Awaker $awaker, $value, Mapper $nested)
134
    {
135
        $result = [];
136
137 View Code Duplication
        foreach ($value as $k => $v) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
138
139
            $awoken = $nested->awake($awaker, $v);
140
141
            // check if we should keep nulls
142
            if ($awoken !== null) {
143
                $result[$k] = $awoken;
144
            }
145
        }
146
147
        return $result;
148
    }
149
}
150