flexible-shipping
/
vendor_prefixed
/
wpdesk
/
wp-wpdesk-fs-table-rate
/
src
/
Weight
/
Rounding.php
flexible-shipping
/
vendor_prefixed
/
wpdesk
/
wp-wpdesk-fs-table-rate
/
src
/
Weight
Last commit date
Rounding.php
1 week ago
Rounding.php
66 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Rounding. |
| 5 | * |
| 6 | * @package WPDesk\FS\TableRate |
| 7 | */ |
| 8 | namespace FSVendor\WPDesk\FS\TableRate\Weight; |
| 9 | |
| 10 | use FSVendor\WPDesk\FS\TableRate\Settings\RuleSettings; |
| 11 | /** |
| 12 | * Can compute rounding precision from Flexible Shipping rules. |
| 13 | */ |
| 14 | class Rounding |
| 15 | { |
| 16 | /** |
| 17 | * @var array |
| 18 | */ |
| 19 | private $shipping_method_rules; |
| 20 | /** |
| 21 | * WeightRounding constructor. |
| 22 | * |
| 23 | * @param RuleSettings[] $shipping_method_rules . |
| 24 | */ |
| 25 | public function __construct(array $shipping_method_rules) |
| 26 | { |
| 27 | $this->shipping_method_rules = $shipping_method_rules; |
| 28 | } |
| 29 | /** |
| 30 | * @return int |
| 31 | */ |
| 32 | public function get_rounding_from_rules() |
| 33 | { |
| 34 | $rounding = 0; |
| 35 | foreach ($this->shipping_method_rules as $rule) { |
| 36 | $rounding = max($rounding, $this->get_rounding_from_rule($rule)); |
| 37 | } |
| 38 | return $rounding; |
| 39 | } |
| 40 | /** |
| 41 | * @param RuleSettings $rule . |
| 42 | * |
| 43 | * @return int |
| 44 | */ |
| 45 | private function get_rounding_from_rule($rule) |
| 46 | { |
| 47 | if ($rule->is_based_on_weight()) { |
| 48 | return max($this->get_rounding_from_value($rule->get_min()), $this->get_rounding_from_value($rule->get_max())); |
| 49 | } |
| 50 | return 0; |
| 51 | } |
| 52 | /** |
| 53 | * @param string $value String representation for float. |
| 54 | * |
| 55 | * @return int |
| 56 | */ |
| 57 | private function get_rounding_from_value($value) |
| 58 | { |
| 59 | $parts = explode('.', $value); |
| 60 | if (isset($parts[1])) { |
| 61 | return strlen($parts[1]); |
| 62 | } |
| 63 | return 0; |
| 64 | } |
| 65 | } |
| 66 |