|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Spiral, Core Components |
|
4
|
|
|
* |
|
5
|
|
|
* @author Wolfy-J |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace Spiral\ORM\Columns; |
|
9
|
|
|
|
|
10
|
|
|
use Spiral\Core\Component; |
|
11
|
|
|
use Spiral\Database\Schemas\Prototypes\AbstractColumn; |
|
12
|
|
|
use Spiral\ORM\ColumnInterface; |
|
13
|
|
|
use Spiral\ORM\Exceptions\AccessException; |
|
14
|
|
|
use Spiral\ORM\Exceptions\EnumException; |
|
15
|
|
|
use Spiral\ORM\RecordAccessorInterface; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Mocks enums values and provides ability to describe associated AbstractColumn via set of |
|
19
|
|
|
* configuration constants. |
|
20
|
|
|
* |
|
21
|
|
|
* Extends Component so you can connect scope specific functionality. |
|
22
|
|
|
*/ |
|
23
|
|
|
class EnumColumn extends Component implements RecordAccessorInterface, ColumnInterface |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* Set of allowed enum values. |
|
27
|
|
|
*/ |
|
28
|
|
|
const VALUES = []; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Default value. |
|
32
|
|
|
*/ |
|
33
|
|
|
const DEFAULT = null; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var bool |
|
37
|
|
|
*/ |
|
38
|
|
|
private $changed = false; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var string |
|
42
|
|
|
*/ |
|
43
|
|
|
private $value; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* {@inheritdoc} |
|
47
|
|
|
*/ |
|
48
|
|
|
public function __construct(string $value) |
|
49
|
|
|
{ |
|
50
|
|
|
$this->value = $value; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* {@inheritdoc} |
|
55
|
|
|
*/ |
|
56
|
|
|
public function setValue($data) |
|
57
|
|
|
{ |
|
58
|
|
|
if (!in_array($data, static::VALUES)) { |
|
59
|
|
|
throw new AccessException("Unable to set enum value, invalid value given"); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
$this->value = $data; |
|
63
|
|
|
$this->changed = true; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* {@inheritdoc} |
|
68
|
|
|
*/ |
|
69
|
|
|
public function packValue(): string |
|
70
|
|
|
{ |
|
71
|
|
|
return $this->value; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* {@inheritdoc} |
|
76
|
|
|
*/ |
|
77
|
|
|
public function hasChanges(): bool |
|
78
|
|
|
{ |
|
79
|
|
|
return $this->changed; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* {@inheritdoc} |
|
84
|
|
|
*/ |
|
85
|
|
|
public function flushChanges() |
|
86
|
|
|
{ |
|
87
|
|
|
$this->changed = false; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* {@inheritdoc} |
|
92
|
|
|
*/ |
|
93
|
|
|
public function compileUpdates(string $field = '') |
|
94
|
|
|
{ |
|
95
|
|
|
return $this->value; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* {@inheritdoc} |
|
100
|
|
|
*/ |
|
101
|
|
|
public function __toString() |
|
102
|
|
|
{ |
|
103
|
|
|
return $this->packValue(); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* {@inheritdoc} |
|
108
|
|
|
*/ |
|
109
|
|
|
public function jsonSerialize() |
|
110
|
|
|
{ |
|
111
|
|
|
return $this->packValue(); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* {@inheritdoc} |
|
116
|
|
|
*/ |
|
117
|
|
|
public static function describeColumn(AbstractColumn $column) |
|
118
|
|
|
{ |
|
119
|
|
|
if (empty(static::VALUES)) { |
|
120
|
|
|
throw new EnumException("Unable to describe enum column, no values are given"); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
$column->enum(static::VALUES); |
|
124
|
|
|
|
|
125
|
|
|
if (!empty(static::DEFAULT)) { |
|
126
|
|
|
$column->defaultValue(static::DEFAULT)->nullable(false); |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
} |