classes
3 days ago
export
3 days ago
factory.php
3 days ago
index.html
3 days ago
wrapper.php
3 days ago
wrapper.php
230 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 | * Abstract order class wrapper. |
| 16 | * |
| 17 | * @since 1.7 |
| 18 | */ |
| 19 | abstract class VAPOrderWrapper extends JObject implements JsonSerializable |
| 20 | { |
| 21 | /** |
| 22 | * An array containing all the properties that should be accessed |
| 23 | * through the lazy loading technique. Children classes can inherit |
| 24 | * this property to inject any other custom property. |
| 25 | * |
| 26 | * @var array |
| 27 | */ |
| 28 | protected $_lazyLookup = array(); |
| 29 | |
| 30 | /** |
| 31 | * Class constructor. |
| 32 | * |
| 33 | * @param integer $id The order ID. |
| 34 | * @param mixed $langtag The language tag. If null, the default one will be used. |
| 35 | * @param array $options An array of options to be passed to the order instance. |
| 36 | */ |
| 37 | public function __construct($id, $langtag = null, array $options = array()) |
| 38 | { |
| 39 | if (!isset($options['translate'])) |
| 40 | { |
| 41 | // in case of no translation rule, translate only if multi-lingual is enabled |
| 42 | $options['translate'] = VikAppointments::isMultilanguage(); |
| 43 | } |
| 44 | |
| 45 | // create lazy loading lookup array |
| 46 | $this->_lazyLookup = array_merge(array( |
| 47 | 'billing', |
| 48 | 'author', |
| 49 | 'invoice', |
| 50 | 'history', |
| 51 | 'notes', |
| 52 | ), (array) $this->_lazyLookup); |
| 53 | |
| 54 | // load order |
| 55 | $order = $this->load($id, $langtag, $options); |
| 56 | |
| 57 | // construct object |
| 58 | parent::__construct($order); |
| 59 | |
| 60 | /** |
| 61 | * When "preload" option is passed, we need to prevent the |
| 62 | * lazy loading of all the secondary information. |
| 63 | */ |
| 64 | if (!empty($options['preload'])) |
| 65 | { |
| 66 | // retrieve all the information with lazy load |
| 67 | foreach ($this->_lazyLookup as $name) |
| 68 | { |
| 69 | $this->__get($name); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | // check if we should translate the order |
| 74 | if ($options['translate']) |
| 75 | { |
| 76 | $this->translate($langtag); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Magic method used to access the internal properties. |
| 82 | * |
| 83 | * @param string $name The property name. |
| 84 | * |
| 85 | * @return mixed The property value if exists, null otherwise. |
| 86 | */ |
| 87 | public function __get($name) |
| 88 | { |
| 89 | // build method name to look for |
| 90 | $method = 'get' . ucfirst($name); |
| 91 | |
| 92 | // check if we should use an apposite method |
| 93 | if (method_exists($this, $method) && is_callable(array($this, $method))) |
| 94 | { |
| 95 | // get stored data, if any |
| 96 | $data = $this->get($name); |
| 97 | |
| 98 | if (is_null($data)) |
| 99 | { |
| 100 | // no stored details, load them |
| 101 | $data = $this->{$method}(); |
| 102 | |
| 103 | // cache details |
| 104 | $this->set($name, $data); |
| 105 | } |
| 106 | |
| 107 | // immediately return the data |
| 108 | return $data; |
| 109 | } |
| 110 | |
| 111 | return $this->get($name); |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Magic method used to check whether an internal property exists. |
| 116 | * |
| 117 | * @param string $name The property name. |
| 118 | * |
| 119 | * @return boolean True if the property is set, false otherwise. |
| 120 | */ |
| 121 | public function __isset($name) |
| 122 | { |
| 123 | return $this->get($name, null) !== null; |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Creates a standard object, containing all the supported properties, |
| 128 | * to be used when this class is passed to "json_encode()". |
| 129 | * |
| 130 | * @return object |
| 131 | * |
| 132 | * @see JsonSerializable |
| 133 | */ |
| 134 | #[ReturnTypeWillChange] |
| 135 | public function jsonSerialize() |
| 136 | { |
| 137 | // lazy load additional details before encoding them |
| 138 | foreach ($this->_lazyLookup as $name) |
| 139 | { |
| 140 | $this->__get($name); |
| 141 | } |
| 142 | |
| 143 | return $this; |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Helper function used to detach the specified property |
| 148 | * from the given object. Useful to avoid having duplicate |
| 149 | * properties in nested objects. |
| 150 | * |
| 151 | * @param object $row The object from which search and detach. |
| 152 | * @param string $k The property to search. |
| 153 | * |
| 154 | * @param mixed The detached value. |
| 155 | */ |
| 156 | protected function detach($row, $k) |
| 157 | { |
| 158 | if (!property_exists($row, $k)) |
| 159 | { |
| 160 | // property not found |
| 161 | return null; |
| 162 | } |
| 163 | |
| 164 | // assign property within a temporary variable |
| 165 | $tmp = $row->{$k}; |
| 166 | |
| 167 | // detach from object |
| 168 | unset($row->{$k}); |
| 169 | |
| 170 | return $tmp; |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * Returns the order object. |
| 175 | * |
| 176 | * @param integer $id The order ID. |
| 177 | * @param mixed $langtag The language tag. If null, the default one will be used. |
| 178 | * @param array $options An array of options to be passed to the order instance. |
| 179 | * |
| 180 | * @return mixed The array/object to load. |
| 181 | * |
| 182 | * @throws Exception |
| 183 | */ |
| 184 | abstract protected function load($id, $langtag = null, array $options = array()); |
| 185 | |
| 186 | /** |
| 187 | * Translates the internal properties. |
| 188 | * |
| 189 | * @param mixed $langtag The language tag. If null, the default one will be used. |
| 190 | * |
| 191 | * @return void |
| 192 | */ |
| 193 | abstract protected function translate($langtag = null); |
| 194 | |
| 195 | /** |
| 196 | * Returns the billing details of the user that made the order. |
| 197 | * |
| 198 | * @return object |
| 199 | */ |
| 200 | abstract protected function getBilling(); |
| 201 | |
| 202 | /** |
| 203 | * Returns the user details of the user that created the order. |
| 204 | * |
| 205 | * @return object |
| 206 | */ |
| 207 | abstract protected function getAuthor(); |
| 208 | |
| 209 | /** |
| 210 | * Returns the invoice details of the order. |
| 211 | * |
| 212 | * @return mixed The invoice object if exists, false otherwise. |
| 213 | */ |
| 214 | abstract protected function getInvoice(); |
| 215 | |
| 216 | /** |
| 217 | * Returns the history of the status codes set for the order. |
| 218 | * |
| 219 | * @return array |
| 220 | */ |
| 221 | abstract protected function getHistory(); |
| 222 | |
| 223 | /** |
| 224 | * Returns a list of notes assigned to this order. |
| 225 | * |
| 226 | * @return array |
| 227 | */ |
| 228 | abstract protected function getNotes(); |
| 229 | } |
| 230 |