Passed
Pull Request — master (#3)
by Wojciech
02:51
created

FlexEntity::toArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
/*
3
 * This file is part of the Bushido\Foundation package.
4
 *
5
 * (c) Wojciech Nowicki <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE.md
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Bushido\Foundation\SmartEntity;
12
13
use Bushido\Foundation\Contracts\Makeable;
14
use Bushido\Foundation\Exceptions\InvalidArgumentException;
15
use Bushido\Foundation\Helpers\ArrayableHelper;
16
use Bushido\Foundation\Helpers\ChangeCase;
17
18
class FlexEntity implements Entity
19
{
20
    private $data = [];
21
22
    /**
23
     * @param array $components
24
     * @return static
25
     */
26 21
    public static function make(array $components): Makeable
27
    {
28 21
        $entity = new static();
29
30 21
        foreach ($components as $k => $v) {
31 18
            $entity->{'set' . ChangeCase::snakeToCamel($k)}($v);
32
        }
33 21
        return $entity;
34
    }
35
36 15
    public function toArray(): array
37
    {
38 15
        return ArrayableHelper::processArray($this->data);
39
    }
40
41 6
    public function toJson(int $options = 0): string
42
    {
43 6
        return (string) json_encode($this->toArray(), $options);
44
    }
45
46 3
    public function __toString(): string
47
    {
48 3
        return $this->toJson();
49
    }
50
51 36
    public function __call(string $name, array $arguments)
52
    {
53 36
        $action = substr($name, 0, 3);
54 36
        $property = ChangeCase::camelToSnake(substr($name, 3));
55 24
        switch ($action) {
56 36
            case 'set':
57 27
                return $this->set($property, $arguments);
58 21
            case 'get':
59 15
                return $this->get($property);
60 9
            case 'add':
61 6
                return $this->add($property, $arguments);
62
        }
63 3
        throw new \RuntimeException('Call to undefined method '.__CLASS__.'::'.$name.'()');
64
    }
65
66
    /**
67
     * @param string $property
68
     * @param array $arguments
69
     * @return $this
70
     */
71 27
    protected function set(string $property, array $arguments)
72
    {
73 27
        $this->data[$property] = $this->fetchValue($arguments, $property);
74
75 24
        return $this;
76
    }
77
78 15
    private function get(string $property)
79
    {
80 15
        if (array_key_exists($property, $this->data)) {
81 12
            return $this->data[$property];
82
        }
83
84 3
        return null;
85
    }
86
87 6
    private function add(string $name, array $arguments)
88
    {
89 6
        if (!isset($this->data[$name]) || is_array($this->data[$name])) {
90 3
            $this->data[$name][] = $arguments[0];
91
92 3
            return $this;
93
        }
94
95 3
        throw new InvalidArgumentException('Expected property [' . $name . '] to be array type');
96
    }
97
98 27
    protected function fetchValue(array $arguments, $propertyName)
99
    {
100 27
        if (count($arguments) == 0) {
101 3
            throw new \RuntimeException('Missing argument on ' . __CLASS__ . '::set_' . $propertyName . '() call');
102
        }
103
104 24
        return $arguments[0];
105
    }
106
}
107