Completed
Pull Request — master (#1)
by Karsten
03:23
created

ArrayCodec::awake()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 12
c 0
b 0
f 0
ccs 0
cts 6
cp 0
rs 9.2
cc 4
eloc 6
nc 3
nop 2
crap 20
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 EntityConfigReader */
16
    private $entityConfigReader;
17
    /** @var GenericSlumberer */
18
    private $slumberer;
19
    /** @var GenericAwaker */
20
    private $awaker;
21
22
    /**
23
     * @param EntityConfigReader $entityConfigReader
24
     */
25 1
    public function __construct(EntityConfigReader $entityConfigReader)
26
    {
27 1
        $this->entityConfigReader = $entityConfigReader;
28
29 1
        $this->slumberer = new GenericSlumberer($entityConfigReader);
30 1
        $this->awaker    = new GenericAwaker($entityConfigReader);
31 1
    }
32
33
    /**
34
     * @return EntityConfigReader
35
     */
36
    public function getEntityConfigReader()
37
    {
38
        return $this->entityConfigReader;
39
    }
40
41
    /**
42
     * @param mixed $subject
43
     *
44
     * @return array|mixed|null
45
     */
46 1
    public function slumber($subject)
47
    {
48 1
        if (is_scalar($subject)) {  // todo: test this
49
            return $subject;
50
        }
51
52 1
        if (is_array($subject)) {
53
            return $this->slumberArray($subject);
54
        }
55
56 1
        if (empty($subject)) {
57
            return null;
58
        }
59
60
        // put to sleep
61 1
        return (array) $this->slumberer->slumber($subject);
62
    }
63
64
    /**
65
     * @param mixed[] $subjects
66
     *
67
     * @return array
68
     */
69
    public function slumberArray(array $subjects)
70
    {
71
        $result = [];
72
73
        /**
74
         * @var string|int $key
75
         * @var mixed      $subject
76
         */
77
        foreach ($subjects as $key => $subject) {
78
            $result[$key] = $this->slumber($subject);
79
        }
80
81
        return $result;
82
    }
83
84
    /**
85
     * @param mixed                   $data
86
     * @param string|\ReflectionClass $cls
87
     *
88
     * @return mixed|null
89
     */
90
    public function awake($data, $cls)
91
    {
92
        if ($data === null || ! is_array($data)) {
93
            return null;
94
        }
95
96
        if (! $cls instanceof \ReflectionClass) {
97
            $cls = new \ReflectionClass($cls);
98
        }
99
100
        return $this->awaker->awake($data, $cls);
101
    }
102
103
    /**
104
     * @param array                   $data
105
     * @param string|\ReflectionClass $cls
106
     *
107
     * @return array
108
     */
109
    public function awakeList($data, $cls)
110
    {
111
        if (! is_array($data) && ! $data instanceof \Traversable) {
112
            return [];
113
        }
114
115
        if (! $cls instanceof \ReflectionClass) {
116
            $cls = new \ReflectionClass($cls);
117
        }
118
119
        $result = [];
120
121
        foreach ($data as $k => $v) {
122
            $result[$k] = $this->awake($v, $cls);
123
        }
124
125
        return $result;
126
    }
127
}
128