PluginProbe
GiveWP – Donation Plugin and Fundraising Platform / 4.16.8.1
GiveWP – Donation Plugin and Fundraising Platform v4.16.8.1
4.16.9 4.16.8.1 4.16.8 4.16.7.2 4.16.7.1 4.16.7 4.16.6.1 4.16.6 4.16.5.1 4.16.5 4.16.4 4.16.3 4.16.2 4.16.1 4.16.0 4.15.5 4.15.4 4.15.3 4.15.2 4.15.1 4.15.0 2.3.0 2.3.1 2.3.2 2.30.0 All 255 releases
give / src / Framework / FieldsAPI / Concerns / HasOptions.php

HasOptions.php in GiveWP – Donation Plugin and Fundraising Platform 4.16.8.1, at src/Framework/FieldsAPI/Concerns/HasOptions.php

89 lines 2.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Give\Framework\FieldsAPI\Concerns;
4
5 use Give\Framework\FieldsAPI\Option;
6
7 trait HasOptions
8 {
9
10 /** @var Option[] */
11 protected $options = [];
12
13 /**
14 * Set the options
15 *
16 * Note that the keys of associative arrays are not supported for setting values or labels.
17 * For setting labels either use `new FieldOption($value, $label)` or `[$value, $label]`.
18 * In either case, the label is optional.
19 *
20 * @param Option|array|mixed ...$options
21 *
22 * @return $this
23 */
24 public function options(...$options)
25 {
26 // Reset options, since they are meant to be set immutably
27 $this->options = [];
28
29 // Loop through the options and transform them to the proper format.
30 foreach ($options as $value) {
31 if ($value instanceof Option) {
32 // In this case, what is provided matches the proper format, so we can just append it.
33 $this->options[] = $value;
34 } elseif (is_array($value)) {
35 // In this case, what has been provided in the value is an array with a value then a label.
36 // This matches the constructor of `FieldOption`, so we can unpack it as arguments for a new instance.
37 $this->options[] = new Option(...$value);
38 } else {
39 // In this case, we just have a value which is the bare minimum required for a `FieldOption`.
40 $this->options[] = new Option($value);
41 }
42 }
43
44 return $this;
45 }
46
47 /**
48 * Access the options
49 *
50 * @return Option[]
51 */
52 public function getOptions()
53 {
54 return $this->options;
55 }
56
57 /**
58 * Check whether options exist
59 *
60 * @since 2.15.0
61 *
62 * @return bool
63 */
64 public function hasOptions()
65 {
66 return (bool)count($this->options);
67 }
68
69 /**
70 * Walk through the options
71 *
72 * @since 2.12.0
73 *
74 * @param callable $callback
75 *
76 * @return void
77 */
78 public function walkOptions(callable $callback)
79 {
80 foreach ($this->options as $option) {
81 // Call the callback for each option.
82 if ($callback($option) === false) {
83 // Returning false breaks the loop.
84 break;
85 }
86 }
87 }
88 }
89