← All changes
|
src/FormBuilder/BlockModels/DonationAmountBlockModel.php
+68
-0
4.15.3
→
4.16.9
View file →
| @@ -223,5 +223,73 @@ | ||
| 223 | 223 | public function getSetPrice(): int |
| 224 | 224 | { |
| 225 | 225 | return $this->block->getAttribute('setPrice'); |
| 226 | 226 | } |
| 227 | + | |
| 228 | + /** | |
| 229 | + * The lowest amount a donor may type into the custom amount input, or zero when the block does not | |
| 230 | + * define one. | |
| 231 | + * | |
| 232 | + * @since 4.16.8 | |
| 233 | + */ | |
| 234 | + public function getCustomAmountMin(): float | |
| 235 | + { | |
| 236 | + return $this->hasAttribute('customAmountMin') ? (float)$this->getAttribute('customAmountMin') : 0.0; | |
| 237 | + } | |
| 238 | + | |
| 239 | + /** | |
| 240 | + * The highest amount a donor may type into the custom amount input, or zero when the block does not | |
| 241 | + * define one. | |
| 242 | + * | |
| 243 | + * @since 4.16.8 | |
| 244 | + */ | |
| 245 | + public function getCustomAmountMax(): float | |
| 246 | + { | |
| 247 | + return $this->hasAttribute('customAmountMax') ? (float)$this->getAttribute('customAmountMax') : 0.0; | |
| 248 | + } | |
| 249 | + | |
| 250 | + /** | |
| 251 | + * The amounts the admin configured on the block: the donation levels, or the fixed set price. The | |
| 252 | + * custom amount minimum and maximum never apply to these. | |
| 253 | + * | |
| 254 | + * @since 4.16.8 | |
| 255 | + * | |
| 256 | + * @return float[] | |
| 257 | + */ | |
| 258 | + public function getAdminDefinedAmounts(): array | |
| 259 | + { | |
| 260 | + $amounts = $this->getPriceOption() === 'multi' | |
| 261 | + ? array_column($this->getLevels(), 'value') | |
| 262 | + : [$this->getSetPrice()]; | |
| 263 | + | |
| 264 | + // An unset level or set price sanitizes to 0, which must never be exempt from the minimum. | |
| 265 | + return array_values( | |
| 266 | + array_filter( | |
| 267 | + array_map('floatval', $amounts), | |
| 268 | + static function (float $amount): bool { | |
| 269 | + return $amount > 0; | |
| 270 | + } | |
| 271 | + ) | |
| 272 | + ); | |
| 273 | + } | |
| 274 | + | |
| 275 | + /** | |
| 276 | + * The custom amount minimum, falling back to the lowest amount the admin configured. Without that | |
| 277 | + * fallback a form that leaves the minimum empty accepts any amount, which is a donation spam and card | |
| 278 | + * testing vector. | |
| 279 | + * | |
| 280 | + * @since 4.16.8 | |
| 281 | + * | |
| 282 | + * @return float|null Null when the block defines neither a minimum nor an amount to derive one from. | |
| 283 | + */ | |
| 284 | + public function getMinimumAmount(): ?float | |
| 285 | + { | |
| 286 | + if ($this->isCustomAmountEnabled() && $this->getCustomAmountMin() > 0) { | |
| 287 | + return $this->getCustomAmountMin(); | |
| 288 | + } | |
| 289 | + | |
| 290 | + $adminDefinedAmounts = $this->getAdminDefinedAmounts(); | |
| 291 | + $lowestAmount = $adminDefinedAmounts ? min($adminDefinedAmounts) : 0.0; | |
| 292 | + | |
| 293 | + return $lowestAmount > 0 ? $lowestAmount : null; | |
| 294 | + } | |
| 227 | 295 | } |