Completed
Push — master ( 3ab678...c518dd )
by Ryan
07:33
created

FieldModel::getRulesAttribute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php namespace Anomaly\Streams\Platform\Field;
2
3
use Anomaly\Streams\Platform\Addon\FieldType\FieldType;
4
use Anomaly\Streams\Platform\Addon\FieldType\FieldTypeBuilder;
5
use Anomaly\Streams\Platform\Assignment\AssignmentCollection;
6
use Anomaly\Streams\Platform\Assignment\Contract\AssignmentInterface;
7
use Anomaly\Streams\Platform\Field\Contract\FieldInterface;
8
use Anomaly\Streams\Platform\Model\EloquentModel;
9
use Anomaly\Streams\Platform\Stream\Contract\StreamInterface;
10
use Illuminate\Database\Eloquent\Relations\HasMany;
11
12
/**
13
 * Class FieldModel
14
 *
15
 * @link    http://anomaly.is/streams-platform
16
 * @author  AnomalyLabs, Inc. <[email protected]>
17
 * @author  Ryan Thompson <[email protected]>
18
 * @package Anomaly\Streams\Platform\Field
19
 */
20
class FieldModel extends EloquentModel implements FieldInterface
21
{
22
23
    /**
24
     * Do not use timestamps.
25
     *
26
     * @var bool
27
     */
28
    public $timestamps = false;
29
30
    /**
31
     * Default attributes.
32
     *
33
     * @var array
34
     */
35
    protected $attributes = [
36
        'config' => 'a:0:{}'
37
    ];
38
39
    /**
40
     * The cache minutes.
41
     *
42
     * @var int
43
     */
44
    protected $cacheMinutes = 99999;
45
46
    /**
47
     * The foreign key for translations.
48
     *
49
     * @var string
50
     */
51
    protected $translationForeignKey = 'field_id';
52
53
    /**
54
     * Translatable attributes.
55
     *
56
     * @var array
57
     */
58
    protected $translatedAttributes = [
59
        'name',
60
        'warning',
61
        'placeholder',
62
        'instructions'
63
    ];
64
65
    /**
66
     * The translation model.
67
     *
68
     * @var string
69
     */
70
    protected $translationModel = 'Anomaly\Streams\Platform\Field\FieldModelTranslation';
71
72
    /**
73
     * The database table name.
74
     *
75
     * @var string
76
     */
77
    protected $table = 'streams_fields';
78
79
    /**
80
     * The field type builder.
81
     *
82
     * @var FieldTypeBuilder
83
     */
84
    protected static $builder;
85
86
    /**
87
     * Boot the model.
88
     */
89
    protected static function boot()
90
    {
91
        self::$builder = app(FieldTypeBuilder::class);
92
93
        parent::boot();
94
    }
95
96
    /**
97
     * Get the ID.
98
     *
99
     * @return mixed
100
     */
101
    public function getId()
102
    {
103
        return $this->getKey();
104
    }
105
106
    /**
107
     * Get the name.
108
     *
109
     * @param null|string $locale
0 ignored issues
show
Bug introduced by
There is no parameter named $locale. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
110
     * @return string
111
     */
112
    public function getName()
113
    {
114
        return $this->name;
0 ignored issues
show
Documentation introduced by
The property name does not exist on object<Anomaly\Streams\Platform\Field\FieldModel>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
115
    }
116
117
    /**
118
     * Get the warning.
119
     *
120
     * @return string
121
     */
122
    public function getWarning()
123
    {
124
        return $this->warning;
0 ignored issues
show
Documentation introduced by
The property warning does not exist on object<Anomaly\Streams\Platform\Field\FieldModel>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
125
    }
126
127
    /**
128
     * Get the instructions.
129
     *
130
     * @return string
131
     */
132
    public function getInstructions()
133
    {
134
        return $this->instructions;
0 ignored issues
show
Documentation introduced by
The property instructions does not exist on object<Anomaly\Streams\Platform\Field\FieldModel>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
135
    }
136
137
    /**
138
     * Get the placeholder.
139
     *
140
     * @return string
141
     */
142
    public function getPlaceholder()
143
    {
144
        return $this->placeholder;
0 ignored issues
show
Documentation introduced by
The property placeholder does not exist on object<Anomaly\Streams\Platform\Field\FieldModel>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
145
    }
146
147
    /**
148
     * Get the slug.
149
     *
150
     * @return mixed
151
     */
152
    public function getSlug()
153
    {
154
        return $this->getAttributeFromArray('slug');
155
    }
156
157
    /**
158
     * Get the stream.
159
     *
160
     * @return string
161
     */
162
    public function getStream()
163
    {
164
        return $this->stream;
0 ignored issues
show
Documentation introduced by
The property stream does not exist on object<Anomaly\Streams\Platform\Field\FieldModel>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
165
    }
166
167
    /**
168
     * Get the namespace.
169
     *
170
     * @return string
171
     */
172
    public function getNamespace()
173
    {
174
        return $this->namespace;
0 ignored issues
show
Documentation introduced by
The property namespace does not exist on object<Anomaly\Streams\Platform\Field\FieldModel>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
175
    }
176
177
    /**
178
     * Get the field type.
179
     *
180
     * @param bool $fresh
181
     * @return FieldType|null
182
     * @throws \Exception
183
     */
184
    public function getType($fresh = false)
185
    {
186
        if ($fresh === false && isset($this->cache['type'])) {
187
            return $this->cache['type'];
188
        }
189
190
        $type   = $this->type;
0 ignored issues
show
Documentation introduced by
The property type does not exist on object<Anomaly\Streams\Platform\Field\FieldModel>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
191
        $field  = $this->slug;
0 ignored issues
show
Documentation introduced by
The property slug does not exist on object<Anomaly\Streams\Platform\Field\FieldModel>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
192
        $label  = $this->name;
0 ignored issues
show
Documentation introduced by
The property name does not exist on object<Anomaly\Streams\Platform\Field\FieldModel>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
193
        $config = $this->config;
0 ignored issues
show
Documentation introduced by
The property config does not exist on object<Anomaly\Streams\Platform\Field\FieldModel>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
194
195
        if (!$type) {
196
            return $this->cache['type'] = null;
197
        }
198
199
        return $this->cache['type'] = self::$builder->build(compact('type', 'field', 'label', 'config'));
200
    }
201
202
    /**
203
     * Get the field type value.
204
     *
205
     * @return string
206
     */
207
    public function getTypeValue()
208
    {
209
        return $this->getAttributeFromArray('type');
210
    }
211
212
    /**
213
     * Get the configuration.
214
     *
215
     * @return mixed
216
     */
217
    public function getConfig()
218
    {
219
        return $this->config;
0 ignored issues
show
Documentation introduced by
The property config does not exist on object<Anomaly\Streams\Platform\Field\FieldModel>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
220
    }
221
222
    /**
223
     * Get the related assignments.
224
     *
225
     * @return AssignmentCollection
226
     */
227
    public function getAssignments()
228
    {
229
        return $this->assignments;
0 ignored issues
show
Documentation introduced by
The property assignments does not exist on object<Anomaly\Streams\Platform\Field\FieldModel>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
230
    }
231
232
    /**
233
     * Return whether the field
234
     * has assignments or not.
235
     *
236
     * @return bool
237
     */
238
    public function hasAssignments()
239
    {
240
        $assignments = $this->getAssignments();
241
242
        return !$assignments->isEmpty();
243
    }
244
245
    /**
246
     * Return whether the field is
247
     * a relationship or not.
248
     *
249
     * @return bool
250
     */
251
    public function isRelationship()
252
    {
253
        return method_exists($this->getType(), 'getRelation');
254
    }
255
256
    /**
257
     * Get the locked flag.
258
     *
259
     * @return mixed
260
     */
261
    public function isLocked()
262
    {
263
        return ($this->locked);
0 ignored issues
show
Documentation introduced by
The property locked does not exist on object<Anomaly\Streams\Platform\Field\FieldModel>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
264
    }
265
266
    /**
267
     * Set config attribute.
268
     *
269
     * @param array $config
270
     */
271
    public function setConfigAttribute($config)
272
    {
273
        $this->attributes['config'] = serialize((array)$config);
274
    }
275
276
    /**
277
     * Return the decoded config attribute.
278
     *
279
     * @param  $config
280
     * @return mixed
281
     */
282
    public function getConfigAttribute($config)
283
    {
284
        return (array)unserialize($config);
285
    }
286
287
    /**
288
     * Set rules attribute.
289
     *
290
     * @param array $rules
291
     */
292
    public function setRulesAttribute($rules)
293
    {
294
        $this->attributes['rules'] = serialize((array)$rules);
295
    }
296
297
    /**
298
     * Return the decoded rules attribute.
299
     *
300
     * @param  $rules
301
     * @return mixed
302
     */
303
    public function getRulesAttribute($rules)
304
    {
305
        return (array)unserialize($rules);
306
    }
307
308
    /**
309
     * Set the stream namespace.
310
     *
311
     * @param StreamInterface $stream
312
     */
313
    public function setStreamAttribute(StreamInterface $stream)
314
    {
315
        $this->attributes['namespace'] = $stream->getNamespace();
316
    }
317
318
    /**
319
     * Compile the fields's stream.
320
     *
321
     * @return FieldInterface
322
     */
323
    public function compileStreams()
324
    {
325
        /* @var AssignmentInterface $assignment */
326
        foreach ($this->getAssignments() as $assignment) {
327
            $assignment->compileStream();
328
        }
329
330
        return $this;
331
    }
332
333
    /**
334
     * Return the assignments relation.
335
     *
336
     * @return HasMany
337
     */
338
    public function assignments()
339
    {
340
        return $this->hasMany('Anomaly\Streams\Platform\Assignment\AssignmentModel', 'field_id')->orderBy('sort_order');
341
    }
342
}
343