Completed
Push — master ( 156e79...007c06 )
by Pascal
02:10
created

Model::makeCollection()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 16
rs 8.8571
cc 5
eloc 8
nc 3
nop 2
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
    public function __construct($attributes = null)
24
    {
25
        if (!is_null($attributes)) {
26
            $this->fill($attributes);
27
        }
28
    }
29
30
    /**
31
     * Fill the attributes
32
     * @param  string|array $attributes
33
     * @return void
34
     */
35
    private function fill($attributes)
36
    {
37
        // json
38
        if (is_string($attributes)) {
39
            $attributes = json_decode($attributes, true);
40
        }
41
42
        // check if attributes are valid
43
        if (!is_array($attributes)) {
44
            throw new \InvalidArgumentException('Attributes must be of type array or a valid json string');
45
        }
46
47
        foreach ($attributes as $key => $value) {
48
            $this->setAttribute($key, $value);
49
        }
50
    }
51
52
    /**
53
     * Set a given attribute on the model.
54
     * @param  string $key
55
     * @param  mixed  $value
56
     * @return $this
57
     */
58 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
        if ($this->hasSetMutator($key)) {
64
            $method = 'set' . studly_case($key) . 'Attribute';
65
        
66
            return $this->{$method}($value);
67
        }
68
69
        $this->attributes[$key] = $value;
70
71
        return $this;
72
    }
73
74
    /**
75
     * Determine if a set mutator exists for an attribute.
76
     * @param  string $key
77
     * @return bool
78
     */
79
    public function hasSetMutator($key)
80
    {
81
        return method_exists($this, 'set' . studly_case($key) . 'Attribute');
82
    }
83
84
    /**
85
     * @param string $name
86
     * @return mixed
87
     */
88
    public function __get($name)
89
    {
90
        return $this->getAttribute($name);
91
    }
92
93
    /**
94
     * @param string $name
95
     * @param mixed  $value
96
     */
97
    public function __set($name, $value)
98
    {
99
        $this->setAttribute($name, $value);
100
    }
101
102
    /**
103
     * Get an attribute from the $attributes array.
104
     * @param  string $key
105
     * @return mixed
106
     */
107 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
        $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
        if ($this->hasGetMutator($key)) {
114
            $method = 'get' . studly_case($key) . 'Attribute';
115
        
116
            return $this->{$method}($value);
117
        }
118
119
        return $value;
120
    }
121
    
122
    /**
123
     * Get an attribute from the $attributes array.
124
     * @param  string $key
125
     * @return mixed
126
     */
127
    protected function getAttributeValue($key)
128
    {
129
        if (array_key_exists($key, $this->attributes)) {
130
            return $this->attributes[$key];
131
        }
132
133
        return null;
134
    }
135
136
    /**
137
     * Determine if a get mutator exists for an attribute.
138
     * @param  string $key
139
     * @return bool
140
     */
141
    public function hasGetMutator($key)
142
    {
143
        return method_exists($this, 'get' . studly_case($key) . 'Attribute');
144
    }
145
    
146
    /**
147
     * @param $key
148
     * @return bool
149
     */
150
    public function __isset($key)
151
    {
152
        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
    protected function makeCollection(array $values, $class = null)
162
    {
163
        $collection = new Collection($values);
164
    
165
        if (!is_null($class) && class_exists($class)) {
166
            $model = new $class();
167
        
168
            if ($model instanceof Model) {
169
                foreach ($collection as $key => $item) {
170
                    $collection[$key] = $model->newInstance($item);
171
                }
172
            }
173
        }
174
    
175
        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
    public function newInstance($attributes = [])
185
    {
186
        return new static($attributes);
187
    }
188
}
189