Completed
Push — master ( 656525...3dc553 )
by Pascal
02:40
created

Model::makeCollection()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 5
Metric Value
dl 0
loc 16
ccs 11
cts 11
cp 1
rs 8.8571
cc 5
eloc 8
nc 3
nop 2
crap 5
1
<?php namespace Atog\Api;
2
3
use Illuminate\Support\Collection;
4
use JsonSerializable;
5
6
/**
7
 * Class Model
8
 * @package Atog\Api
9
 */
10
class Model implements JsonSerializable
11
{
12
    use Jsonable;
13
14
    /**
15
     * @var array
16
     */
17
    protected $attributes = [];
18
19
    /**
20
     * Model constructor.
21
     * @param null $attributes
22
     */
23 28
    public function __construct($attributes = null)
24
    {
25 28
        if (!is_null($attributes)) {
26 17
            $this->fill($attributes);
27 16
        }
28 27
    }
29
30
    /**
31
     * Fill the attributes
32
     * @param  string|array $attributes
33
     * @return void
34
     */
35 17
    private function fill($attributes)
36
    {
37
        // json
38 17
        if (is_string($attributes)) {
39 6
            $attributes = json_decode($attributes, true);
40 6
        }
41
42
        // check if attributes are valid
43 17
        if (!is_array($attributes)) {
44 1
            throw new \InvalidArgumentException('Attributes must be of type array or a valid json string');
45
        }
46
47 16
        foreach ($attributes as $key => $value) {
48 16
            $this->setAttribute($key, $value);
49 16
        }
50 16
    }
51
52
    /**
53
     * Set a given attribute on the model.
54
     * @param  string $key
55
     * @param  mixed  $value
56
     * @return $this
57
     */
58 19 View Code Duplication
    public function setAttribute($key, $value)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
59
    {
60
        // First we will check for the presence of a mutator for the set operation
61
        // which simply lets the developers tweak the attribute as it is set on
62
        // the model, such as "json_encoding" an listing of data for storage.
63 19
        if ($this->hasSetMutator($key)) {
64 1
            $method = 'set' . studly_case($key) . 'Attribute';
65
        
66 1
            return $this->{$method}($value);
67
        }
68
69 18
        $this->attributes[$key] = $value;
70
71 18
        return $this;
72
    }
73
74
    /**
75
     * Determine if a set mutator exists for an attribute.
76
     * @param  string $key
77
     * @return bool
78
     */
79 19
    public function hasSetMutator($key)
80
    {
81 19
        return method_exists($this, 'set' . studly_case($key) . 'Attribute');
82
    }
83
84
    /**
85
     * @param string $name
86
     * @return mixed
87
     */
88 3
    public function __get($name)
89
    {
90 3
        return $this->getAttribute($name);
91
    }
92
93
    /**
94
     * @param string $name
95
     * @param mixed  $value
96
     */
97 1
    public function __set($name, $value)
98
    {
99 1
        $this->setAttribute($name, $value);
100 1
    }
101
102
    /**
103
     * Get an attribute from the $attributes array.
104
     * @param  string $key
105
     * @return mixed
106
     */
107 6 View Code Duplication
    public function getAttribute($key)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
108
    {
109 6
        $value = $this->getAttributeValue($key);
110
        
111
        // First we will check for the presence of a mutator for the set operation
112
        // which simply lets the developers tweak the attribute as it is set.
113 6
        if ($this->hasGetMutator($key)) {
114 1
            $method = 'get' . studly_case($key) . 'Attribute';
115
        
116 1
            return $this->{$method}($value);
117
        }
118
119 5
        return $value;
120
    }
121
    
122
    /**
123
     * Get an attribute from the $attributes array.
124
     * @param  string $key
125
     * @return mixed
126
     */
127 5
    protected function getAttributeValue($key)
128
    {
129 5
        if (array_key_exists($key, $this->attributes)) {
130 3
            return $this->attributes[$key];
131
        }
132
133 2
        return null;
134
    }
135
136
    /**
137
     * Determine if a get mutator exists for an attribute.
138
     * @param  string $key
139
     * @return bool
140
     */
141 6
    public function hasGetMutator($key)
142
    {
143 6
        return method_exists($this, 'get' . studly_case($key) . 'Attribute');
144
    }
145
    
146
    /**
147
     * @param $key
148
     * @return bool
149
     */
150 1
    public function __isset($key)
151
    {
152 1
        return array_key_exists($key, $this->attributes);
153
    }
154
155
    /**
156
     * Transform an array of values into a collection of models
157
     * @param array $values
158
     * @param null  $class
159
     * @return \Illuminate\Support\Collection
160
     */
161 1
    public function makeCollection(array $values, $class = null)
162
    {
163 1
        $collection = new Collection($values);
164
    
165 1
        if (!is_null($class) && class_exists($class)) {
166 1
            $model = new $class();
167
        
168 1
            if ($model instanceof Model) {
169 1
                foreach ($collection as $key => $item) {
170 1
                    $collection[$key] = $model->newInstance($item);
171 1
                }
172 1
            }
173 1
        }
174
    
175 1
        return $collection;
176
    }
177
178
    /**
179
     * Create a new model instance
180
     * @param array $attributes
181
     * @return \Atog\Api\Model
182
     * @throws \InvalidArgumentException if attributes is not an array or a json object
183
     */
184 4
    public function newInstance($attributes = [])
185
    {
186 4
        return new static($attributes);
187
    }
188
}
189