fluent-booking
/
vendor
/
wpfluent
/
framework
/
src
/
WPFluent
/
Database
/
Query
/
JsonExpression.php
JsonExpression.php in Fluent Booking – The Ultimate Appointments Scheduling, Events Booking, Events Calendar Solution 1.5.21, at vendor/wpfluent/framework/src/WPFluent/Database/Query/JsonExpression.php
| 1 | <?php |
| 2 | |
| 3 | namespace FluentBooking\Framework\Database\Query; |
| 4 | |
| 5 | use InvalidArgumentException; |
| 6 | use FluentBooking\Framework\Database\Query\Expression; |
| 7 | |
| 8 | class JsonExpression extends Expression |
| 9 | { |
| 10 | /** |
| 11 | * The value of the expression. |
| 12 | * |
| 13 | * @var mixed |
| 14 | */ |
| 15 | protected $value; |
| 16 | |
| 17 | /** |
| 18 | * Create a new raw query expression. |
| 19 | * |
| 20 | * @param mixed $value |
| 21 | * @return void |
| 22 | */ |
| 23 | public function __construct($value) |
| 24 | { |
| 25 | $this->value = $this->getJsonBindingParameter($value); |
| 26 | } |
| 27 | |
| 28 | /** |
| 29 | * Translate the given value into the appropriate JSON binding parameter. |
| 30 | * |
| 31 | * @param mixed $value |
| 32 | * @return string |
| 33 | */ |
| 34 | protected function getJsonBindingParameter($value) |
| 35 | { |
| 36 | switch ($type = gettype($value)) { |
| 37 | case 'boolean': |
| 38 | return $value ? 'true' : 'false'; |
| 39 | case 'integer': |
| 40 | case 'double': |
| 41 | return $value; |
| 42 | case 'string': |
| 43 | return '?'; |
| 44 | case 'object': |
| 45 | case 'array': |
| 46 | return '?'; |
| 47 | } |
| 48 | |
| 49 | throw new InvalidArgumentException('JSON value is of illegal type: '.$type); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Get the value of the expression. |
| 54 | * |
| 55 | * @return mixed |
| 56 | */ |
| 57 | public function getValue() |
| 58 | { |
| 59 | return $this->value; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Get the value of the expression. |
| 64 | * |
| 65 | * @return string |
| 66 | */ |
| 67 | public function __toString() |
| 68 | { |
| 69 | return (string) $this->getValue(); |
| 70 | } |
| 71 | } |
| 72 |