Passed
Pull Request — master (#2)
by Wojciech
02:34
created

FlexEntity::set()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 10
cc 1
nc 1
nop 2
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 12
    public static function make(array $components): Makeable
27
    {
28 12
        $entity = new static();
29
30 12
        foreach ($components as $k => $v) {
31 12
            $entity->{'set' . ChangeCase::snakeToCamel($k)}($v);
32
        }
33 12
        return $entity;
34
    }
35
36 9
    public function toArray(): array
37
    {
38 9
        return ArrayableHelper::processArray($this->data);
39
    }
40
41 6
    public function toJson(int $options = 0): string
42
    {
43 6
        return json_encode($this->toArray(), $options);
44
    }
45
46 3
    public function __toString(): string
47
    {
48 3
        return $this->toJson();
49
    }
50
51 18
    public function __call(string $name, array $arguments)
52
    {
53 18
        $action = substr($name, 0, 3);
54 18
        $property = ChangeCase::camelToSnake(substr($name, 3));
55
        switch ($action) {
56 18
            case 'set':
57 15
                return $this->set($property, $arguments);
58 9
            case 'get':
59 8
                return $this->get($property);
60 3
            case 'add':
61 3
                return $this->add($property, $arguments);
62
        }
63
        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 15
    private function set(string $property, array $arguments)
72
    {
73 15
        $this->checkArguments($arguments, $property);
74 15
        $this->data[$property] = $arguments[0];
75
76 15
        return $this;
77
    }
78
79 8
    private function get(string $property)
80
    {
81 8
        if (array_key_exists($property, $this->data)) {
82 8
            return $this->data[$property];
83
        }
84
85
        return null;
86
    }
87
88 3
    private function add(string $name, array $arguments)
89
    {
90 3
        if (!isset($this->data[$name]) || is_array($this->data[$name])) {
91 3
            $this->data[$name][] = $arguments[0];
92
93 3
            return $this;
94
        }
95
96
        throw new InvalidArgumentException('Expected property [' . $name . '] to be array type');
97
    }
98
99 15
    private function checkArguments(array $arguments, $property): void
100
    {
101 15
        if (count($arguments) == 0) {
102
            throw new \RuntimeException('Missing argument on method ' . __CLASS__ . '::set_' . $property . '() call');
103
        }
104 15
    }
105
}
106