Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
76 / 76 |
|
100.00% |
4 / 4 |
CRAP | |
100.00% |
1 / 1 |
| Field | |
100.00% |
76 / 76 |
|
100.00% |
4 / 4 |
23 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
2 | |||
| __get | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
| setTypeName | |
100.00% |
30 / 30 |
|
100.00% |
1 / 1 |
1 | |||
| setFlags | |
100.00% |
34 / 34 |
|
100.00% |
1 / 1 |
18 | |||
| 1 | <?php declare(strict_types=1); |
| 2 | /* |
| 3 | * This file is part of Aplus Framework Database Library. |
| 4 | * |
| 5 | * (c) Natan Felles <natanfelles@gmail.com> |
| 6 | * |
| 7 | * For the full copyright and license information, please view the LICENSE |
| 8 | * file that was distributed with this source code. |
| 9 | */ |
| 10 | namespace Framework\Database\Result; |
| 11 | |
| 12 | use Error; |
| 13 | |
| 14 | /** |
| 15 | * Class Field. |
| 16 | * |
| 17 | * @property-read string $name The name of the column |
| 18 | * @property-read string $orgname Original column name if an alias was specified |
| 19 | * @property-read string $table The name of the table this field belongs to (if not calculated) |
| 20 | * @property-read string $orgtable Original table name if an alias was specified |
| 21 | * @property-read string $def The default value for this field, represented as a string |
| 22 | * @property-read string $db |
| 23 | * @property-read string $catalog |
| 24 | * @property-read int $maxLength The maximum width of the field for the result set |
| 25 | * @property-read int $length The width of the field, as specified in the table definition |
| 26 | * @property-read int $charsetnr The character set number for the field |
| 27 | * @property-read int $flags An integer representing the bit-flags for the field |
| 28 | * @property-read int $type The data type used for this field |
| 29 | * @property-read int $decimals The number of decimals used (for integer fields) |
| 30 | * @property-read string|null $typeName The data type name |
| 31 | * @property-read bool $flagBinary Tell if it has the MYSQLI_BINARY_FLAG bit-flag |
| 32 | * @property-read bool $flagBlob Tell if it has the MYSQLI_BLOB_FLAG bit-flag |
| 33 | * @property-read bool $flagEnum Tell if it has the MYSQLI_ENUM_FLAG bit-flag |
| 34 | * @property-read bool $flagGroup Tell if it has the MYSQLI_GROUP_FLAG bit-flag |
| 35 | * @property-read bool $flagNum Tell if it has the MYSQLI_NUM_FLAG bit-flag |
| 36 | * @property-read bool $flagSet Tell if it has the MYSQLI_SET_FLAG bit-flag |
| 37 | * @property-read bool $flagTimestamp Tell if it has the MYSQLI_TIMESTAMP_FLAG bit-flag |
| 38 | * @property-read bool $flagUnsigned Tell if it has the MYSQLI_UNSIGNED_FLAG bit-flag |
| 39 | * @property-read bool $flagZerofill Tell if it has the MYSQLI_ZEROFILL_FLAG bit-flag |
| 40 | * @property-read bool $flagAutoIncrement Tell if it has the MYSQLI_AUTO_INCREMENT_FLAG bit-flag |
| 41 | * @property-read bool $flagMultipleKey Tell if it has the MYSQLI_MULTIPLE_KEY_FLAG bit-flag |
| 42 | * @property-read bool $flagNotNull Tell if it has the MYSQLI_NOT_NULL_FLAG bit-flag |
| 43 | * @property-read bool $flagPartKey Tell if it has the MYSQLI_PART_KEY_FLAG bit-flag |
| 44 | * @property-read bool $flagPriKey Tell if it has the MYSQLI_PRI_KEY_FLAG bit-flag |
| 45 | * @property-read bool $flagUniqueKey Tell if it has the MYSQLI_UNIQUE_KEY_FLAG bit-flag |
| 46 | * @property-read bool $flagNoDefaultValue Tell if it has the MYSQLI_NO_DEFAULT_VALUE_FLAG bit-flag |
| 47 | * @property-read bool $flagOnUpdateNow Tell if it has the MYSQLI_ON_UPDATE_NOW_FLAG bit-flag |
| 48 | * |
| 49 | * @package database |
| 50 | */ |
| 51 | class Field |
| 52 | { |
| 53 | protected string $name; |
| 54 | protected string $orgname; |
| 55 | protected string $table; |
| 56 | protected string $orgtable; |
| 57 | protected string $def; |
| 58 | protected string $db; |
| 59 | protected string $catalog; |
| 60 | protected int $maxLength; |
| 61 | protected int $length; |
| 62 | protected int $charsetnr; |
| 63 | protected int $flags; |
| 64 | protected int $type; |
| 65 | protected int $decimals; |
| 66 | protected ?string $typeName; |
| 67 | protected bool $flagBinary = false; |
| 68 | protected bool $flagBlob = false; |
| 69 | protected bool $flagEnum = false; |
| 70 | protected bool $flagGroup = false; |
| 71 | protected bool $flagNum = false; |
| 72 | protected bool $flagSet = false; |
| 73 | protected bool $flagTimestamp = false; |
| 74 | protected bool $flagUnsigned = false; |
| 75 | protected bool $flagZerofill = false; |
| 76 | protected bool $flagAutoIncrement = false; |
| 77 | protected bool $flagMultipleKey = false; |
| 78 | protected bool $flagNotNull = false; |
| 79 | protected bool $flagPartKey = false; |
| 80 | protected bool $flagPriKey = false; |
| 81 | protected bool $flagUniqueKey = false; |
| 82 | protected bool $flagNoDefaultValue = false; |
| 83 | protected bool $flagOnUpdateNow = false; |
| 84 | |
| 85 | public function __construct(\stdClass $field) |
| 86 | { |
| 87 | foreach ((array) $field as $key => $value) { |
| 88 | $key = \ucwords($key, '_'); |
| 89 | $key = \strtr($key, ['_' => '']); |
| 90 | $key[0] = \strtolower($key[0]); |
| 91 | $this->{$key} = $value; |
| 92 | } |
| 93 | $this->setTypeName(); |
| 94 | $this->setFlags(); |
| 95 | } |
| 96 | |
| 97 | public function __get(string $name) : mixed |
| 98 | { |
| 99 | if (\property_exists($this, $name)) { |
| 100 | return $this->{$name}; |
| 101 | } |
| 102 | throw new Error( |
| 103 | 'Undefined property: ' . static::class . '::$' . $name |
| 104 | ); |
| 105 | } |
| 106 | |
| 107 | protected function setTypeName() : void |
| 108 | { |
| 109 | $this->typeName = match ($this->type) { |
| 110 | \MYSQLI_TYPE_BIT => 'bit', |
| 111 | \MYSQLI_TYPE_BLOB => 'blob', |
| 112 | \MYSQLI_TYPE_CHAR => 'char', |
| 113 | \MYSQLI_TYPE_DATE => 'date', |
| 114 | \MYSQLI_TYPE_DATETIME => 'datetime', |
| 115 | \MYSQLI_TYPE_DECIMAL => 'decimal', |
| 116 | \MYSQLI_TYPE_DOUBLE => 'double', |
| 117 | \MYSQLI_TYPE_ENUM => 'enum', |
| 118 | \MYSQLI_TYPE_FLOAT => 'float', |
| 119 | \MYSQLI_TYPE_GEOMETRY => 'geometry', |
| 120 | \MYSQLI_TYPE_INT24 => 'int24', |
| 121 | //\MYSQLI_TYPE_INTERVAL => 'interval', |
| 122 | \MYSQLI_TYPE_JSON => 'json', |
| 123 | \MYSQLI_TYPE_LONG => 'long', |
| 124 | \MYSQLI_TYPE_LONG_BLOB => 'long_blob', |
| 125 | \MYSQLI_TYPE_LONGLONG => 'longlong', |
| 126 | \MYSQLI_TYPE_MEDIUM_BLOB => 'medium_blob', |
| 127 | \MYSQLI_TYPE_NEWDATE => 'newdate', |
| 128 | \MYSQLI_TYPE_NEWDECIMAL => 'newdecimal', |
| 129 | \MYSQLI_TYPE_NULL => 'null', |
| 130 | \MYSQLI_TYPE_SET => 'set', |
| 131 | \MYSQLI_TYPE_SHORT => 'short', |
| 132 | \MYSQLI_TYPE_STRING => 'string', |
| 133 | \MYSQLI_TYPE_TIME => 'time', |
| 134 | \MYSQLI_TYPE_TIMESTAMP => 'timestamp', |
| 135 | //\MYSQLI_TYPE_TINY => 'tiny', |
| 136 | \MYSQLI_TYPE_TINY_BLOB => 'tiny_blob', |
| 137 | \MYSQLI_TYPE_VAR_STRING => 'var_string', |
| 138 | \MYSQLI_TYPE_YEAR => 'year', |
| 139 | default => null |
| 140 | }; |
| 141 | } |
| 142 | |
| 143 | protected function setFlags() : void |
| 144 | { |
| 145 | if ($this->flags & \MYSQLI_BINARY_FLAG) { |
| 146 | $this->flagBinary = true; |
| 147 | } |
| 148 | if ($this->flags & \MYSQLI_BLOB_FLAG) { |
| 149 | $this->flagBlob = true; |
| 150 | } |
| 151 | if ($this->flags & \MYSQLI_ENUM_FLAG) { |
| 152 | $this->flagEnum = true; |
| 153 | } |
| 154 | if ($this->flags & \MYSQLI_GROUP_FLAG) { |
| 155 | $this->flagGroup = true; |
| 156 | } |
| 157 | if ($this->flags & \MYSQLI_NUM_FLAG) { |
| 158 | $this->flagNum = true; |
| 159 | } |
| 160 | if ($this->flags & \MYSQLI_SET_FLAG) { |
| 161 | $this->flagSet = true; |
| 162 | } |
| 163 | if ($this->flags & \MYSQLI_TIMESTAMP_FLAG) { |
| 164 | $this->flagTimestamp = true; |
| 165 | } |
| 166 | if ($this->flags & \MYSQLI_UNSIGNED_FLAG) { |
| 167 | $this->flagUnsigned = true; |
| 168 | } |
| 169 | if ($this->flags & \MYSQLI_ZEROFILL_FLAG) { |
| 170 | $this->flagZerofill = true; |
| 171 | } |
| 172 | if ($this->flags & \MYSQLI_AUTO_INCREMENT_FLAG) { |
| 173 | $this->flagAutoIncrement = true; |
| 174 | } |
| 175 | if ($this->flags & \MYSQLI_MULTIPLE_KEY_FLAG) { |
| 176 | $this->flagMultipleKey = true; |
| 177 | } |
| 178 | if ($this->flags & \MYSQLI_NOT_NULL_FLAG) { |
| 179 | $this->flagNotNull = true; |
| 180 | } |
| 181 | if ($this->flags & \MYSQLI_PART_KEY_FLAG) { |
| 182 | $this->flagPartKey = true; |
| 183 | } |
| 184 | if ($this->flags & \MYSQLI_PRI_KEY_FLAG) { |
| 185 | $this->flagPriKey = true; |
| 186 | } |
| 187 | if ($this->flags & \MYSQLI_UNIQUE_KEY_FLAG) { |
| 188 | $this->flagUniqueKey = true; |
| 189 | } |
| 190 | if ($this->flags & \MYSQLI_NO_DEFAULT_VALUE_FLAG) { |
| 191 | $this->flagNoDefaultValue = true; |
| 192 | } |
| 193 | if ($this->flags & \MYSQLI_ON_UPDATE_NOW_FLAG) { |
| 194 | $this->flagOnUpdateNow = true; |
| 195 | } |
| 196 | } |
| 197 | } |