| @@ 7-177 (lines=171) @@ | ||
| 4 | /** |
|
| 5 | * a >= x |
|
| 6 | */ |
|
| 7 | class AboveOrEqualRule extends OrRule |
|
| 8 | { |
|
| 9 | use Trait_RuleWithField; |
|
| 10 | ||
| 11 | /** @var string operator */ |
|
| 12 | const operator = '>='; |
|
| 13 | ||
| 14 | /** @var mixed minimum */ |
|
| 15 | protected $minimum; |
|
| 16 | ||
| 17 | /** |
|
| 18 | * @param string $field The field to apply the rule on. |
|
| 19 | * @param array $value The value the field can below to. |
|
| 20 | */ |
|
| 21 | public function __construct($field, $minimum, array $options=[]) |
|
| 22 | { |
|
| 23 | if ( ! empty($options)) { |
|
| 24 | $this->setOptions($options); |
|
| 25 | } |
|
| 26 | ||
| 27 | $this->field = $field; |
|
| 28 | $this->minimum = $minimum; |
|
| 29 | } |
|
| 30 | ||
| 31 | /** |
|
| 32 | * @return mixed The minimum for the field of this rule |
|
| 33 | */ |
|
| 34 | public function getMinimum() |
|
| 35 | { |
|
| 36 | return $this->minimum; |
|
| 37 | } |
|
| 38 | ||
| 39 | /** |
|
| 40 | * Defines the minimum of the current rule |
|
| 41 | * |
|
| 42 | * @param mixed $minimum |
|
| 43 | * @return BelowOrEqualRule $this |
|
| 44 | */ |
|
| 45 | public function setMinimum($minimum) |
|
| 46 | { |
|
| 47 | if (null === $minimum) { |
|
| 48 | throw new \InvalidArgumentException( |
|
| 49 | "The minimum of a below or equal rule cannot be null" |
|
| 50 | ); |
|
| 51 | } |
|
| 52 | ||
| 53 | if ($this->minimum == $minimum) { |
|
| 54 | return $this; |
|
| 55 | } |
|
| 56 | ||
| 57 | $this->minimum = $minimum; |
|
| 58 | $this->flushCache(); |
|
| 59 | ||
| 60 | return $this; |
|
| 61 | } |
|
| 62 | ||
| 63 | /** |
|
| 64 | * @return bool |
|
| 65 | */ |
|
| 66 | public function isNormalizationAllowed(array $contextual_options=[]) |
|
| 67 | { |
|
| 68 | return $this->getOption('above_or_equal.normalization', $contextual_options); |
|
| 69 | } |
|
| 70 | ||
| 71 | /** |
|
| 72 | * @return array $operands |
|
| 73 | */ |
|
| 74 | public function getOperands() |
|
| 75 | { |
|
| 76 | if ( ! empty($this->cache['operands'])) { |
|
| 77 | return $this->cache['operands']; |
|
| 78 | } |
|
| 79 | ||
| 80 | $operands = [ |
|
| 81 | new AboveRule($this->field, $this->minimum), |
|
| 82 | new EqualRule($this->field, $this->minimum), |
|
| 83 | ]; |
|
| 84 | ||
| 85 | return $this->cache['operands'] = $operands; |
|
| 86 | } |
|
| 87 | ||
| 88 | /** |
|
| 89 | * Set the minimum and the field of the current instance by giving |
|
| 90 | * an array of opereands as parameter. |
|
| 91 | * |
|
| 92 | * @param array $operands |
|
| 93 | * @return BelowOrEqualRule $this |
|
| 94 | */ |
|
| 95 | public function setOperands(array $operands) |
|
| 96 | { |
|
| 97 | foreach ($operands as $operand) { |
|
| 98 | if ($operand instanceof EqualRule) { |
|
| 99 | $equalRuleField = $operand->getField(); |
|
| 100 | $equalRuleValue = $operand->getValue(); |
|
| 101 | } |
|
| 102 | elseif ($operand instanceof AboveRule) { |
|
| 103 | $aboveRuleField = $operand->getField(); |
|
| 104 | $aboveRuleValue = $operand->getLowerLimit(); |
|
| 105 | } |
|
| 106 | } |
|
| 107 | ||
| 108 | if ( 2 != count($operands) |
|
| 109 | || ! isset($equalRuleValue) |
|
| 110 | || ! isset($aboveRuleValue) |
|
| 111 | || $aboveRuleValue != $equalRuleValue |
|
| 112 | || $aboveRuleField != $equalRuleField |
|
| 113 | ) { |
|
| 114 | throw new \InvalidArgumentException( |
|
| 115 | "Operands must be an array of two rules like (field > minimum || field = minimum) instead of:\n" |
|
| 116 | .var_export($operands, true) |
|
| 117 | ); |
|
| 118 | } |
|
| 119 | ||
| 120 | $this->setMinimum($aboveRuleValue); |
|
| 121 | $this->setField($aboveRuleField); |
|
| 122 | ||
| 123 | return $this; |
|
| 124 | } |
|
| 125 | ||
| 126 | /** |
|
| 127 | * @return array |
|
| 128 | */ |
|
| 129 | public function getValues() |
|
| 130 | { |
|
| 131 | return $this->getMinimum(); |
|
| 132 | } |
|
| 133 | ||
| 134 | /** |
|
| 135 | * @param array $options + show_instance=false Display the operator of the rule or its instance id |
|
| 136 | * |
|
| 137 | * @return array |
|
| 138 | */ |
|
| 139 | public function toArray(array $options=[]) |
|
| 140 | { |
|
| 141 | $default_options = [ |
|
| 142 | 'show_instance' => false, |
|
| 143 | ]; |
|
| 144 | foreach ($default_options as $default_option => &$default_value) { |
|
| 145 | if ( ! isset($options[ $default_option ])) { |
|
| 146 | $options[ $default_option ] = $default_value; |
|
| 147 | } |
|
| 148 | } |
|
| 149 | ||
| 150 | try { |
|
| 151 | // TODO replace this rule by a simple OrRule? |
|
| 152 | return [ |
|
| 153 | $this->getField(), |
|
| 154 | $options['show_instance'] ? $this->getInstanceId() : self::operator, |
|
| 155 | $this->getValues(), |
|
| 156 | ]; |
|
| 157 | } |
|
| 158 | catch (\RuntimeException $e) { |
|
| 159 | return parent::toArray(); |
|
| 160 | } |
|
| 161 | } |
|
| 162 | ||
| 163 | /** |
|
| 164 | */ |
|
| 165 | public function toString(array $options=[]) |
|
| 166 | { |
|
| 167 | if (isset($this->cache['string'])) { |
|
| 168 | return $this->cache['string']; |
|
| 169 | } |
|
| 170 | ||
| 171 | $operator = self::operator; |
|
| 172 | ||
| 173 | return $this->cache['string'] = "['{$this->getField()}', '$operator', " . var_export($this->getValues(), true). "]"; |
|
| 174 | } |
|
| 175 | ||
| 176 | /**/ |
|
| 177 | } |
|
| 178 | ||
| @@ 7-177 (lines=171) @@ | ||
| 4 | /** |
|
| 5 | * a <= x |
|
| 6 | */ |
|
| 7 | class BelowOrEqualRule extends OrRule |
|
| 8 | { |
|
| 9 | use Trait_RuleWithField; |
|
| 10 | ||
| 11 | /** @var string operator */ |
|
| 12 | const operator = '<='; |
|
| 13 | ||
| 14 | /** @var mixed maximum */ |
|
| 15 | protected $maximum; |
|
| 16 | ||
| 17 | /** |
|
| 18 | * @param string $field The field to apply the rule on. |
|
| 19 | * @param array $value The value the field can below to. |
|
| 20 | */ |
|
| 21 | public function __construct($field, $maximum, array $options=[]) |
|
| 22 | { |
|
| 23 | if ( ! empty($options)) { |
|
| 24 | $this->setOptions($options); |
|
| 25 | } |
|
| 26 | ||
| 27 | $this->field = $field; |
|
| 28 | $this->maximum = $maximum; |
|
| 29 | } |
|
| 30 | ||
| 31 | /** |
|
| 32 | * @return mixed The maximum for the field of this rule |
|
| 33 | */ |
|
| 34 | public function getMaximum() |
|
| 35 | { |
|
| 36 | return $this->maximum; |
|
| 37 | } |
|
| 38 | ||
| 39 | /** |
|
| 40 | * Defines the maximum of the current rule |
|
| 41 | * |
|
| 42 | * @param mixed $maximum |
|
| 43 | * @return BelowOrEqualRule $this |
|
| 44 | */ |
|
| 45 | public function setMaximum($maximum) |
|
| 46 | { |
|
| 47 | if (null === $maximum) { |
|
| 48 | throw new \InvalidArgumentException( |
|
| 49 | "The maximum of a below or equal rule cannot be null" |
|
| 50 | ); |
|
| 51 | } |
|
| 52 | ||
| 53 | if ($this->maximum == $maximum) { |
|
| 54 | return $this; |
|
| 55 | } |
|
| 56 | ||
| 57 | $this->maximum = $maximum; |
|
| 58 | $this->flushCache(); |
|
| 59 | ||
| 60 | return $this; |
|
| 61 | } |
|
| 62 | ||
| 63 | /** |
|
| 64 | * @return bool |
|
| 65 | */ |
|
| 66 | public function isNormalizationAllowed(array $contextual_options=[]) |
|
| 67 | { |
|
| 68 | return $this->getOption('below_or_equal.normalization', $contextual_options); |
|
| 69 | } |
|
| 70 | ||
| 71 | /** |
|
| 72 | * @return array $operands |
|
| 73 | */ |
|
| 74 | public function getOperands() |
|
| 75 | { |
|
| 76 | if ( ! empty($this->cache['operands'])) { |
|
| 77 | return $this->cache['operands']; |
|
| 78 | } |
|
| 79 | ||
| 80 | $operands = [ |
|
| 81 | new BelowRule($this->field, $this->maximum), |
|
| 82 | new EqualRule($this->field, $this->maximum), |
|
| 83 | ]; |
|
| 84 | ||
| 85 | return $this->cache['operands'] = $operands; |
|
| 86 | } |
|
| 87 | ||
| 88 | /** |
|
| 89 | * Set the maximum and the field of the current instance by giving |
|
| 90 | * an array of opereands as parameter. |
|
| 91 | * |
|
| 92 | * @param array $operands |
|
| 93 | * @return BelowOrEqualRule $this |
|
| 94 | */ |
|
| 95 | public function setOperands(array $operands) |
|
| 96 | { |
|
| 97 | foreach ($operands as $operand) { |
|
| 98 | if ($operand instanceof EqualRule) { |
|
| 99 | $equalRuleField = $operand->getField(); |
|
| 100 | $equalRuleValue = $operand->getValue(); |
|
| 101 | } |
|
| 102 | elseif ($operand instanceof BelowRule) { |
|
| 103 | $belowRuleField = $operand->getField(); |
|
| 104 | $belowRuleValue = $operand->getUpperLimit(); |
|
| 105 | } |
|
| 106 | } |
|
| 107 | ||
| 108 | if ( 2 != count($operands) |
|
| 109 | || ! isset($equalRuleValue) |
|
| 110 | || ! isset($belowRuleValue) |
|
| 111 | || $belowRuleValue != $equalRuleValue |
|
| 112 | || $belowRuleField != $equalRuleField |
|
| 113 | ) { |
|
| 114 | throw new \InvalidArgumentException( |
|
| 115 | "Operands must be an array of two rules like (field < maximum || field = maximum) instead of:\n" |
|
| 116 | .var_export($operands, true) |
|
| 117 | ); |
|
| 118 | } |
|
| 119 | ||
| 120 | $this->setMaximum($belowRuleValue); |
|
| 121 | $this->setField($belowRuleField); |
|
| 122 | ||
| 123 | return $this; |
|
| 124 | } |
|
| 125 | ||
| 126 | /** |
|
| 127 | * @return array |
|
| 128 | */ |
|
| 129 | public function getValues() |
|
| 130 | { |
|
| 131 | return $this->getMaximum(); |
|
| 132 | } |
|
| 133 | ||
| 134 | /** |
|
| 135 | * @param array $options + show_instance=false Display the operator of the rule or its instance id |
|
| 136 | * |
|
| 137 | * @return array |
|
| 138 | */ |
|
| 139 | public function toArray(array $options=[]) |
|
| 140 | { |
|
| 141 | $default_options = [ |
|
| 142 | 'show_instance' => false, |
|
| 143 | ]; |
|
| 144 | foreach ($default_options as $default_option => &$default_value) { |
|
| 145 | if ( ! isset($options[ $default_option ])) { |
|
| 146 | $options[ $default_option ] = $default_value; |
|
| 147 | } |
|
| 148 | } |
|
| 149 | ||
| 150 | try { |
|
| 151 | // TODO replace this rule by a simple OrRule? |
|
| 152 | return [ |
|
| 153 | $this->getField(), |
|
| 154 | $options['show_instance'] ? $this->getInstanceId() : self::operator, |
|
| 155 | $this->getValues(), |
|
| 156 | ]; |
|
| 157 | } |
|
| 158 | catch (\RuntimeException $e) { |
|
| 159 | return parent::toArray(); |
|
| 160 | } |
|
| 161 | } |
|
| 162 | ||
| 163 | /** |
|
| 164 | */ |
|
| 165 | public function toString(array $options=[]) |
|
| 166 | { |
|
| 167 | if (isset($this->cache['string'])) { |
|
| 168 | return $this->cache['string']; |
|
| 169 | } |
|
| 170 | ||
| 171 | $operator = self::operator; |
|
| 172 | ||
| 173 | return $this->cache['string'] = "['{$this->getField()}', '$operator', " . var_export($this->getValues(), true). "]"; |
|
| 174 | } |
|
| 175 | ||
| 176 | /**/ |
|
| 177 | } |
|
| 178 | ||