Completed
Push — master ( 8e0025...04518a )
by
05:55
created

Hydration::hydrate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of the PhpMob package.
5
 *
6
 * (c) Ishmael Doss <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace PhpMob\Omise\Hydrator;
13
14
use PhpMob\Omise\Model;
15
16
/**
17
 * @author Ishmael Doss <[email protected]>
18
 */
19
class Hydration implements HydrationInterface
20
{
21
    /**
22
     * TODO: improve me
23
     * @param array $data
24
     *
25
     * @return array
26
     */
27
    private function doHydrate(array &$data)
28
    {
29
        foreach ($data as $key => $value) {
30
            if (is_array($value)) {
31
                $data[$key] = self::doHydrate($data[$key]);
32
            }
33
        }
34
35
        if (empty($data) || empty($data['object'])) {
36
            return $data;
37
        }
38
39
        $domain = $this->makeDomainClass($data['object']);
40
41
        return new $domain($data);
42
    }
43
44
    /**
45
     * @param string $rawData
46
     *
47
     * @return Model|mixed
48
     */
49
    public function hydrate($rawData)
50
    {
51
        $data = json_decode($rawData, true);
52
53
        return $this->doHydrate($data);
54
    }
55
56
    /**
57
     * @param $className
58
     *
59
     * @return string
60
     */
61
    protected function makeDomainClass($className)
62
    {
63
        return "PhpMob\\Omise\\Domain\\".ucfirst($className === 'list' ? 'Pagination' : $className);
64
    }
65
}
66