|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Squirrel\Queries\Builder; |
| 4 | + |
| 5 | +use Squirrel\Debug\Debug; |
| 6 | +use Squirrel\Queries\Exception\DBInvalidOptionException; |
| 7 | + |
| 8 | +trait FlattenedFieldsWithTypeTrait |
| 9 | +{ |
| 10 | + /** |
| 11 | + * @return int[] |
| 12 | + */ |
| 13 | + public function getFlattenedIntegerFields(): array |
| 14 | + { |
| 15 | + $values = $this->getFlattenedFields(); |
| 16 | + |
| 17 | + foreach ($values as $key => $value) { |
| 18 | + if (!\is_integer($value) && !\is_string($value)) { |
| 19 | + throw Debug::createException( |
| 20 | + DBInvalidOptionException::class, |
| 21 | + [BuilderInterface::class], |
| 22 | + 'Flattened integers requested, but not all values were int or string', |
| 23 | + ); |
| 24 | + } |
| 25 | + |
| 26 | + // Convert non-int values which do not change when type casted |
| 27 | + if ( |
| 28 | + !\is_integer($value) |
| 29 | + && \strval(\intval($value)) === \strval($value) |
| 30 | + ) { |
| 31 | + $values[$key] = \intval($value); |
| 32 | + continue; |
| 33 | + } |
| 34 | + |
| 35 | + if (!\is_int($value)) { |
| 36 | + throw Debug::createException( |
| 37 | + DBInvalidOptionException::class, |
| 38 | + [BuilderInterface::class], |
| 39 | + 'Flattened integers requested, but not all values were integers', |
| 40 | + ); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + return $values; |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * @return float[] |
| 49 | + */ |
| 50 | + public function getFlattenedFloatFields(): array |
| 51 | + { |
| 52 | + $values = $this->getFlattenedFields(); |
| 53 | + |
| 54 | + foreach ($values as $key => $value) { |
| 55 | + if (\is_int($value)) { |
| 56 | + $values[$key] = \floatval($value); |
| 57 | + continue; |
| 58 | + } |
| 59 | + |
| 60 | + if (!\is_float($value) && !\is_string($value)) { |
| 61 | + throw Debug::createException( |
| 62 | + DBInvalidOptionException::class, |
| 63 | + [BuilderInterface::class], |
| 64 | + 'Flattened floats requested, but not all values were float or string', |
| 65 | + ); |
| 66 | + } |
| 67 | + |
| 68 | + // Convert non-float values which do not change when type casted |
| 69 | + if ( |
| 70 | + !\is_float($value) |
| 71 | + && \strval(\floatval($value)) === \strval($value) |
| 72 | + ) { |
| 73 | + $values[$key] = \floatval($value); |
| 74 | + continue; |
| 75 | + } |
| 76 | + |
| 77 | + if (!\is_float($value)) { |
| 78 | + throw Debug::createException( |
| 79 | + DBInvalidOptionException::class, |
| 80 | + [BuilderInterface::class], |
| 81 | + 'Flattened floats requested, but not all values were floats', |
| 82 | + ); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + return $values; |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * @return string[] |
| 91 | + */ |
| 92 | + public function getFlattenedStringFields(): array |
| 93 | + { |
| 94 | + $values = $this->getFlattenedFields(); |
| 95 | + |
| 96 | + foreach ($values as $key => $value) { |
| 97 | + // Integers and floats can be converted to strings without problems |
| 98 | + if ( |
| 99 | + \is_int($value) |
| 100 | + || \is_float($value) |
| 101 | + ) { |
| 102 | + $values[$key] = \strval($value); |
| 103 | + continue; |
| 104 | + } |
| 105 | + |
| 106 | + if (!\is_string($value)) { |
| 107 | + throw Debug::createException( |
| 108 | + DBInvalidOptionException::class, |
| 109 | + [BuilderInterface::class], |
| 110 | + 'Flattened strings requested, but not all values were strings', |
| 111 | + ); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + return $values; |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * @return bool[] |
| 120 | + */ |
| 121 | + public function getFlattenedBooleanFields(): array |
| 122 | + { |
| 123 | + $values = $this->getFlattenedFields(); |
| 124 | + |
| 125 | + foreach ($values as $key => $value) { |
| 126 | + // Convert non-boolean values which can reasonably be converted to boolean |
| 127 | + if ( |
| 128 | + $value === 0 |
| 129 | + || $value === '0' |
| 130 | + ) { |
| 131 | + $values[$key] = false; |
| 132 | + continue; |
| 133 | + } elseif ( |
| 134 | + $value === 1 |
| 135 | + || $value === '1' |
| 136 | + ) { |
| 137 | + $values[$key] = true; |
| 138 | + continue; |
| 139 | + } |
| 140 | + |
| 141 | + if (!\is_bool($value)) { |
| 142 | + throw Debug::createException( |
| 143 | + DBInvalidOptionException::class, |
| 144 | + [BuilderInterface::class], |
| 145 | + 'Flattened booleans requested, but not all values were booleans', |
| 146 | + ); |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + return $values; |
| 151 | + } |
| 152 | +} |
0 commit comments