/var/www/html/wp-content/plugins/wp-smtp/vendor/stellarwp/field-conditions/src/FieldCondition.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php

namespace StellarWP\FieldConditions;

use 
InvalidArgumentException;
use 
StellarWP\FieldConditions\Concerns\HasLogicalOperator;
use 
StellarWP\FieldConditions\Contracts\Condition;

/**
 * @since 1.0.0
 */
class FieldCondition implements Condition
{
    use 
HasLogicalOperator;

    const 
COMPARISON_OPERATORS = ['=''!=''>''>=''<''<=''contains''not_contains'];

    
/** @var string */
    
const TYPE 'basic';

    
/** @var string */
    
protected $field;

    
/** @var mixed */
    
protected $value;

    
/** @var string */
    
protected $comparisonOperator;

    protected 
$strict false;

    
/**
     * Create a new FieldCondition.
     *
     * @since 1.0.0
     *
     * @param mixed $value
     */
    
public function __construct(string $fieldstring $comparisonOperator$valuestring $logicalOperator 'and')
    {
        if (
$this->isInvalidComparisonOperator($comparisonOperator)) {
            throw 
Config::throwInvalidArgumentException(
                
"Invalid comparison operator: $comparisonOperator. Must be one of: " implode(
                    
', ',
                    
self::COMPARISON_OPERATORS
                
)
            );
        }

        
$this->field $field;
        
$this->comparisonOperator $comparisonOperator;
        
$this->value $value;
        
$this->setLogicalOperator($logicalOperator);
    }

    
/**
     * @since 1.0.0
     *
     * @inheritDoc
     */
    
public function jsonSerialize(): array
    {
        return [
            
'type' => static::TYPE,
            
'field' => $this->field,
            
'value' => $this->value,
            
'strictComparison' => $this->strict,
            
'comparisonOperator' => $this->comparisonOperator,
            
'logicalOperator' => $this->logicalOperator,
        ];
    }

    
/**
     * @since 1.0.0
     */
    
public function passes(array $values): bool
    
{
        if (!isset(
$values[$this->field])) {
            throw new 
InvalidArgumentException("Field {$this->field} not found in test values.");
        }

        
$testValue $values[$this->field];

        if (
$this->strict && $this->comparisonOperator !== '!=' && gettype($testValue) !== gettype($this->value)) {
            return 
false;
        }

        switch (
$this->comparisonOperator) {
            case 
'=':
                return 
$testValue == $this->value;
            case 
'!=':
                return 
$this->strict $testValue !== $this->value $testValue != $this->value;
            case 
'>':
                return 
$testValue $this->value;
            case 
'>=':
                return 
$testValue >= $this->value;
            case 
'<':
                return 
$testValue $this->value;
            case 
'<=':
                return 
$testValue <= $this->value;
            case 
'contains':
                return (
'' == $this->value || false !== strpos($testValue$this->value));
            case 
'not_contains':
                return (
'' == $this->value || false === strpos($testValue$this->value));
        }
    }

    
/**
     * @inheritDoc
     */
    
public function fails(array $values): bool
    
{
        return !
$this->passes($values);
    }

    
/**
     * Sets the condition to strict mode.
     *
     * @since 1.1.0
     */
    
public function strict(bool $strict true): self
    
{
        
$this->strict $strict;

        return 
$this;
    }

    
/**
     * Check if the provided operator is invalid.
     *
     * @since 1.0.0
     */
    
protected function isInvalidComparisonOperator(string $operator): bool
    
{
        return !
in_array($operator, static::COMPARISON_OPERATORStrue);
    }

    
/**
     * Check if the provided boolean is invalid.
     *
     * @since 1.0.0
     */
    
protected function isInvalidLogicalOperator(string $operator): bool
    
{
        return !
in_array($operatorCondition::LOGICAL_OPERATORStrue);
    }
}