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 / import / column.php
vikappointments / site / helpers / libraries / import Last commit date
classes 2 days ago export 2 days ago forms 2 days ago samples 2 days ago column.php 2 days ago exportable.php 2 days ago factory.php 2 days ago index.html 2 days ago object.php 2 days ago
column.php
195 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 * Encapsulates the information held by a column, providing
16 * further consistency to the properties access.
17 *
18 * @since 1.7
19 */
20 #[\AllowDynamicProperties]
21 class ImportColumn
22 {
23 /**
24 * Tries to look for a column matching the specified type.
25 *
26 * @param mixed $data Either a SimpleXMLElement or a non-scalar value.
27 *
28 * @return ImportColumns
29 *
30 * @throws RuntimeException
31 */
32 final public static function getInstance($data)
33 {
34 if ($data instanceof SimpleXMLElement)
35 {
36 // extract type from XML
37 $type = (string) $data->attributes()->type;
38 }
39 else
40 {
41 // extract type from array
42 $data = (array) $data;
43 $type = isset($data['type']) ? $data['type'] : '';
44 }
45
46 if (!$type)
47 {
48 // no type given, use base class
49 return new static($data);
50 }
51
52 // attempt to load the column type
53 if (!VAPLoader::import('libraries.import.forms.columns.' . $type))
54 {
55 // import column file not found
56 throw new RuntimeException(sprintf('Import column [%s] not found', $type), 404);
57 }
58
59 // capitalize type
60 $type = str_replace('_', ' ', $type);
61 $type = str_replace(' ', '', ucwords($type));
62
63 // build classname
64 $classname = 'ImportColumn' . $type;
65
66 if (!$classname)
67 {
68 // class not found
69 throw new RuntimeException(sprintf('Import column class [%s] not found', $classname), 404);
70 }
71
72 // instantiate column
73 return new $classname($data);
74 }
75
76 /**
77 * Class constructor.
78 *
79 * @param mixed $data Either a SimpleXMLElement or a non-scalar value.
80 */
81 public function __construct($data)
82 {
83 if ($data instanceof SimpleXMLElement)
84 {
85 // set up as XML
86 $this->setupXML($data);
87 }
88 else
89 {
90 // set up as array/object
91 $this->setup($data);
92 }
93 }
94
95 /**
96 * Binds the columns properties by extracting them
97 * from the given XML element.
98 *
99 * @param SimpleXMLElement $data The XML element.
100 *
101 * @return void
102 */
103 protected function setupXML(SimpleXMLElement $data)
104 {
105 $obj = new stdClass;
106 $obj->name = (string) $data->attributes()->name;
107 $obj->label = (string) $data->attributes()->label;
108 $obj->required = (int) $data->attributes()->required;
109 $obj->default = (string) $data->attributes()->default;
110 $obj->filter = (string) $data->attributes()->filter;
111 $obj->type = (string) $data->attributes()->type;
112 $obj->options = array();
113
114 foreach ($data->option as $opt)
115 {
116 $k = (string) $opt->attributes()->value;
117 $v = (string) JText::translate($opt);
118
119 $obj->options[$k] = $v;
120 }
121
122 // attempt to translate the label
123 $obj->label = JText::translate($obj->label);
124
125 // now finalise by binding it as an array
126 $this->setup($obj);
127 }
128
129 /**
130 * Binds the internal properties with the given array/object.
131 *
132 * @param mixed $data Either an array or an object.
133 *
134 * @return void
135 */
136 protected function setup($data)
137 {
138 foreach ($data as $k => $v)
139 {
140 $this->{$k} = $v;
141 }
142 }
143
144 /**
145 * Magic method used to directly access the properties
146 * without raising any errors or warnings.
147 *
148 * @param string $name The property to access.
149 *
150 * @return mixed The property value if exists, null otherwise.
151 */
152 public function __get($name)
153 {
154 if (isset($this->{$name}))
155 {
156 return $this->{$name};
157 }
158
159 return null;
160 }
161
162 /**
163 * Helper method used to manipulate the given value before
164 * binding the object to save.
165 *
166 * @param mixed $value The default import value.
167 *
168 * @return string The value to bind
169 */
170 public function onImport($value)
171 {
172 return $value;
173 }
174
175 /**
176 * Helper method used to format the values under this column.
177 *
178 * @param mixed $value The default column value.
179 *
180 * @return string The formatted value
181 */
182 public function format($value)
183 {
184 // look by default into the options array
185 if (isset($this->options[$value]))
186 {
187 // option found, return the stored value
188 return $this->options[$value];
189 }
190
191 // use the default value
192 return (string) $value;
193 }
194 }
195