|
| 1 | +<?php |
| 2 | +namespace Sql\Predicate; |
| 3 | +use Sql\AbstractExpression; |
| 4 | +use Sql\Exception; |
| 5 | +use Sql\Select; |
| 6 | +class IfExpression extends AbstractExpression implements PredicateInterface { |
| 7 | + protected null|Expression|Select|PredicateInterface $condition = null; |
| 8 | + protected null|Expression|Select|PredicateInterface $value_if_true = null; |
| 9 | + protected null|Expression|Select|PredicateInterface $value_if_false = null; |
| 10 | + protected string $specification = 'IF(%1$s, %2$s, %3$s)'; |
| 11 | + /** |
| 12 | + * Constructor |
| 13 | + * |
| 14 | + * @param null|string|Select|Expression|PredicateInterface $condition |
| 15 | + * @param mixed ...$valueParameter |
| 16 | + */ |
| 17 | + public function __construct(null|string|Select|Expression|PredicateInterface $condition = null, |
| 18 | + mixed ...$valueParameter) { |
| 19 | + if ($condition !== null) { |
| 20 | + $this->setCondition($condition, ...$valueParameter); |
| 21 | + } |
| 22 | + } |
| 23 | + /** |
| 24 | + * Set condition for IF comparison |
| 25 | + * |
| 26 | + * @param null|string|Select|Expression|PredicateInterface $condition |
| 27 | + * @param mixed ...$valueParameter |
| 28 | + * @return self Provides a fluent interface |
| 29 | + * @throws Exception\InvalidArgumentException |
| 30 | + */ |
| 31 | + public function setCondition(null|string|Select|Expression|PredicateInterface $condition, |
| 32 | + mixed ...$valueParameter): self { |
| 33 | + if (is_string($condition)) { |
| 34 | + if (! empty($valueParameter)) { |
| 35 | + $valueParameter1 = current($valueParameter); |
| 36 | + $valueParameterArray = is_array($valueParameter1) ? $valueParameter1 : $valueParameter; |
| 37 | + $condition = new Expression($condition, $valueParameterArray); |
| 38 | + } else { |
| 39 | + $condition = new Expression($condition); |
| 40 | + } |
| 41 | + } |
| 42 | + $this->condition = $condition; |
| 43 | + |
| 44 | + return $this; |
| 45 | + } |
| 46 | + /** |
| 47 | + * Gets set of values in IN comparison |
| 48 | + * |
| 49 | + * @return null|Expression|Select |
| 50 | + */ |
| 51 | + public function getCondition(): null|Expression|Select|PredicateInterface { |
| 52 | + return $this->condition; |
| 53 | + } |
| 54 | + /** |
| 55 | + * Set value_if_true for IF comparison |
| 56 | + * |
| 57 | + * @param null|string|Select|Expression|PredicateInterface $value_if_true |
| 58 | + * @param mixed ...$valueParameter |
| 59 | + * @return self Provides a fluent interface |
| 60 | + * @throws Exception\InvalidArgumentException |
| 61 | + */ |
| 62 | + public function setValueIfTrue(null|string|Select|Expression|PredicateInterface $value_if_true, |
| 63 | + mixed ...$valueParameter): self { |
| 64 | + if (is_string($value_if_true)) { |
| 65 | + if (! empty($valueParameter)) { |
| 66 | + $valueParameter1 = current($valueParameter); |
| 67 | + $valueParameterArray = is_array($valueParameter1) ? $valueParameter1 : $valueParameter; |
| 68 | + $value_if_true = new Expression($value_if_true, $valueParameterArray); |
| 69 | + } else { |
| 70 | + $value_if_true = new Expression($value_if_true); |
| 71 | + } |
| 72 | + } |
| 73 | + $this->value_if_true = $value_if_true; |
| 74 | + |
| 75 | + return $this; |
| 76 | + } |
| 77 | + /** |
| 78 | + * Gets set of values in IN comparison |
| 79 | + * |
| 80 | + * @return null|Expression|Select |
| 81 | + */ |
| 82 | + public function getValueIfTrue(): null|Expression|Select|PredicateInterface { |
| 83 | + return $this->value_if_true; |
| 84 | + } |
| 85 | + /** |
| 86 | + * Set value_if_false for IF comparison |
| 87 | + * |
| 88 | + * @param null|string|Select|Expression|PredicateInterface $value_if_false |
| 89 | + * @param mixed ...$valueParameter |
| 90 | + * @return self Provides a fluent interface |
| 91 | + * @throws Exception\InvalidArgumentException |
| 92 | + */ |
| 93 | + public function setValueIfFalse(null|string|Select|Expression|PredicateInterface $value_if_false, |
| 94 | + mixed ...$valueParameter): self { |
| 95 | + if (is_string($value_if_false)) { |
| 96 | + if (! empty($valueParameter)) { |
| 97 | + $valueParameter1 = current($valueParameter); |
| 98 | + $valueParameterArray = is_array($valueParameter1) ? $valueParameter1 : $valueParameter; |
| 99 | + $value_if_false = new Expression($value_if_false, $valueParameterArray); |
| 100 | + } else { |
| 101 | + $value_if_false = new Expression($value_if_false); |
| 102 | + } |
| 103 | + } |
| 104 | + $this->value_if_false = $value_if_false; |
| 105 | + |
| 106 | + return $this; |
| 107 | + } |
| 108 | + /** |
| 109 | + * Gets set of values in IN comparison |
| 110 | + * |
| 111 | + * @return null|Expression|Select |
| 112 | + */ |
| 113 | + public function getValueIfFalse(): null|Expression|Select|PredicateInterface { |
| 114 | + return $this->value_if_false; |
| 115 | + } |
| 116 | + /** |
| 117 | + * Return array of parts for where statement |
| 118 | + * |
| 119 | + * @return array |
| 120 | + */ |
| 121 | + public function getExpressionData(): array { |
| 122 | + $condition = $this->getCondition(); |
| 123 | + $specification = $this->specification; |
| 124 | + if (! $condition instanceof \Sql\Select && ! $condition instanceof \Sql\Where) { |
| 125 | + $specification = str_replace('%1$s', '(%1$s)', $specification); |
| 126 | + } |
| 127 | + $value_if_true = $this->getValueIfTrue(); |
| 128 | + if (! $value_if_true instanceof \Sql\Select && ! $condition instanceof \Sql\Where) { |
| 129 | + $specification = str_replace('%2$s', '(%2$s)', $specification); |
| 130 | + } |
| 131 | + $value_if_false = $this->getValueIfFalse(); |
| 132 | + if (! $value_if_false instanceof \Sql\Select && ! $condition instanceof \Sql\Where) { |
| 133 | + $specification = str_replace('%3$s', '(%3$s)', $specification); |
| 134 | + } |
| 135 | + |
| 136 | + $replacements = [ |
| 137 | + $condition ?? new Expression('null'), |
| 138 | + $value_if_true ?? new Expression('null'), |
| 139 | + $value_if_false ?? new Expression('null') |
| 140 | + ]; |
| 141 | + $types = [self::TYPE_VALUE, self::TYPE_VALUE, self::TYPE_VALUE]; |
| 142 | + |
| 143 | + return [ |
| 144 | + [ |
| 145 | + $specification, |
| 146 | + $replacements, |
| 147 | + $types |
| 148 | + ] |
| 149 | + ]; |
| 150 | + } |
| 151 | +} |
0 commit comments