PluginProbe
PostNL for WooCommerce / 4.0.0
PostNL for WooCommerce v4.0.0
5.9.12 5.9.11 5.9.10 5.9.9 5.9.8 5.9.7 5.9.6 trunk 2.5.0 2.5.1 2.5.2 2.5.3 2.5.4 2.5.5 3.1.4 3.1.5 3.1.6 3.1.7 4.0.0 4.0.1 4.0.2 4.3.2 4.3.3 4.4.0 4.4.1 All 72 releases
woo-postnl / includes / collections / settings-collection.php

settings-collection.php in PostNL for WooCommerce 4.0.0, at includes/collections/settings-collection.php

122 lines 2.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace WPO\WC\PostNL\Collections;
4
5 use MyParcelNL\Sdk\src\Support\Collection;
6 use WPO\WC\PostNL\Entity\Setting;
7
8 defined('ABSPATH') or exit;
9
10 if (class_exists('\\WPO\\WC\\PostNL\\Collections\\SettingsCollection')) {
11 return;
12 }
13
14 /**
15 * @mixin Setting
16 */
17
18 /**
19 * @property mixed getByName
20 */
21 class SettingsCollection extends Collection
22 {
23 /**
24 * @param array $rawSettings
25 * @param string $type
26 * @param string $carrierName
27 */
28 public function setSettingsByType(array $rawSettings = [], string $type = "", string $carrierName = null)
29 {
30 foreach ($rawSettings as $name => $value) {
31 $setting = new Setting($name, $value, $type, $carrierName);
32 $this->push($setting);
33 }
34 }
35
36 /**
37 * Check if a setting is enabled
38 *
39 * @param string $name
40 *
41 * @return bool
42 */
43 public function isEnabled(string $name): bool
44 {
45 return $this->getByName($name) ?? false;
46 }
47
48 /**
49 * Search for a setting by name and value.
50 *
51 * @param string $name
52 * @param string $value
53 *
54 * @return SettingsCollection
55 */
56 public function like(string $name, string $value): self
57 {
58 return $this->filter(
59 function (Setting $item) use ($name, $value) {
60 return false !== strpos($item->name, $value);
61 }
62 );
63 }
64
65 /**
66 * @param string $name
67 *
68 * @return mixed|null
69 */
70 public function getByName(string $name)
71 {
72 /** @var Setting $setting */
73 $setting = $this->where('name', $name)->first();
74
75 return $setting->value ?? null;
76 }
77
78 /**
79 * @param string $name
80 *
81 * @return bool
82 */
83 public function exists(string $name): bool
84 {
85 /** @var Setting $setting */
86 $setting = $this->where('name', $name)->first();
87
88 return (bool) $setting->value;
89 }
90
91 /**
92 * @param string $name
93 *
94 * @return int
95 */
96 public function getIntegerByName(string $name): int
97 {
98 return (int) $this->getByName($name) ?? 0;
99 }
100
101 /**
102 * @param string $name
103 *
104 * @return float
105 */
106 public function getFloatByName(string $name): float
107 {
108 $value = str_replace(',', '.', $this->getByName($name));
109 return (float) $value ?? 0;
110 }
111
112 /**
113 * @param string $name
114 *
115 * @return string
116 */
117 public function getStringByName(string $name): string
118 {
119 return $this->getByName($name) ?? '';
120 }
121 }
122