ellisgl /
GeekLab-GLPDO2
| 1 | <?php |
||
| 2 | |||
| 3 | namespace GeekLab\GLPDO2\Bindings\MySQL; |
||
| 4 | |||
| 5 | use GeekLab\GLPDO2\Bindings\StringBindingInterface; |
||
| 6 | use InvalidArgumentException; |
||
| 7 | use JsonException; |
||
| 8 | use PDO; |
||
| 9 | |||
| 10 | class MySQLStringBindings implements StringBindingInterface |
||
| 11 | { |
||
| 12 | /** |
||
| 13 | * @param mixed $value |
||
| 14 | * @param bool $null |
||
| 15 | * |
||
| 16 | * @return array{string, int} |
||
|
0 ignored issues
–
show
Documentation
Bug
introduced
by
Loading history...
|
|||
| 17 | * @throws JsonException |
||
| 18 | */ |
||
| 19 | 5 | public function bJSON(mixed $value, bool $null = false): array |
|
| 20 | { |
||
| 21 | // Use NULL? |
||
| 22 | 5 | if ($value === null) { |
|
| 23 | 2 | if ($null) { |
|
| 24 | 1 | return $this->bStr(null, true); |
|
| 25 | } |
||
| 26 | |||
| 27 | 1 | throw new JsonException('Can not bind NULL in JSON spot.'); |
|
| 28 | } |
||
| 29 | |||
| 30 | 3 | if (is_object($value)) { |
|
| 31 | 1 | $value = json_encode($value); |
|
| 32 | 3 | } elseif (is_string($value)) { |
|
| 33 | 2 | $JSON = json_decode($value, false, 255); |
|
| 34 | |||
| 35 | 2 | if (json_last_error()) { |
|
| 36 | 1 | throw new JsonException('Can not bind invalid JSON in JSON spot. (' . json_last_error_msg() . ')'); |
|
| 37 | } |
||
| 38 | |||
| 39 | 1 | $value = json_encode($JSON); |
|
| 40 | } else { |
||
| 41 | 1 | throw new JsonException('Can not bind invalid JSON in JSON spot. (' . $value . ')'); |
|
| 42 | } |
||
| 43 | |||
| 44 | 1 | return $this->bStr($value); |
|
| 45 | } |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Create and bind string for LIKE() statements. |
||
| 49 | * |
||
| 50 | * @param string $value |
||
| 51 | * @param bool $ends Ends with? |
||
| 52 | * @param bool $starts Starts with? |
||
| 53 | * |
||
| 54 | * @return array{string} |
||
|
0 ignored issues
–
show
|
|||
| 55 | */ |
||
| 56 | 4 | public function bLike(string $value, bool $ends = false, bool $starts = false): array |
|
| 57 | { |
||
| 58 | 4 | $arr = ['%', $value, '%']; |
|
| 59 | |||
| 60 | 4 | if ($starts) { |
|
| 61 | // Starts with. |
||
| 62 | 2 | array_shift($arr); |
|
| 63 | } |
||
| 64 | |||
| 65 | 4 | if ($ends) { |
|
| 66 | // Ends with. |
||
| 67 | 2 | array_pop($arr); |
|
| 68 | } |
||
| 69 | |||
| 70 | 4 | return [implode('', $arr)]; |
|
| 71 | } |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Bind a string value. |
||
| 75 | * |
||
| 76 | * @param float | bool | int | string | null $value |
||
| 77 | * @param bool $null |
||
| 78 | * @param int $type |
||
| 79 | * |
||
| 80 | * @return array{string, int} |
||
|
0 ignored issues
–
show
|
|||
| 81 | * @throws InvalidArgumentException |
||
| 82 | */ |
||
| 83 | 24 | public function bStr( |
|
| 84 | float | bool | int | string | null $value, |
||
| 85 | bool $null = false, |
||
| 86 | int $type = PDO::PARAM_STR |
||
| 87 | ): array { |
||
| 88 | 24 | if ($value === null) { |
|
| 89 | 3 | if (!$null) { |
|
| 90 | 1 | throw new InvalidArgumentException('Can not bind NULL in string spot.'); |
|
| 91 | } |
||
| 92 | |||
| 93 | 2 | $type = PDO::PARAM_NULL; |
|
| 94 | } |
||
| 95 | |||
| 96 | 23 | return [(string)$value, $type]; |
|
| 97 | } |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Convert an array into a string and bind it. |
||
| 101 | * Great for IN() statements. |
||
| 102 | * |
||
| 103 | * @param array{} | array{mixed} $values |
||
|
0 ignored issues
–
show
|
|||
| 104 | * @param float | bool | int | string $default |
||
| 105 | * |
||
| 106 | * @return array{float | bool | int | string} |
||
|
0 ignored issues
–
show
|
|||
| 107 | */ |
||
| 108 | 1 | public function bStrArr(array $values, float | bool | int | string $default = ''): array |
|
| 109 | { |
||
| 110 | 1 | return [empty($values) ? $default : '\'' . implode("', '", $values) . '\'']; |
|
| 111 | } |
||
| 112 | } |
||
| 113 |