SimpleCondition.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\db\conditions;
  8. use yii\base\InvalidArgumentException;
  9. /**
  10. * Class SimpleCondition represents a simple condition like `"column" operator value`.
  11. *
  12. * @author Dmytro Naumenko <d.naumenko.a@gmail.com>
  13. * @since 2.0.14
  14. */
  15. class SimpleCondition implements ConditionInterface
  16. {
  17. /**
  18. * @var string $operator the operator to use. Anything could be used e.g. `>`, `<=`, etc.
  19. */
  20. private $operator;
  21. /**
  22. * @var mixed the column name to the left of [[operator]]
  23. */
  24. private $column;
  25. /**
  26. * @var mixed the value to the right of the [[operator]]
  27. */
  28. private $value;
  29. /**
  30. * SimpleCondition constructor
  31. *
  32. * @param mixed $column the literal to the left of $operator
  33. * @param string $operator the operator to use. Anything could be used e.g. `>`, `<=`, etc.
  34. * @param mixed $value the literal to the right of $operator
  35. */
  36. public function __construct($column, $operator, $value)
  37. {
  38. $this->column = $column;
  39. $this->operator = $operator;
  40. $this->value = $value;
  41. }
  42. /**
  43. * @return string
  44. */
  45. public function getOperator()
  46. {
  47. return $this->operator;
  48. }
  49. /**
  50. * @return mixed
  51. */
  52. public function getColumn()
  53. {
  54. return $this->column;
  55. }
  56. /**
  57. * @return mixed
  58. */
  59. public function getValue()
  60. {
  61. return $this->value;
  62. }
  63. /**
  64. * {@inheritdoc}
  65. * @throws InvalidArgumentException if wrong number of operands have been given.
  66. */
  67. public static function fromArrayDefinition($operator, $operands)
  68. {
  69. if (count($operands) !== 2) {
  70. throw new InvalidArgumentException("Operator '$operator' requires two operands.");
  71. }
  72. return new static($operands[0], $operator, $operands[1]);
  73. }
  74. }