Resource::fill()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
c 0
b 0
f 0
rs 9.9332
nc 3
cc 3
nop 0
1
<?php
2
3
namespace TestMonitor\ActiveCampaign\Resources;
4
5
class Resource
6
{
7
    /**
8
     * The resource attributes.
9
     *
10
     * @var array
11
     */
12
    public $attributes;
13
14
    /**
15
     * The ActiveCampaign SDK instance.
16
     *
17
     * @var \TestMonitor\ActiveCampaign\ActiveCampaign
18
     */
19
    protected $activeCampaign;
20
21
    /**
22
     * Create a new resource instance.
23
     *
24
     * @param  array $attributes
25
     * @param  \TestMonitor\ActiveCampaign\ActiveCampaign $activeCampaign
26
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
27
     */
28
    public function __construct(array $attributes, $activeCampaign = null)
29
    {
30
        $this->attributes = $attributes;
31
        $this->activeCampaign = $activeCampaign;
32
33
        $this->fill();
34
    }
35
36
    /**
37
     * Fill the resource with the array of attributes.
38
     *
39
     * @return void
40
     */
41
    private function fill()
42
    {
43
        foreach ($this->attributes as $key => $value) {
44
            $key = $this->camelCase($key);
45
46
            if (property_exists($this, $key)) {
47
                $this->{$key} = $value;
48
            }
49
        }
50
    }
51
52
    /**
53
     * Convert the key name to camel case.
54
     *
55
     * @param $key
56
     *
57
     * @return mixed
58
     */
59
    private function camelCase($key)
60
    {
61
        $parts = explode('_', $key);
62
63
        foreach ($parts as $i => $part) {
64
            if ($i !== 0) {
65
                $parts[$i] = ucfirst($part);
66
            }
67
        }
68
69
        return str_replace(' ', '', implode(' ', $parts));
70
    }
71
}
72