1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is a part of "Axessors" library. |
4
|
|
|
* |
5
|
|
|
* @author <[email protected]> |
6
|
|
|
* @license GPL |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace NoOne4rever\Axessors; |
10
|
|
|
|
11
|
|
|
use NoOne4rever\Axessors\Exceptions\TypeError; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class ConditionsSuit. |
15
|
|
|
* |
16
|
|
|
* Processes Axessors conditions. |
17
|
|
|
* |
18
|
|
|
* @package NoOne4rever\Axessors |
19
|
|
|
*/ |
20
|
|
|
class ConditionsRunner extends RunningSuit |
21
|
|
|
{ |
22
|
|
|
/** @var string method name */ |
23
|
|
|
private $method; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* ConditionsSuit constructor. |
27
|
|
|
* |
28
|
|
|
* @param int $mode mode of execution |
29
|
|
|
* @param PropertyData $data property data |
30
|
|
|
* @param string $class class |
31
|
|
|
* @param string $method method name |
32
|
|
|
* @param object|null $object object |
33
|
|
|
*/ |
34
|
82 |
|
public function __construct(int $mode, PropertyData $data, string $class, string $method, $object = null) |
35
|
|
|
{ |
36
|
82 |
|
parent::__construct($mode, $data, $class, $object); |
37
|
82 |
|
$this->method = $method; |
38
|
82 |
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Casts the property to integer. |
42
|
|
|
* |
43
|
|
|
* If the property is string or array returns it's length. |
44
|
|
|
* If the property is integer of float returns the property itself. |
45
|
|
|
* |
46
|
|
|
* @param $value mixed value of the property |
47
|
|
|
* @return int integer value of the property |
48
|
|
|
* @throws TypeError if the property can't be turned into integer |
49
|
|
|
*/ |
50
|
32 |
|
public static function count($value): int |
51
|
|
|
{ |
52
|
32 |
|
switch (gettype($value)) { |
53
|
32 |
|
case 'integer': |
54
|
7 |
|
case 'double': |
55
|
25 |
|
break; |
56
|
7 |
|
case 'string': |
57
|
5 |
|
$value = strlen($value); |
58
|
5 |
|
break; |
59
|
2 |
|
case 'array': |
60
|
1 |
|
$value = count($value); |
61
|
1 |
|
break; |
62
|
|
|
default: |
63
|
1 |
|
throw new TypeError('value of ' . var_export($value, true) . ' is not countable'); |
64
|
|
|
} |
65
|
31 |
|
return $value; |
|
|
|
|
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Checks the conditions defined in the Axessors comment. |
70
|
|
|
* |
71
|
|
|
* Creates logical tree of the conditions and then checks if the general result is true. |
72
|
|
|
* |
73
|
|
|
* @param $var mixed value of the property |
74
|
|
|
* @return bool result of checking of the conditions |
75
|
|
|
*/ |
76
|
82 |
|
public function processConditions($var): bool |
77
|
|
|
{ |
78
|
82 |
|
if ($this->mode == RunningSuit::INPUT_MODE) { |
79
|
64 |
|
$conditions = $this->propertyData->getInputConditions(); |
80
|
|
|
} else { |
81
|
18 |
|
$conditions = $this->propertyData->getOutputConditions(); |
82
|
|
|
} |
83
|
82 |
|
return $this->executeInjectedString($conditions, $var, $this->mode); |
|
|
|
|
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Executes conditions. |
88
|
|
|
* |
89
|
|
|
* @param string $expr conditions expression |
90
|
|
|
* @param mixed $var value to process |
91
|
|
|
* @param bool $mode mode of execution defined in RunningSuit |
92
|
|
|
* @return bool the result of condition |
93
|
|
|
*/ |
94
|
82 |
|
protected function executeInjectedString(string $expr, $var, bool $mode): bool |
95
|
|
|
{ |
96
|
82 |
|
if (is_null($this->object)) { |
97
|
1 |
|
return call_user_func("{$this->class}::__axessorsExecuteStatic", $expr, $var, $mode); |
98
|
|
|
} else { |
99
|
81 |
|
return $this->object->__axessorsExecute($expr, $var, $mode); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
} |