ResourceContract   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 16
eloc 34
c 1
b 0
f 0
dl 0
loc 90
ccs 40
cts 40
cp 1
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getValue() 0 6 3
A arrayIndexToCamelCase() 0 8 2
A toArray() 0 8 2
A __construct() 0 8 2
A bySetMethod() 0 19 5
A byDinamicallyAttribute() 0 5 2
1
<?php
2
3
4
namespace Hell\Vephar\Contracts;
5
6
use Hell\Vephar\Response;
7
8
abstract class ResourceContract
9
{
10
    /**
11
     * ResourceContract constructor.
12
     * @param $data
13
     * @param bool $setters
14
     */
15 21
    public function __construct($data, $setters = true)
16
    {
17 21
        if ($setters) {
18 9
            $this->bySetMethod($data);
19 9
            return;
20
        }
21 21
        $this->byDinamicallyAttribute($data);
22 21
        return;
23
    }
24
25
    /**
26
     * @param $data
27
     * @return ResourceContract
28
     */
29 9
    public function bySetMethod($data)
30
    {
31 9
        $data = $this->arrayIndexToCamelCase($data);
32 9
        $object = $this;
33 9
        $methods = get_class_methods($object);
34 9
        foreach ($methods as $method) {
35 9
            preg_match(' /^(set)(.*?)$/i', $method, $results);
36 9
            $setMethod = $results[1] ?? '';
37 9
            $attritbuteName = toCamelCase($results[2] ?? '');
38 9
            if ($setMethod == 'set' && array_key_exists($attritbuteName, $data)) {
39 9
                $object->$method($this->getValue($data[$attritbuteName]));
40
            }
41
        }
42
43 9
        if (method_exists($object, "setCustomAttributes")) {
44 3
            $object->setCustomAttributes(new $this($data, false));
45
        }
46
47 9
        return $object;
48
    }
49
50
    /**
51
     * @param $data
52
     * @return array
53
     */
54 9
    protected function arrayIndexToCamelCase($data = [])
55
    {
56 9
        $newData = [];
57 9
        foreach ($data as $attribute => $value) {
58 9
            $attributeName = toCamelCase($attribute);
59 9
            $newData[$attributeName] = $value;
60
        }
61 9
        return $newData;
62
    }
63
64
    /**
65
     * @param $data
66
     */
67 21
    protected function byDinamicallyAttribute($data)
68
    {
69 21
        foreach ($data as $attribute => $value) {
70 21
            $attributeName = toCamelCase($attribute);
71 21
            $this->{$attributeName} = $this->getValue($value);
72
        }
73 21
    }
74
75
    /**
76
     * @param $value
77
     * @return mixed
78
     */
79 21
    protected function getValue($value)
80
    {
81 21
        if (!is_array($value) || !is_object_array($value)) {
82 21
            return $value;
83
        }
84 21
        return Response::resource($value);
85
    }
86
87
    /**
88
     * @return array
89
     */
90 3
    public function toArray(): array
91
    {
92 3
        $attributes = get_object_vars($this);
93 3
        $newAttributes = [];
94 3
        foreach ($attributes as $key => $value) {
95 3
            $newAttributes[toSnakeCase($key)] = $value;
96
        }
97 3
        return $newAttributes;
98
    }
99
}
100