| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Give\Vendors\StellarWP\FieldConditions\Concerns; |
| 6 |
|
| 7 |
use Closure; |
| 8 |
use Give\Vendors\StellarWP\FieldConditions\Config; |
| 9 |
use Give\Vendors\StellarWP\FieldConditions\Contracts\Condition; |
| 10 |
use Give\Vendors\StellarWP\FieldConditions\Contracts\ConditionSet; |
| 11 |
use Give\Vendors\StellarWP\FieldConditions\FieldCondition; |
| 12 |
use Give\Vendors\StellarWP\FieldConditions\NestedCondition; |
| 13 |
|
| 14 |
/** |
| 15 |
* @template C of Condition |
| 16 |
* @template-extends ConditionSet<C> |
| 17 |
*/ |
| 18 |
trait HasConditions |
| 19 |
{ |
| 20 |
/** |
| 21 |
* @var array<C> |
| 22 |
*/ |
| 23 |
protected $conditions = []; |
| 24 |
|
| 25 |
/** |
| 26 |
* Append condition instances to the end of the conditions array. |
| 27 |
* |
| 28 |
* @since 1.0.0 |
| 29 |
* |
| 30 |
* @param Condition ...$conditions |
| 31 |
*/ |
| 32 |
public function append(...$conditions) |
| 33 |
{ |
| 34 |
foreach ($conditions as $condition) { |
| 35 |
$this->validateCondition($condition); |
| 36 |
$this->conditions[] = $condition; |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Returns all internal conditions. |
| 42 |
* |
| 43 |
* @since 1.0.0 |
| 44 |
* |
| 45 |
* @return array<C> |
| 46 |
*/ |
| 47 |
public function getConditions(): array |
| 48 |
{ |
| 49 |
return $this->conditions; |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Returns whether the condition set has any conditions. |
| 54 |
* |
| 55 |
* @since 1.1.0 |
| 56 |
*/ |
| 57 |
public function hasConditions(): bool |
| 58 |
{ |
| 59 |
return ! empty($this->conditions); |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* @since 1.1.2 make nullable parameters explicit for PHP 8.4 compatibility |
| 64 |
* @since 1.1.1 return ConditionSet type for PHP 7.0 compatibility |
| 65 |
* @since 1.0.0 |
| 66 |
* |
| 67 |
* @param string|C|Closure $condition |
| 68 |
* @param string|null $comparisonOperator |
| 69 |
* @param mixed|null $value |
| 70 |
*/ |
| 71 |
public function where($condition, ?string $comparisonOperator = null, $value = null): ConditionSet |
| 72 |
{ |
| 73 |
return $this->and($condition, $comparisonOperator, $value); |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* @since 1.1.2 make nullable parameters explicit for PHP 8.4 compatibility |
| 78 |
* @since 1.1.1 return ConditionSet type for PHP 7.0 compatibility |
| 79 |
* @since 1.0.0 |
| 80 |
* |
| 81 |
* @param string|C|Closure $condition |
| 82 |
* @param string|null $comparisonOperator |
| 83 |
* @param mixed|null $value |
| 84 |
*/ |
| 85 |
public function and($condition, ?string $comparisonOperator = null, $value = null): ConditionSet |
| 86 |
{ |
| 87 |
$this->conditions[] = $this->createCondition($condition, $comparisonOperator, $value, 'and'); |
| 88 |
|
| 89 |
return $this; |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* @since 1.1.2 make nullable parameters explicit for PHP 8.4 compatibility |
| 94 |
* @since 1.1.1 return ConditionSet type for PHP 7.0 compatibility |
| 95 |
* @since 1.0.0 |
| 96 |
* |
| 97 |
* @param string|C|Closure $condition |
| 98 |
* @param string|null $comparisonOperator |
| 99 |
* @param mixed|null $value |
| 100 |
*/ |
| 101 |
public function or($condition, ?string $comparisonOperator = null, $value = null): ConditionSet |
| 102 |
{ |
| 103 |
$this->conditions[] = $this->createCondition($condition, $comparisonOperator, $value, 'or'); |
| 104 |
|
| 105 |
return $this; |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* @param C|Closure|string $condition |
| 110 |
* @param string|null $comparisonOperator |
| 111 |
* @param mixed $value |
| 112 |
* @param string|null $logicalOperator |
| 113 |
* |
| 114 |
* @return Condition|FieldCondition|NestedCondition |
| 115 |
*/ |
| 116 |
private function createCondition($condition, ?string $comparisonOperator = null, $value = null, ?string $logicalOperator = null) |
| 117 |
{ |
| 118 |
$baseConditionClass = static::getBaseConditionClass(); |
| 119 |
if ($condition instanceof $baseConditionClass) { |
| 120 |
return $condition; |
| 121 |
} |
| 122 |
|
| 123 |
if ($condition instanceof Closure) { |
| 124 |
$nestedCondition = new NestedCondition([], $logicalOperator); |
| 125 |
$condition($nestedCondition); |
| 126 |
return $nestedCondition; |
| 127 |
} |
| 128 |
|
| 129 |
return new FieldCondition($condition, $comparisonOperator, $value, $logicalOperator); |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* @since 1.0.0 |
| 134 |
*/ |
| 135 |
public function passes(array $values): bool |
| 136 |
{ |
| 137 |
return array_reduce( |
| 138 |
$this->conditions, |
| 139 |
static function ($carry, Condition $condition) use ($values) { |
| 140 |
return $condition->getLogicalOperator() === 'and' |
| 141 |
? $carry && $condition->passes($values) |
| 142 |
: $carry || $condition->passes($values); |
| 143 |
}, |
| 144 |
true |
| 145 |
); |
| 146 |
} |
| 147 |
|
| 148 |
/** |
| 149 |
* @since 1.0.0 |
| 150 |
*/ |
| 151 |
public function fails(array $values): bool |
| 152 |
{ |
| 153 |
return ! $this->passes($values); |
| 154 |
} |
| 155 |
|
| 156 |
/** |
| 157 |
* Returns the Condition interface/class used as the base for this ConditionSet. By default, this is Condition, |
| 158 |
* but this allows for creating a ConditionSet that only accepts a specific type of Condition. |
| 159 |
* |
| 160 |
* @since 1.0.0 |
| 161 |
* |
| 162 |
* @return class-string<C> |
| 163 |
*/ |
| 164 |
protected static function getBaseConditionClass(): string |
| 165 |
{ |
| 166 |
return Condition::class; |
| 167 |
} |
| 168 |
|
| 169 |
/** |
| 170 |
* Validates the condition based on the base condition class. |
| 171 |
* |
| 172 |
* @since 1.0.0 |
| 173 |
* |
| 174 |
* @param $condition |
| 175 |
* |
| 176 |
* @return void |
| 177 |
*/ |
| 178 |
protected function validateCondition($condition) |
| 179 |
{ |
| 180 |
$baseConditionClass = static::getBaseConditionClass(); |
| 181 |
if ( ! $condition instanceof $baseConditionClass) { |
| 182 |
Config::throwInvalidArgumentException( |
| 183 |
sprintf( |
| 184 |
'Condition must be an instance of %s, %s given.', |
| 185 |
$baseConditionClass, |
| 186 |
is_object($condition) ? get_class($condition) : gettype($condition) |
| 187 |
) |
| 188 |
); |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
/** |
| 193 |
* Validates the conditions based on the base condition class. |
| 194 |
* |
| 195 |
* @since 1.0.0 |
| 196 |
*/ |
| 197 |
protected function validateConditions(array $conditions) |
| 198 |
{ |
| 199 |
foreach ($conditions as $condition) { |
| 200 |
$this->validateCondition($condition); |
| 201 |
} |
| 202 |
} |
| 203 |
} |
| 204 |
|