flexible-shipping
/
src
/
WPDesk
/
FS
/
TableRate
/
ShippingMethod
/
FreeShippingThresholdRuleValidator.php
BlockEditing
2 weeks ago
Convert
2 weeks ago
Duplicate
2 weeks ago
Management
2 weeks ago
Timestamps
2 weeks ago
Tracker
2 weeks ago
views
2 weeks ago
CommonMethodSettings.php
2 weeks ago
FreeShippingCalculator.php
2 weeks ago
FreeShippingThresholdRuleValidator.php
2 weeks ago
MethodDescription.php
2 weeks ago
MethodLogo.php
2 weeks ago
MethodLogoCheckoutBlocksAssets.php
2 weeks ago
MethodLogoSettingsField.php
2 weeks ago
MethodSettings.php
2 weeks ago
MethodTitle.php
2 weeks ago
RateCalculator.php
2 weeks ago
RateCalculatorFactory.php
2 weeks ago
SettingsDisplayPreparer.php
2 weeks ago
SettingsProcessor.php
2 weeks ago
SingleMethodSettings.php
2 weeks ago
FreeShippingThresholdRuleValidator.php
110 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Class FreeShippingThresholdRuleValidator |
| 4 | * |
| 5 | * @package WPDesk\FS\TableRate\ShippingMethod |
| 6 | */ |
| 7 | |
| 8 | declare( strict_types=1 ); |
| 9 | |
| 10 | namespace WPDesk\FS\TableRate\ShippingMethod; |
| 11 | |
| 12 | use WPDesk\FS\TableRate\Rule\Condition\Price; |
| 13 | use WPDesk\FS\TableRate\Rule\Condition\None; |
| 14 | |
| 15 | /** |
| 16 | * Validates if free shipping threshold is covered by price rules. |
| 17 | */ |
| 18 | final class FreeShippingThresholdRuleValidator { |
| 19 | |
| 20 | /** |
| 21 | * @param mixed $threshold Free shipping threshold after WooCommerce formatting. |
| 22 | * @param array $rules Normalized method rules. |
| 23 | * |
| 24 | * @return bool |
| 25 | */ |
| 26 | public function is_valid( $threshold, array $rules ): bool { |
| 27 | if ( '' === $threshold || ! is_numeric( $threshold ) ) { |
| 28 | return true; |
| 29 | } |
| 30 | |
| 31 | $threshold = (float) $threshold; |
| 32 | |
| 33 | foreach ( $rules as $rule ) { |
| 34 | if ( ! is_array( $rule ) || $this->is_deleted( $rule ) ) { |
| 35 | continue; |
| 36 | } |
| 37 | |
| 38 | if ( $this->rule_covers_threshold( $threshold, $rule ) ) { |
| 39 | return true; |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | return false; |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * @param float $threshold Free shipping threshold. |
| 48 | * @param array $rule Rule settings. |
| 49 | * |
| 50 | * @return bool |
| 51 | */ |
| 52 | private function rule_covers_threshold( float $threshold, array $rule ): bool { |
| 53 | if ( ! isset( $rule['conditions'] ) || ! is_array( $rule['conditions'] ) ) { |
| 54 | return false; |
| 55 | } |
| 56 | |
| 57 | foreach ( $rule['conditions'] as $condition ) { |
| 58 | if ( ! is_array( $condition ) || $this->is_deleted( $condition ) ) { |
| 59 | continue; |
| 60 | } |
| 61 | |
| 62 | if ( ( $condition['condition_id'] ?? '' ) === None::CONDITION_ID ) { |
| 63 | return true; |
| 64 | } |
| 65 | |
| 66 | if ( ( $condition['condition_id'] ?? '' ) !== Price::CONDITION_ID ) { |
| 67 | continue; |
| 68 | } |
| 69 | |
| 70 | if ( $this->condition_covers_threshold( $threshold, $condition ) ) { |
| 71 | return true; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | return false; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * @param float $threshold Free shipping threshold. |
| 80 | * @param array $condition Price condition settings. |
| 81 | * |
| 82 | * @return bool |
| 83 | */ |
| 84 | private function condition_covers_threshold( float $threshold, array $condition ): bool { |
| 85 | $min = $condition[ Price::MIN ] ?? ''; |
| 86 | $max = $condition[ Price::MAX ] ?? ''; |
| 87 | |
| 88 | if ( '' !== $min && ! is_numeric( $min ) ) { |
| 89 | return false; |
| 90 | } |
| 91 | |
| 92 | if ( '' !== $max && ! is_numeric( $max ) ) { |
| 93 | return false; |
| 94 | } |
| 95 | |
| 96 | $min = '' === $min ? 0.0 : (float) $min; |
| 97 | |
| 98 | return $min <= $threshold && ( '' === $max || (float) $max >= $threshold ); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * @param array $settings Rule or condition settings. |
| 103 | * |
| 104 | * @return bool |
| 105 | */ |
| 106 | private function is_deleted( array $settings ): bool { |
| 107 | return filter_var( $settings['deleted'] ?? false, FILTER_VALIDATE_BOOLEAN ); |
| 108 | } |
| 109 | } |
| 110 |