| 1 | <?php |
||
| 2 | |||
| 3 | namespace Lagdo\DbAdmin\Db\Page\Dml; |
||
| 4 | |||
| 5 | use Lagdo\DbAdmin\Driver\Entity\TableFieldEntity; |
||
| 6 | |||
| 7 | use function implode; |
||
| 8 | use function in_array; |
||
| 9 | use function preg_match; |
||
| 10 | |||
| 11 | class FieldEditEntity |
||
| 12 | { |
||
| 13 | /** |
||
| 14 | * @var mixed |
||
| 15 | */ |
||
| 16 | public $name; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * @var mixed |
||
| 20 | */ |
||
| 21 | public $type; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @var mixed |
||
| 25 | */ |
||
| 26 | public $fullType; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var mixed |
||
| 30 | */ |
||
| 31 | public $comment; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var mixed |
||
| 35 | */ |
||
| 36 | public $value; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var string|null |
||
| 40 | */ |
||
| 41 | public $function; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var array |
||
| 45 | */ |
||
| 46 | public $functions; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var array |
||
| 50 | */ |
||
| 51 | public $valueInput; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var array|null |
||
| 55 | */ |
||
| 56 | public $functionInput; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var array |
||
| 60 | */ |
||
| 61 | public $enums = []; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var bool|null |
||
| 65 | */ |
||
| 66 | public $isText = null; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @param TableFieldEntity $field |
||
| 70 | */ |
||
| 71 | public function __construct(public TableFieldEntity $field) |
||
| 72 | { |
||
| 73 | $this->type = $field->type; |
||
| 74 | $this->comment = $field->comment; |
||
| 75 | } |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @return boolean |
||
| 79 | */ |
||
| 80 | public function isDisabled(): bool |
||
| 81 | { |
||
| 82 | return $this->field->isDisabled(); |
||
| 83 | } |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @return boolean |
||
| 87 | */ |
||
| 88 | public function isEnum(): bool |
||
| 89 | { |
||
| 90 | return $this->type === 'enum'; |
||
| 91 | } |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @return boolean |
||
| 95 | */ |
||
| 96 | public function isSet(): bool |
||
| 97 | { |
||
| 98 | return $this->type === 'set'; |
||
| 99 | } |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @return boolean |
||
| 103 | */ |
||
| 104 | public function isBool(): bool |
||
| 105 | { |
||
| 106 | return preg_match('~bool~', $this->type); |
||
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 107 | } |
||
| 108 | |||
| 109 | /** |
||
| 110 | * @return boolean |
||
| 111 | */ |
||
| 112 | public function isJson(): bool |
||
| 113 | { |
||
| 114 | return $this->function === "json" || preg_match('~^jsonb?$~', $this->type); |
||
| 115 | } |
||
| 116 | |||
| 117 | /** |
||
| 118 | * @return boolean |
||
| 119 | */ |
||
| 120 | public function isText(): bool |
||
| 121 | { |
||
| 122 | return $this->isText ??= (bool)preg_match('~text|lob|memo~i', $this->type); |
||
| 123 | } |
||
| 124 | |||
| 125 | /** |
||
| 126 | * @return boolean |
||
| 127 | */ |
||
| 128 | public function hasNewLine(): bool |
||
| 129 | { |
||
| 130 | return preg_match("~\n~", $this->value ?? ''); |
||
|
0 ignored issues
–
show
|
|||
| 131 | } |
||
| 132 | |||
| 133 | /** |
||
| 134 | * @return boolean |
||
| 135 | */ |
||
| 136 | public function isSearch(): bool |
||
| 137 | { |
||
| 138 | // PostgreSQL search types. |
||
| 139 | return in_array($this->type, ['tsvector', 'tsquery']); |
||
| 140 | } |
||
| 141 | |||
| 142 | /** |
||
| 143 | * @return boolean |
||
| 144 | */ |
||
| 145 | public function editText(): bool |
||
| 146 | { |
||
| 147 | return $this->isText() || $this->hasNewLine() || $this->isSearch(); |
||
| 148 | } |
||
| 149 | |||
| 150 | /** |
||
| 151 | * @return boolean |
||
| 152 | */ |
||
| 153 | public function isChecked(): bool |
||
| 154 | { |
||
| 155 | return preg_match('~^(1|t|true|y|yes|on)$~i', $this->value ?? ''); |
||
|
0 ignored issues
–
show
|
|||
| 156 | } |
||
| 157 | |||
| 158 | /** |
||
| 159 | * @return boolean |
||
| 160 | */ |
||
| 161 | public function hasFunction(): bool |
||
| 162 | { |
||
| 163 | return in_array($this->function, $this->functions) || |
||
| 164 | isset($functions[$this->function]); |
||
|
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
| 165 | } |
||
| 166 | |||
| 167 | /** |
||
| 168 | * @return boolean |
||
| 169 | */ |
||
| 170 | public function isNumber(): bool |
||
| 171 | { |
||
| 172 | return (!$this->hasFunction() || $this->function === "") && |
||
| 173 | preg_match('~(?<!o)int(?!er)~', $this->type) && |
||
| 174 | !preg_match('~\[\]~', $this->field->fullType); |
||
| 175 | } |
||
| 176 | |||
| 177 | /** |
||
| 178 | * @param int $maxlength |
||
| 179 | * |
||
| 180 | * @return boolean |
||
| 181 | */ |
||
| 182 | public function bigSize(int $maxlength): bool |
||
| 183 | { |
||
| 184 | return preg_match('~char|binary~', $this->type) && $maxlength > 20; |
||
| 185 | } |
||
| 186 | |||
| 187 | /** |
||
| 188 | * @return string |
||
| 189 | */ |
||
| 190 | public function enumsLength(): string |
||
| 191 | { |
||
| 192 | return !$this->enums ? '' : "'" . implode("', '", $this->enums) . "'"; |
||
| 193 | } |
||
| 194 | |||
| 195 | /** |
||
| 196 | * @return mixed |
||
| 197 | */ |
||
| 198 | public function functionValue(): mixed |
||
| 199 | { |
||
| 200 | return $this->function === null || $this->hasFunction() ? $this->function : ''; |
||
| 201 | } |
||
| 202 | } |
||
| 203 |