InCondition.php 2.5 KB

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