1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DiceCalc; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class CalcSet |
7
|
|
|
* |
8
|
|
|
* @package DiceCalc |
9
|
|
|
* @author Owen Winkler <[email protected]> |
10
|
|
|
* @license MIT http://opensource.org/licenses/MIT |
11
|
|
|
*/ |
12
|
|
|
class CalcSet { |
13
|
|
|
protected $values = []; |
14
|
|
|
protected $label; |
15
|
|
|
|
16
|
|
|
public function __construct($set_value) { |
17
|
|
|
if (is_array($set_value)) { |
18
|
|
|
$this->values = $set_value; |
19
|
|
|
$this->saved_values = $this->values; |
|
|
|
|
20
|
|
|
$this->label = '[' . implode(', ', $set_value) . ']'; |
21
|
|
|
} else { |
22
|
|
|
$this->label = $set_value; |
23
|
|
|
preg_match('%^(?P<multiple>\d*)\[(?P<set>[^\]]+)\]$%i', $set_value, $matches); |
24
|
|
|
$set_value = $matches['set']; |
25
|
|
|
$values = explode(',', $set_value); |
26
|
|
|
for ($z = 0; $z < max(1, intval($matches['multiple'])); $z ++) { |
|
|
|
|
27
|
|
|
foreach ($values as $set_value) { |
28
|
|
|
$this->values[] = $set_value; |
29
|
|
|
} |
30
|
|
|
} |
31
|
|
|
$this->saved_values = $this->values; |
|
|
|
|
32
|
|
|
foreach ($this->values as $k => $set_value) { |
33
|
|
|
$calc = new Calc($set_value); |
34
|
|
|
$this->values[$k] = $calc(); |
35
|
|
|
} |
36
|
|
|
foreach ($this->values as $k => $value) { |
37
|
|
|
$calc = new Calc($value); |
38
|
|
|
$this->values[$k] = $calc(); |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function __toString() { |
44
|
|
|
$out = []; |
45
|
|
|
foreach (array_keys($this->saved_values) as $key) { |
|
|
|
|
46
|
|
|
$vout = $this->values[$key]; |
47
|
|
|
if (!isset($this->values[$key])) { |
48
|
|
|
$vout = $this->saved_values[$key]; |
|
|
|
|
49
|
|
|
} |
50
|
|
|
if ($vout === true) { |
51
|
|
|
$vout = '<span class="true">true</span>'; |
52
|
|
|
} |
53
|
|
|
if ($vout === false) { |
54
|
|
|
$vout = '<span class="false">false</span>'; |
55
|
|
|
} |
56
|
|
|
if (isset($this->values[$key])) { |
57
|
|
|
$out[] = $vout; |
58
|
|
|
} else { |
59
|
|
|
$out[] = '<s>' . $vout . '</s>'; |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
$out = '[' . implode(', ', $out) . ']'; |
63
|
|
|
|
64
|
|
|
return $out; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
View Code Duplication |
public function calc($operator, $operand) { |
|
|
|
|
68
|
|
|
$out = []; |
69
|
|
|
foreach ($this->values as $value) { |
70
|
|
|
$out[] = CalcOperation::calc($operator, $operand, $value); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return new CalcSet($out); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
View Code Duplication |
public function rcalc($operator, $operand) { |
|
|
|
|
77
|
|
|
$out = []; |
78
|
|
|
foreach ($this->values as $value) { |
79
|
|
|
$out[] = CalcOperation::calc($operator, $value, $operand); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return new CalcSet($out); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
View Code Duplication |
public function mcalc($operator, $operand) |
|
|
|
|
86
|
|
|
{ |
87
|
|
|
$out = []; |
88
|
|
|
foreach ($this->values as $value) { |
89
|
|
|
$out[] = $operand->rcalc($operator, $value); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return new CalcSet($out); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function value() |
96
|
|
|
{ |
97
|
|
|
$allnumeric = true; |
98
|
|
|
foreach ($this->values as $v) { |
99
|
|
|
if (is_numeric($v)) { |
|
|
|
|
100
|
|
|
} else { |
101
|
|
|
$allnumeric = false; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
if ($allnumeric) { |
106
|
|
|
return array_sum($this->values); |
107
|
|
|
} else { |
108
|
|
|
return $this; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.
If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.