SimpleCondition.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. /**
  3. * @link https://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license https://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. * @phpcs:disable Squiz.NamingConventions.ValidVariableName.PrivateNoUnderscore
  15. */
  16. class SimpleCondition implements ConditionInterface
  17. {
  18. /**
  19. * @var string $operator the operator to use. Anything could be used e.g. `>`, `<=`, etc.
  20. */
  21. private $operator;
  22. /**
  23. * @var mixed the column name to the left of [[operator]]
  24. */
  25. private $column;
  26. /**
  27. * @var mixed the value to the right of the [[operator]]
  28. */
  29. private $value;
  30. /**
  31. * SimpleCondition constructor
  32. *
  33. * @param mixed $column the literal to the left of $operator
  34. * @param string $operator the operator to use. Anything could be used e.g. `>`, `<=`, etc.
  35. * @param mixed $value the literal to the right of $operator
  36. */
  37. public function __construct($column, $operator, $value)
  38. {
  39. $this->column = $column;
  40. $this->operator = $operator;
  41. $this->value = $value;
  42. }
  43. /**
  44. * @return string
  45. */
  46. public function getOperator()
  47. {
  48. return $this->operator;
  49. }
  50. /**
  51. * @return mixed
  52. */
  53. public function getColumn()
  54. {
  55. return $this->column;
  56. }
  57. /**
  58. * @return mixed
  59. */
  60. public function getValue()
  61. {
  62. return $this->value;
  63. }
  64. /**
  65. * {@inheritdoc}
  66. * @throws InvalidArgumentException if wrong number of operands have been given.
  67. */
  68. public static function fromArrayDefinition($operator, $operands)
  69. {
  70. if (count($operands) !== 2) {
  71. throw new InvalidArgumentException("Operator '$operator' requires two operands.");
  72. }
  73. return new static($operands[0], $operator, $operands[1]);
  74. }
  75. }