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