AssignmentModel::getFieldRules()   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\Assignment;
2
3
use Anomaly\Streams\Platform\Addon\FieldType\FieldType;
4
use Anomaly\Streams\Platform\Assignment\Contract\AssignmentInterface;
5
use Anomaly\Streams\Platform\Field\Contract\FieldInterface;
6
use Anomaly\Streams\Platform\Model\EloquentModel;
7
use Anomaly\Streams\Platform\Stream\Contract\StreamInterface;
8
use Robbo\Presenter\PresentableInterface;
9
10
/**
11
 * Class AssignmentModel
12
 *
13
 * @link    http://pyrocms.com/
14
 * @author  PyroCMS, Inc. <[email protected]>
15
 * @author  Ryan Thompson <[email protected]>
16
 */
17
class AssignmentModel extends EloquentModel implements AssignmentInterface, PresentableInterface
18
{
19
20
    /**
21
     * Do not use timestamps.
22
     *
23
     * @var bool
24
     */
25
    public $timestamps = false;
26
27
    /**
28
     * Default attributes.
29
     *
30
     * @var array
31
     */
32
    protected $attributes = [
33
        'config' => 'a:0:{}',
34
    ];
35
36
    /**
37
     * The cache minutes.
38
     *
39
     * @var int
40
     */
41
    protected $cacheMinutes = 99999;
42
43
    /**
44
     * The foreign key for translations.
45
     *
46
     * @var string
47
     */
48
    protected $translationForeignKey = 'assignment_id';
49
50
    /**
51
     * Translatable attributes.
52
     *
53
     * @var array
54
     */
55
    protected $translatedAttributes = [
56
        'label',
57
        'warning',
58
        'placeholder',
59
        'instructions',
60
    ];
61
62
    /**
63
     * The translation model.
64
     *
65
     * @var string
66
     */
67
    protected $translationModel = 'Anomaly\Streams\Platform\Assignment\AssignmentModelTranslation';
68
69
    /**
70
     * The database table name.
71
     *
72
     * @var string
73
     */
74
    protected $table = 'streams_assignments';
75
76
    /**
77
     * Because the assignment record holds translatable data
78
     * we have a conflict. The assignment table has translations
79
     * but not all assignment are translatable. This helps avoid
80
     * the translatable conflict during specific procedures.
81
     *
82
     * @param  array  $attributes
83
     * @return static
84
     */
85
    public static function create(array $attributes = [])
86
    {
87
        $model = parent::create($attributes);
88
89
        $model->saveTranslations();
90
91
        return $model;
92
    }
93
94
    /**
95
     * Set the field attribute.
96
     *
97
     * @param FieldInterface $field
98
     */
99
    public function setFieldAttribute(FieldInterface $field)
100
    {
101
        $this->attributes['field_id'] = $field->getId();
102
    }
103
104
    /**
105
     * Set the stream attribute.
106
     *
107
     * @param StreamInterface $stream
108
     */
109
    public function setStreamAttribute(StreamInterface $stream)
110
    {
111
        $this->attributes['stream_id'] = $stream->getId();
112
    }
113
114
    /**
115
     * Get the field slug.
116
     *
117
     * @return string
118
     */
119
    public function getFieldSlug()
120
    {
121
        if (isset($this->cache['field_slug']) && $this->cache['field_slug']) {
122
            return $this->cache['field_slug'];
123
        }
124
125
        $field = $this->getField();
126
127
        return $this->cache['field_slug'] = $field->getSlug();
128
    }
129
130
    /**
131
     * Get the assignment's field's type.
132
     *
133
     * @param  bool           $fresh
134
     * @return FieldType|null
135
     */
136
    public function getFieldType($fresh = false)
137
    {
138
        $field = $this->getField();
139
140
        if (!$field) {
141
            return null;
142
        }
143
144
        $type = $field->getType($fresh);
145
146
        if (!$type) {
147
            return null;
148
        }
149
150
        $type->setField($field->getSlug());
151
        $type->mergeConfig($this->getConfig());
152
        $type->setRequired($this->isRequired());
153
154
        return $type;
155
    }
156
157
    /**
158
     * Get the field type value. This helps
159
     * avoid spinning up a type instance
160
     * if you don't really need it.
161
     *
162
     * @return string
163
     */
164
    public function getFieldTypeValue()
165
    {
166
        $field = $this->getField();
167
168
        return $field->getTypeValue();
169
    }
170
171
    /**
172
     * Get the field name.
173
     *
174
     * @return string
175
     */
176
    public function getFieldName()
177
    {
178
        $field = $this->getField();
179
180
        return $field->getName();
181
    }
182
183
    /**
184
     * Get the assignment's field's config.
185
     *
186
     * @return string
187
     */
188
    public function getFieldConfig()
189
    {
190
        $field = $this->getField();
191
192
        return $field->getConfig();
193
    }
194
195
    /**
196
     * Get the assignment's field's rules.
197
     *
198
     * @return array
199
     */
200
    public function getFieldRules()
201
    {
202
        $field = $this->getField();
203
204
        return $field->getRules();
0 ignored issues
show
Bug introduced by
The method getRules() does not seem to exist on object<Anomaly\Streams\P...ontract\FieldInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
205
    }
206
207
    /**
208
     * Get the config.
209
     *
210
     * @return array
211
     */
212
    public function getConfig()
213
    {
214
        if (isset($this->cache['config']) && $this->cache['config']) {
215
            return $this->cache['config'];
216
        }
217
218
        if (is_array($this->attributes['config'])) {
219
            return $this->cache['config'] = $this->attributes['config'];
220
        }
221
222
        return $this->cache['config'] = $this->attributes['config'] = $this->config;
0 ignored issues
show
Documentation introduced by
The property config does not exist on object<Anomaly\Streams\P...gnment\AssignmentModel>. 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...
223
    }
224
225
    /**
226
     * Get the label.
227
     *
228
     * @return string
229
     */
230
    public function getLabel()
231
    {
232
        return $this->label;
0 ignored issues
show
Documentation introduced by
The property label does not exist on object<Anomaly\Streams\P...gnment\AssignmentModel>. 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...
233
    }
234
235
    /**
236
     * Get the warning.
237
     *
238
     * @return string
239
     */
240
    public function getWarning()
241
    {
242
        return $this->warning;
0 ignored issues
show
Documentation introduced by
The property warning does not exist on object<Anomaly\Streams\P...gnment\AssignmentModel>. 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...
243
    }
244
245
    /**
246
     * Get the instructions.
247
     *
248
     * @return null
249
     */
250
    public function getInstructions()
251
    {
252
        return $this->instructions;
0 ignored issues
show
Documentation introduced by
The property instructions does not exist on object<Anomaly\Streams\P...gnment\AssignmentModel>. 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...
253
    }
254
255
    /**
256
     * Get the placeholder.
257
     *
258
     * @return null
259
     */
260
    public function getPlaceholder()
261
    {
262
        return $this->placeholder;
0 ignored issues
show
Documentation introduced by
The property placeholder does not exist on object<Anomaly\Streams\P...gnment\AssignmentModel>. 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...
263
    }
264
265
    /**
266
     * Get the related stream.
267
     *
268
     * @return StreamInterface
269
     */
270
    public function getStream()
271
    {
272
        return $this->stream;
0 ignored issues
show
Documentation introduced by
The property stream does not exist on object<Anomaly\Streams\P...gnment\AssignmentModel>. 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...
273
    }
274
275
    /**
276
     * Get the related stream's slug.
277
     *
278
     * @return string
279
     */
280
    public function getStreamSlug()
281
    {
282
        if ($stream = $this->getStream()) {
283
            return $stream->getSlug();
284
        }
285
286
        return null;
287
    }
288
289
    /**
290
     * Get the related stream's prefix.
291
     *
292
     * @return string
293
     */
294
    public function getStreamPrefix()
295
    {
296
        if ($stream = $this->getStream()) {
297
            return $stream->getPrefix();
298
        }
299
300
        return null;
301
    }
302
303
    /**
304
     * Get the related field.
305
     *
306
     * @return FieldInterface
307
     */
308
    public function getField()
309
    {
310
        if (isset($this->cache['field']) && $this->cache['field']) {
311
            return $this->cache['field'];
312
        }
313
314
        return $this->cache['field'] = $this->field;
0 ignored issues
show
Documentation introduced by
The property field does not exist on object<Anomaly\Streams\P...gnment\AssignmentModel>. 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...
315
    }
316
317
    /**
318
     * Get the related field ID.
319
     *
320
     * @return null|int
321
     */
322
    public function getFieldId()
323
    {
324
        $field = $this->getField();
325
326
        if (!$field) {
327
            return null;
328
        }
329
330
        return $field->getId();
331
    }
332
333
    /**
334
     * Get the unique flag.
335
     *
336
     * @return mixed
337
     */
338
    public function isUnique()
339
    {
340
        return $this->getAttributeFromArray('unique');
341
    }
342
343
    /**
344
     * Get the required flag.
345
     *
346
     * @return bool
347
     */
348
    public function isRequired()
349
    {
350
        return $this->getAttributeFromArray('required');
351
    }
352
353
    /**
354
     * Get the translatable flag.
355
     *
356
     * @return bool
357
     */
358
    public function isTranslatable()
359
    {
360
        $stream = $this->getStream();
361
362
        if ($stream && !$stream->isTranslatable()) {
363
            return false;
364
        }
365
366
        return $this->getAttributeFromArray('translatable');
367
    }
368
369
    /**
370
     * Get the column name.
371
     *
372
     * @return mixed
373
     */
374
    public function getColumnName()
375
    {
376
        $type = $this->getFieldType();
377
378
        return $type->getColumnName();
379
    }
380
381
    /**
382
     * Set config attribute.
383
     *
384
     * @param array $config
385
     */
386
    public function setConfigAttribute($config)
387
    {
388
        $this->attributes['config'] = serialize((array)$config);
389
    }
390
391
    /**
392
     * Return the decoded config attribute.
393
     *
394
     * @param  $config
395
     * @return mixed
396
     */
397
    public function getConfigAttribute($config)
398
    {
399
        if (!is_array($config)) {
400
            return (array)unserialize($config);
401
        }
402
403
        return $config;
404
    }
405
406
    /**
407
     * @param  array                $items
408
     * @return AssignmentCollection
409
     */
410
    public function newCollection(array $items = [])
411
    {
412
        return new AssignmentCollection($items);
413
    }
414
415
    /**
416
     * Return a created presenter.
417
     *
418
     * @return AssignmentPresenter
419
     */
420
    public function getPresenter()
421
    {
422
        return new AssignmentPresenter($this);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \Anomaly\Stre...gnmentPresenter($this); (Anomaly\Streams\Platform...ent\AssignmentPresenter) is incompatible with the return type declared by the interface Robbo\Presenter\PresentableInterface::getPresenter of type Robbo\Presenter\Robbo\Presenter\Presenter.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
423
    }
424
425
    /**
426
     * Compile the assignment's stream.
427
     *
428
     * @return AssignmentInterface
429
     */
430
    public function compileStream()
431
    {
432
        if ($stream = $this->getStream()) {
433
            $stream->compile();
434
        }
435
436
        return $this;
437
    }
438
439
    /**
440
     * Return the stream relation.
441
     *
442
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
443
     */
444
    public function stream()
445
    {
446
        return $this->belongsTo('Anomaly\Streams\Platform\Stream\StreamModel');
447
    }
448
449
    /**
450
     * Return the field relation.
451
     *
452
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
453
     */
454
    public function field()
455
    {
456
        return $this->belongsTo('Anomaly\Streams\Platform\Field\FieldModel', 'field_id');
457
    }
458
}
459