ArrayCodec::slumber()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 16
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 16
c 0
b 0
f 0
rs 10
ccs 8
cts 8
cp 1
cc 4
nc 4
nop 1
crap 4
1
<?php
2
/**
3
 * File was created 07.10.2015 15:20
4
 */
5
6
namespace PeekAndPoke\Component\Slumber\Core\Codec;
7
8
use PeekAndPoke\Component\Slumber\Core\LookUp\EntityConfigReader;
9
10
/**
11
 * @author Karsten J. Gerber <[email protected]>
12
 */
13
class ArrayCodec
14
{
15
    /** @var GenericSlumberer */
16
    private $slumberer;
17
    /** @var GenericAwaker */
18
    private $awaker;
19
20
    /**
21
     * @param EntityConfigReader $entityConfigReader
22
     */
23 26
    public function __construct(EntityConfigReader $entityConfigReader)
24
    {
25 26
        $this->slumberer = new GenericSlumberer($entityConfigReader);
26 26
        $this->awaker    = new GenericAwaker($entityConfigReader);
27 26
    }
28
29
    /**
30
     * @param mixed $subject
31
     *
32
     * @return array|mixed|null
33
     */
34 14
    public function slumber($subject)
35
    {
36 14
        if (\is_scalar($subject)) {  // todo: test this
37 7
            return $subject;
38
        }
39
40 12
        if (\is_array($subject)) {
41 8
            return $this->slumberArray($subject);
42
        }
43
44 10
        if (empty($subject)) {
45 3
            return null;
46
        }
47
48
        // put to sleep
49 7
        return (array) $this->slumberer->slumber($subject);
50
    }
51
52
    /**
53
     * @param mixed[] $subjects
54
     *
55
     * @return array
56
     */
57 8
    public function slumberArray(array $subjects)
58
    {
59 8
        $result = [];
60
61
        /**
62
         * @var string|int $key
63
         * @var mixed      $subject
64
         */
65 8
        foreach ($subjects as $key => $subject) {
66 8
            $result[$key] = $this->slumber($subject);
67
        }
68
69 8
        return $result;
70
    }
71
72
    /**
73
     * @param mixed                   $data
74
     * @param string|\ReflectionClass $cls
75
     *
76
     * @return mixed|null
77
     */
78 8
    public function awake($data, $cls)
79
    {
80 8
        if ($data === null || ! \is_array($data)) {
81 5
            return null;
82
        }
83
84 4
        if (! $cls instanceof \ReflectionClass) {
85 1
            $cls = new \ReflectionClass($cls);
86
        }
87
88 4
        return $this->awaker->awake($data, $cls);
89
    }
90
91
    /**
92
     * @param array                   $data
93
     * @param string|\ReflectionClass $cls
94
     *
95
     * @return array
96
     */
97 6
    public function awakeList($data, $cls)
98
    {
99 6
        if (! \is_array($data) && ! $data instanceof \Traversable) {
0 ignored issues
show
introduced by
The condition is_array($data) is always true.
Loading history...
100 4
            return [];
101
        }
102
103 2
        if (! $cls instanceof \ReflectionClass) {
104 2
            $cls = new \ReflectionClass($cls);
105
        }
106
107 2
        $result = [];
108
109 2
        foreach ($data as $k => $v) {
110 2
            $result[$k] = $this->awake($v, $cls);
111
        }
112
113 2
        return $result;
114
    }
115
}
116