| 1 |
<?php |
| 2 |
|
| 3 |
namespace WPO\WC\PostNL\Collections; |
| 4 |
|
| 5 |
use MyParcelNL\Sdk\src\Support\Collection; |
| 6 |
use WC_Tax; |
| 7 |
use WPO\WC\PostNL\Entity\Setting; |
| 8 |
|
| 9 |
defined('ABSPATH') or exit; |
| 10 |
|
| 11 |
if (class_exists('\\WPO\\WC\\PostNL\\Collections\\SettingsCollection')) { |
| 12 |
return; |
| 13 |
} |
| 14 |
|
| 15 |
/** |
| 16 |
* @mixin Setting |
| 17 |
*/ |
| 18 |
|
| 19 |
/** |
| 20 |
* @property mixed getByName |
| 21 |
*/ |
| 22 |
class SettingsCollection extends Collection |
| 23 |
{ |
| 24 |
/** |
| 25 |
* @param array $rawSettings |
| 26 |
* @param string $type |
| 27 |
* @param string $carrierName |
| 28 |
*/ |
| 29 |
public function setSettingsByType(array $rawSettings = [], string $type = "", string $carrierName = null) |
| 30 |
{ |
| 31 |
foreach ($rawSettings as $name => $value) { |
| 32 |
$setting = new Setting($name, $value, $type, $carrierName); |
| 33 |
$this->push($setting); |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Check if a setting is enabled |
| 39 |
* |
| 40 |
* @param string $name |
| 41 |
* |
| 42 |
* @return bool |
| 43 |
*/ |
| 44 |
public function isEnabled(string $name): bool |
| 45 |
{ |
| 46 |
return $this->getByName($name) ?? false; |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Search for a setting by name and value. |
| 51 |
* |
| 52 |
* @param string $name |
| 53 |
* @param string $value |
| 54 |
* |
| 55 |
* @return SettingsCollection |
| 56 |
*/ |
| 57 |
public function like(string $name, string $value): self |
| 58 |
{ |
| 59 |
return $this->filter( |
| 60 |
function (Setting $item) use ($name, $value) { |
| 61 |
return false !== strpos($item->name, $value); |
| 62 |
} |
| 63 |
); |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* @param string $name |
| 68 |
* |
| 69 |
* @return mixed|null |
| 70 |
*/ |
| 71 |
public function getByName(string $name) |
| 72 |
{ |
| 73 |
/** @var Setting $setting */ |
| 74 |
$setting = $this->where('name', $name)->first(); |
| 75 |
|
| 76 |
return $setting->value ?? null; |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* @param string $name |
| 81 |
* |
| 82 |
* @return bool |
| 83 |
*/ |
| 84 |
public function exists(string $name): bool |
| 85 |
{ |
| 86 |
/** @var Setting $setting */ |
| 87 |
$setting = $this->where('name', $name)->first(); |
| 88 |
|
| 89 |
return (bool) $setting->value; |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* @param string $name |
| 94 |
* |
| 95 |
* @return int |
| 96 |
*/ |
| 97 |
public function getIntegerByName(string $name): int |
| 98 |
{ |
| 99 |
return (int) ($this->getByName($name) ?? 0); |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* @param string $name |
| 104 |
* |
| 105 |
* @return float |
| 106 |
*/ |
| 107 |
public function getFloatByName(string $name): float |
| 108 |
{ |
| 109 |
$value = str_replace(',', '.', $this->getByName($name)); |
| 110 |
return (float) ($value ?? 0); |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* @param string $name |
| 115 |
* |
| 116 |
* @return string |
| 117 |
*/ |
| 118 |
public function getStringByName(string $name): string |
| 119 |
{ |
| 120 |
return $this->getByName($name) ?? ''; |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* If prices are displayed with VAT/tax included, add the taxes to the price. Otherwise just pass the base price |
| 125 |
* as all taxes will be combined and shown in separate "Tax" fees. |
| 126 |
* |
| 127 |
* Never use this as a base price, this is only for displaying a total price including taxes. |
| 128 |
* |
| 129 |
* @param string $name |
| 130 |
* |
| 131 |
* @return float |
| 132 |
*/ |
| 133 |
public function getPriceByName(string $name): float |
| 134 |
{ |
| 135 |
$basePrice = $this->getFloatByName($name); |
| 136 |
$displayIncludingTax = WC()->cart->display_prices_including_tax(); |
| 137 |
|
| 138 |
if ($displayIncludingTax) { |
| 139 |
$taxRates = WC_Tax::get_shipping_tax_rates(); |
| 140 |
$taxes = WC_Tax::calc_shipping_tax($basePrice, $taxRates); |
| 141 |
$sumTaxes = array_sum($taxes); |
| 142 |
|
| 143 |
return $basePrice + $sumTaxes; |
| 144 |
} |
| 145 |
|
| 146 |
return $basePrice; |
| 147 |
} |
| 148 |
} |
| 149 |
|