JsonExpression.php 1.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. <?php
  2. namespace Illuminate\Database\Query;
  3. use InvalidArgumentException;
  4. class JsonExpression extends Expression
  5. {
  6. /**
  7. * Create a new raw query expression.
  8. *
  9. * @param mixed $value
  10. * @return void
  11. */
  12. public function __construct($value)
  13. {
  14. parent::__construct(
  15. $this->getJsonBindingParameter($value)
  16. );
  17. }
  18. /**
  19. * Translate the given value into the appropriate JSON binding parameter.
  20. *
  21. * @param mixed $value
  22. * @return string
  23. */
  24. protected function getJsonBindingParameter($value)
  25. {
  26. switch ($type = gettype($value)) {
  27. case 'boolean':
  28. return $value ? 'true' : 'false';
  29. case 'integer':
  30. case 'double':
  31. return $value;
  32. case 'string':
  33. return '?';
  34. case 'object':
  35. case 'array':
  36. return '?';
  37. }
  38. throw new InvalidArgumentException("JSON value is of illegal type: {$type}");
  39. }
  40. }