-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIntField.php
70 lines (56 loc) · 1.51 KB
/
IntField.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?php
namespace ModernPDO\Fields;
use ModernPDO\Escaper;
/**
* Int table field.
*/
class IntField extends Field
{
/**
* Int constructor.
*
* @param string $name field name
* @param bool $unsigned field value is positive
* @param bool $canBeNull field value can be null
* @param int|false|null $default default field value
*
* If $canBeNull is false $default must be integer or false.
* If $default is false, it means that no default value will be set.
*/
public function __construct(
private string $name,
private bool $unsigned = false,
private bool $canBeNull = false,
private int|null|bool $default = false,
) {
}
/**
* Returns field query.
*/
public function build(Escaper $escaper): string
{
$query = $escaper->column($this->name) . ' INT';
// Check unsigned
if ($this->unsigned) {
$query .= ' UNSIGNED';
}
// Check can be null
if (!$this->canBeNull) {
$query .= ' NOT';
if ($this->default === null) {
$this->default = false;
}
}
$query .= ' NULL';
// Check default
if ($this->default !== false) {
$query .= ' DEFAULT ';
if ($this->default === null) {
$query .= 'NULL';
} else {
$query .= $this->default;
}
}
return $query;
}
}