ExistsCondition.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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\Query;
  10. /**
  11. * Condition that represents `EXISTS` operator.
  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 ExistsCondition implements ConditionInterface
  18. {
  19. /**
  20. * @var string $operator the operator to use (e.g. `EXISTS` or `NOT EXISTS`)
  21. */
  22. private $operator;
  23. /**
  24. * @var Query the [[Query]] object representing the sub-query.
  25. */
  26. private $query;
  27. /**
  28. * ExistsCondition constructor.
  29. *
  30. * @param string $operator the operator to use (e.g. `EXISTS` or `NOT EXISTS`)
  31. * @param Query $query the [[Query]] object representing the sub-query.
  32. */
  33. public function __construct($operator, $query)
  34. {
  35. $this->operator = $operator;
  36. $this->query = $query;
  37. }
  38. /**
  39. * {@inheritdoc}
  40. */
  41. public static function fromArrayDefinition($operator, $operands)
  42. {
  43. if (!isset($operands[0]) || !$operands[0] instanceof Query) {
  44. throw new InvalidArgumentException('Subquery for EXISTS operator must be a Query object.');
  45. }
  46. return new static($operator, $operands[0]);
  47. }
  48. /**
  49. * @return string
  50. */
  51. public function getOperator()
  52. {
  53. return $this->operator;
  54. }
  55. /**
  56. * @return Query
  57. */
  58. public function getQuery()
  59. {
  60. return $this->query;
  61. }
  62. }