FieldModel::boot()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
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://pyrocms.com/
16
 * @author  PyroCMS, Inc. <[email protected]>
17
 * @author  Ryan Thompson <[email protected]>
18
 */
19
class FieldModel extends EloquentModel implements FieldInterface
20
{
21
22
    /**
23
     * Eager loaded relations.
24
     *
25
     * @var array
26
     */
27
    protected $with = [
28
        'translations',
29
    ];
30
31
    /**
32
     * Do not use timestamps.
33
     *
34
     * @var bool
35
     */
36
    public $timestamps = false;
37
38
    /**
39
     * Default attributes.
40
     *
41
     * @var array
42
     */
43
    protected $attributes = [
44
        'config' => 'a:0:{}',
45
    ];
46
47
    /**
48
     * The cache minutes.
49
     *
50
     * @var int
51
     */
52
    protected $cacheMinutes = 99999;
53
54
    /**
55
     * The foreign key for translations.
56
     *
57
     * @var string
58
     */
59
    protected $translationForeignKey = 'field_id';
60
61
    /**
62
     * Translatable attributes.
63
     *
64
     * @var array
65
     */
66
    protected $translatedAttributes = [
67
        'name',
68
        'warning',
69
        'placeholder',
70
        'instructions',
71
    ];
72
73
    /**
74
     * The translation model.
75
     *
76
     * @var string
77
     */
78
    protected $translationModel = 'Anomaly\Streams\Platform\Field\FieldModelTranslation';
79
80
    /**
81
     * The database table name.
82
     *
83
     * @var string
84
     */
85
    protected $table = 'streams_fields';
86
87
    /**
88
     * The field type builder.
89
     *
90
     * @var FieldTypeBuilder
91
     */
92
    protected static $builder;
93
94
    /**
95
     * Boot the model.
96
     */
97
    protected static function boot()
98
    {
99
        self::$builder = app(FieldTypeBuilder::class);
100
101
        parent::boot();
102
    }
103
104
    /**
105
     * Get the ID.
106
     *
107
     * @return mixed
108
     */
109
    public function getId()
110
    {
111
        return $this->getKey();
112
    }
113
114
    /**
115
     * Get the name.
116
     *
117
     * @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...
118
     * @return string
119
     */
120
    public function getName()
121
    {
122
        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...
123
    }
124
125
    /**
126
     * Get the warning.
127
     *
128
     * @return string
129
     */
130
    public function getWarning()
131
    {
132
        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...
133
    }
134
135
    /**
136
     * Get the instructions.
137
     *
138
     * @return string
139
     */
140
    public function getInstructions()
141
    {
142
        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...
143
    }
144
145
    /**
146
     * Get the placeholder.
147
     *
148
     * @return string
149
     */
150
    public function getPlaceholder()
151
    {
152
        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...
153
    }
154
155
    /**
156
     * Get the slug.
157
     *
158
     * @return mixed
159
     */
160
    public function getSlug()
161
    {
162
        return $this->getAttributeFromArray('slug');
163
    }
164
165
    /**
166
     * Get the stream.
167
     *
168
     * @return string
169
     */
170
    public function getStream()
171
    {
172
        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...
173
    }
174
175
    /**
176
     * Get the namespace.
177
     *
178
     * @return string
179
     */
180
    public function getNamespace()
181
    {
182
        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...
183
    }
184
185
    /**
186
     * Get the field type.
187
     *
188
     * @param  bool $fresh
189
     * @return FieldType|null
190
     * @throws \Exception
191
     */
192
    public function getType($fresh = false)
193
    {
194
        if ($fresh === false && isset($this->cache['type'])) {
195
            return $this->cache['type'];
196
        }
197
198
        $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...
199
        $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...
200
        $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...
201
        $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...
202
203
        if (!$type) {
204
            return $this->cache['type'] = null;
205
        }
206
207
        return $this->cache['type'] = self::$builder->build(compact('type', 'field', 'label', 'config'));
208
    }
209
210
    /**
211
     * Get the field type value.
212
     *
213
     * @return string
214
     */
215
    public function getTypeValue()
216
    {
217
        return $this->getAttributeFromArray('type');
218
    }
219
220
    /**
221
     * Get the configuration.
222
     *
223
     * @return mixed
224
     */
225
    public function getConfig()
226
    {
227
        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...
228
    }
229
230
    /**
231
     * Get the related assignments.
232
     *
233
     * @return AssignmentCollection
234
     */
235
    public function getAssignments()
236
    {
237
        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...
238
    }
239
240
    /**
241
     * Return whether the field
242
     * has assignments or not.
243
     *
244
     * @return bool
245
     */
246
    public function hasAssignments()
247
    {
248
        $assignments = $this->getAssignments();
249
250
        return !$assignments->isEmpty();
251
    }
252
253
    /**
254
     * Return whether the field is
255
     * a relationship or not.
256
     *
257
     * @return bool
258
     */
259
    public function isRelationship()
260
    {
261
        return method_exists($this->getType(), 'getRelation');
262
    }
263
264
    /**
265
     * Get the locked flag.
266
     *
267
     * @return mixed
268
     */
269
    public function isLocked()
270
    {
271
        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...
272
    }
273
274
    /**
275
     * Set config attribute.
276
     *
277
     * @param array $config
278
     */
279
    public function setConfigAttribute($config)
280
    {
281
        $this->attributes['config'] = serialize((array)$config);
282
    }
283
284
    /**
285
     * Return the decoded config attribute.
286
     *
287
     * @param  $config
288
     * @return mixed
289
     */
290
    public function getConfigAttribute($config)
291
    {
292
        return (array)unserialize($config);
293
    }
294
295
    /**
296
     * Set rules attribute.
297
     *
298
     * @param array $rules
299
     */
300
    public function setRulesAttribute($rules)
301
    {
302
        $this->attributes['rules'] = serialize((array)$rules);
303
    }
304
305
    /**
306
     * Return the decoded rules attribute.
307
     *
308
     * @param  $rules
309
     * @return mixed
310
     */
311
    public function getRulesAttribute($rules)
312
    {
313
        return (array)unserialize($rules);
314
    }
315
316
    /**
317
     * Set the stream namespace.
318
     *
319
     * @param StreamInterface $stream
320
     */
321
    public function setStreamAttribute(StreamInterface $stream)
322
    {
323
        $this->attributes['namespace'] = $stream->getNamespace();
324
    }
325
326
    /**
327
     * Compile the fields's stream.
328
     *
329
     * @return FieldInterface
330
     */
331
    public function compileStreams()
332
    {
333
        /* @var AssignmentInterface $assignment */
334
        foreach ($this->getAssignments() as $assignment) {
335
            $assignment->compileStream();
336
        }
337
338
        return $this;
339
    }
340
341
    /**
342
     * Return the assignments relation.
343
     *
344
     * @return HasMany
345
     */
346
    public function assignments()
347
    {
348
        return $this->hasMany('Anomaly\Streams\Platform\Assignment\AssignmentModel', 'field_id')->orderBy('sort_order');
349
    }
350
}
351