InCondition.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. use yii\db\ExpressionInterface;
  10. /**
  11. * Class InCondition represents `IN` condition.
  12. *
  13. * @author Dmytro Naumenko <d.naumenko.a@gmail.com>
  14. * @since 2.0.14
  15. */
  16. class InCondition implements ConditionInterface
  17. {
  18. /**
  19. * @var string $operator the operator to use (e.g. `IN` or `NOT IN`)
  20. */
  21. private $operator;
  22. /**
  23. * @var string|string[] the column name. If it is an array, a composite `IN` condition
  24. * will be generated.
  25. */
  26. private $column;
  27. /**
  28. * @var ExpressionInterface[]|string[]|int[] an array of values that [[column]] value should be among.
  29. * If it is an empty array the generated expression will be a `false` value if
  30. * [[operator]] is `IN` and empty if operator is `NOT IN`.
  31. */
  32. private $values;
  33. /**
  34. * SimpleCondition constructor
  35. *
  36. * @param string|string[] the column name. If it is an array, a composite `IN` condition
  37. * will be generated.
  38. * @param string $operator the operator to use (e.g. `IN` or `NOT IN`)
  39. * @param array an array of values that [[column]] value should be among. If it is an empty array the generated
  40. * expression will be a `false` value if [[operator]] is `IN` and empty if operator is `NOT IN`.
  41. */
  42. public function __construct($column, $operator, $values)
  43. {
  44. $this->column = $column;
  45. $this->operator = $operator;
  46. $this->values = $values;
  47. }
  48. /**
  49. * @return string
  50. */
  51. public function getOperator()
  52. {
  53. return $this->operator;
  54. }
  55. /**
  56. * @return mixed
  57. */
  58. public function getColumn()
  59. {
  60. return $this->column;
  61. }
  62. /**
  63. * @return ExpressionInterface[]|string[]|int[]
  64. */
  65. public function getValues()
  66. {
  67. return $this->values;
  68. }
  69. /**
  70. * {@inheritdoc}
  71. * @throws InvalidArgumentException if wrong number of operands have been given.
  72. */
  73. public static function fromArrayDefinition($operator, $operands)
  74. {
  75. if (!isset($operands[0], $operands[1])) {
  76. throw new InvalidArgumentException("Operator '$operator' requires two operands.");
  77. }
  78. return new static($operands[0], $operator, $operands[1]);
  79. }
  80. }