1
|
|
|
<?php namespace Rocket\Entities; |
2
|
|
|
|
3
|
|
|
use Illuminate\Database\Eloquent\Model; |
4
|
|
|
use Illuminate\Support\Facades\Validator; |
5
|
|
|
use Rocket\Entities\Exceptions\InvalidValueException; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Field |
9
|
|
|
* |
10
|
|
|
* @property int $id The field id |
11
|
|
|
* @property string $name The name of the field |
12
|
|
|
* @property int $weight The order of this field |
13
|
|
|
* @property int $revision_id The field's revision id |
14
|
|
|
* @property-read \DateTime $created_at |
15
|
|
|
* @property-read \DateTime $updated_at |
16
|
|
|
*/ |
17
|
|
|
abstract class Field extends Model |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* {@inheritdoc} |
21
|
|
|
*/ |
22
|
90 |
|
public function toArray() |
23
|
|
|
{ |
24
|
90 |
|
return $this->getAttribute('value'); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Get the revisions for this class |
29
|
|
|
* |
30
|
|
|
* @codeCoverageIgnore |
31
|
|
|
*/ |
32
|
|
|
public function revision() |
33
|
|
|
{ |
34
|
|
|
return $this->belongsTo(Revision::class); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Validate and set the value |
39
|
|
|
* |
40
|
|
|
* @param mixed $value |
41
|
|
|
*/ |
42
|
231 |
|
public function setValueAttribute($value) |
43
|
|
|
{ |
44
|
231 |
|
if (!$this->isValid($value)) { |
45
|
51 |
|
throw new InvalidValueException("The value in the field '" . get_class($this) . "' is invalid"); |
46
|
|
|
} |
47
|
|
|
|
48
|
180 |
|
$this->attributes['value'] = $this->prepareValue($value); |
49
|
180 |
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Prepare the value to be stored. |
53
|
|
|
* |
54
|
|
|
* Particularly useful for dates or very special fields |
55
|
|
|
* |
56
|
|
|
* @param mixed $value The value to prepare |
57
|
|
|
* @return mixed |
58
|
|
|
*/ |
59
|
165 |
|
protected function prepareValue($value) |
60
|
|
|
{ |
61
|
165 |
|
return $value; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Checks if a field is valid |
66
|
|
|
* |
67
|
|
|
* @param mixed $value The value to validate |
68
|
|
|
* @return bool |
69
|
|
|
*/ |
70
|
|
|
abstract protected function isValid($value); |
71
|
|
|
} |
72
|
|
|
|