| 1 |
<?php |
| 2 |
|
| 3 |
namespace Depicter\Rules\Condition; |
| 4 |
|
| 5 |
use Averta\Core\Utility\Str; |
| 6 |
|
| 7 |
class Base { |
| 8 |
|
| 9 |
/** |
| 10 |
* @var string |
| 11 |
*/ |
| 12 |
public $id; |
| 13 |
|
| 14 |
/** |
| 15 |
* |
| 16 |
* @var string |
| 17 |
*/ |
| 18 |
public $slug; |
| 19 |
|
| 20 |
/** |
| 21 |
* Control type 'multiSelect', 'remoteMultiSelect', 'dropdown' |
| 22 |
* |
| 23 |
* @var string |
| 24 |
*/ |
| 25 |
public $control; |
| 26 |
|
| 27 |
/** |
| 28 |
* Selection mode of condition |
| 29 |
* |
| 30 |
* @var string |
| 31 |
*/ |
| 32 |
public $selectionMode = 'include'; |
| 33 |
|
| 34 |
/** |
| 35 |
* Value of this condition |
| 36 |
* |
| 37 |
* @var array |
| 38 |
*/ |
| 39 |
public $value = []; |
| 40 |
|
| 41 |
/** |
| 42 |
* Tier of this condition |
| 43 |
* |
| 44 |
* @var string |
| 45 |
*/ |
| 46 |
protected $tier = 'pro-user'; |
| 47 |
|
| 48 |
/** |
| 49 |
* Whether the condition is queryable or not |
| 50 |
* |
| 51 |
* @var bool |
| 52 |
*/ |
| 53 |
protected $queryable = false; |
| 54 |
|
| 55 |
/** |
| 56 |
* Default value |
| 57 |
* |
| 58 |
* @var array|string |
| 59 |
*/ |
| 60 |
protected $defaultValue = ["all"]; |
| 61 |
|
| 62 |
/** |
| 63 |
* Group id that condition belongs to |
| 64 |
* |
| 65 |
* @var string |
| 66 |
*/ |
| 67 |
protected $belongsTo = ''; |
| 68 |
|
| 69 |
/** |
| 70 |
* Post type |
| 71 |
* |
| 72 |
* @var string |
| 73 |
*/ |
| 74 |
protected $postType = 'post'; |
| 75 |
|
| 76 |
/** |
| 77 |
* Maximum number of allowed options for the control |
| 78 |
* Only useful for conditions having query option enabled |
| 79 |
* |
| 80 |
* @var int |
| 81 |
*/ |
| 82 |
protected $maxOptionsNumber = 2000; |
| 83 |
|
| 84 |
|
| 85 |
/** |
| 86 |
* Whether current condition is queryable or not |
| 87 |
* |
| 88 |
* @return bool |
| 89 |
*/ |
| 90 |
public function queryable(){ |
| 91 |
return $this->queryable; |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* Condition label |
| 96 |
* |
| 97 |
* @return string |
| 98 |
*/ |
| 99 |
public function getLabel(){ |
| 100 |
return ''; |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Condition description |
| 105 |
* |
| 106 |
* @return string|null |
| 107 |
*/ |
| 108 |
public function getDescription(){ |
| 109 |
return null; |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Retrieves control options |
| 114 |
* |
| 115 |
* @return array |
| 116 |
*/ |
| 117 |
public function getControlOptions(){ |
| 118 |
$options = [ |
| 119 |
'defaultValue' => $this->defaultValue |
| 120 |
]; |
| 121 |
if( $this->queryable() ){ |
| 122 |
$options['query'] = $this->slug; |
| 123 |
} |
| 124 |
|
| 125 |
return $options; |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* Retrieves properties which can be exposed |
| 130 |
* |
| 131 |
* @return array |
| 132 |
*/ |
| 133 |
public function getProperties(){ |
| 134 |
$properties = [ |
| 135 |
'slug' => $this->slug, |
| 136 |
'label' => $this->getLabel(), |
| 137 |
'control' => $this->control, |
| 138 |
'tier' => $this->tier, |
| 139 |
'controlOptions' => $this->getControlOptions() |
| 140 |
]; |
| 141 |
if( $this->getDescription() ){ |
| 142 |
$properties['description'] = $this->getDescription(); |
| 143 |
} |
| 144 |
|
| 145 |
return $properties; |
| 146 |
} |
| 147 |
|
| 148 |
/** |
| 149 |
* Get query results for dynamic options of this condition |
| 150 |
* |
| 151 |
* @return array|null |
| 152 |
*/ |
| 153 |
public function getQueryResults(){ |
| 154 |
return null; |
| 155 |
} |
| 156 |
|
| 157 |
/** |
| 158 |
* Check if the condition is qualified or not |
| 159 |
* |
| 160 |
* @param $value |
| 161 |
* |
| 162 |
* @return bool |
| 163 |
*/ |
| 164 |
public function check( $value = null ){ |
| 165 |
return false; |
| 166 |
} |
| 167 |
|
| 168 |
/** |
| 169 |
* Compare two variables |
| 170 |
* |
| 171 |
* @param $var |
| 172 |
* @param $operator |
| 173 |
* @param $value |
| 174 |
* |
| 175 |
* @return bool |
| 176 |
*/ |
| 177 |
public function compare( $var, $operator, $value ): bool{ |
| 178 |
switch( $operator ) { |
| 179 |
case 'equal': |
| 180 |
return $var == $value; |
| 181 |
case 'greaterThan': |
| 182 |
return (float) $var > (float) $value; |
| 183 |
case 'lowerThan': |
| 184 |
return (float) $var < (float) $value; |
| 185 |
case 'greaterThanOrEqual': |
| 186 |
return (float) $var >= (float) $value; |
| 187 |
case 'lowerThanOrEqual': |
| 188 |
return (float) $var <= (float) $value; |
| 189 |
case 'containing': |
| 190 |
return false !== strpos( $var, $value ); |
| 191 |
case 'notContaining': |
| 192 |
return false === strpos( $var, $value ); |
| 193 |
case 'endsWith': |
| 194 |
return Str::ends( $var, $value ); |
| 195 |
case 'beginsWith': |
| 196 |
return Str::starts( $var, $value ); |
| 197 |
case 'regExpMatch': |
| 198 |
return (bool) preg_match( "/$value/", $var ); |
| 199 |
case 'oneOf': |
| 200 |
$lines = explode( '\n', $value ); |
| 201 |
return in_array( $var, $lines); |
| 202 |
case 'nonOf': |
| 203 |
$lines = explode( '\n', $value ); |
| 204 |
return ! in_array( $var, $lines); |
| 205 |
default: |
| 206 |
return false; |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
/** |
| 211 |
* Check if condition is visible or not |
| 212 |
* |
| 213 |
* @return boolean |
| 214 |
*/ |
| 215 |
public function isVisible() { |
| 216 |
return true; |
| 217 |
} |
| 218 |
} |
| 219 |
|