PluginProbe ʕ •ᴥ•ʔ
VikAppointments Services Booking Calendar / 1.2.21
VikAppointments Services Booking Calendar v1.2.21
1.2.21 1.2.20 trunk 1.2.17 1.2.18 1.2.19
vikappointments / site / helpers / libraries / action / result.php
vikappointments / site / helpers / libraries / action Last commit date
container 2 days ago observable 2 days ago result 2 days ago container.php 2 days ago index.html 2 days ago observable.php 2 days ago observer.php 2 days ago result.php 2 days ago state.php 2 days ago
result.php
138 lines
1 <?php
2 /**
3 * @package VikAppointments
4 * @subpackage core
5 * @author E4J s.r.l.
6 * @copyright Copyright (C) 2021 E4J s.r.l. All Rights Reserved.
7 * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
8 * @link https://vikwp.com
9 */
10
11 // No direct access
12 defined('ABSPATH') or die('No script kiddies please!');
13
14 /**
15 * Wraps the results returned by the subscribers.
16 *
17 * @since 1.7.3
18 */
19 class VAPActionResult implements IteratorAggregate
20 {
21 /**
22 * An array of results.
23 *
24 * @var array
25 */
26 private $results = [];
27
28 /**
29 * Class Constructor.
30 *
31 * @param mixed $results Either an array or an action result instance
32 */
33 final public function __construct($results = [])
34 {
35 if ($results instanceof VAPActionResult)
36 {
37 // in case of a self instance, extracts the array results
38 $results = $results->toArray();
39 }
40
41 if (!is_array($results))
42 {
43 // not an array, throw exception
44 throw new InvalidArgumentException(sprintf('The argument must be an array, %s given', gettype($results)), 400);
45 }
46
47 // map the values according to the requirements of the class
48 $results = array_map([$this, 'map'], $results);
49
50 // get rid of NULL elements
51 $results = array_filter($results, function($elem)
52 {
53 return $elem !== null;
54 });
55
56 // take only the values of the array
57 $this->results = array_values($results);
58 }
59
60 /**
61 * Filters the resulting elements while constructing the object.
62 * Children classes can override this method to sanitize the
63 * received results.
64 *
65 * @param mixed $elem The element to map.
66 *
67 * @return mixed The mapped element.
68 */
69 protected function map($elem)
70 {
71 return $elem;
72 }
73
74 /**
75 * Returns the results array.
76 *
77 * @return array
78 */
79 final public function toArray()
80 {
81 return $this->results;
82 }
83
84 /**
85 * Checks whether the specified value exists within the
86 * array of results. If omitted, it will check whether
87 * the list is empty or not.
88 *
89 * @param mixed $value The value to search.
90 *
91 * @return boolean True if exists, false otherwise.
92 */
93 final public function has($value = null)
94 {
95 if (is_null($value))
96 {
97 // check whether the list is empty or not
98 return (bool) $this->results;
99 }
100
101 // look for the given value
102 return in_array($value, $this->results, true);
103 }
104
105 /**
106 * Extracts the first returned element.
107 *
108 * @return mixed
109 */
110 final public function first()
111 {
112 return $this->results ? $this->results[0] : null;
113 }
114
115 /**
116 * Extracts the last returned element.
117 *
118 * @return mixed
119 */
120 final public function last()
121 {
122 return $this->results ? $this->results[count($this->results) - 1] : null;
123 }
124
125 /**
126 * Returns the iterator interface.
127 *
128 * @return Traversable
129 *
130 * @see IteratorAggregate
131 */
132 #[ReturnTypeWillChange]
133 final public function getIterator()
134 {
135 return new ArrayIterator($this->toArray());
136 }
137 }
138