AccessToken
5 days ago
AuthHandler
5 days ago
Http
5 days ago
Service
5 days ago
Task
5 days ago
Utils
5 days ago
Client.php
5 days ago
Collection.php
5 days ago
Exception.php
5 days ago
Model.php
5 days ago
Service.php
5 days ago
aliases.php
5 days ago
Model.php
302 lines
| 1 | <?php |
| 2 | |
| 3 | /* |
| 4 | * Copyright 2011 Google Inc. |
| 5 | * |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | * you may not use this file except in compliance with the License. |
| 8 | * You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | */ |
| 18 | namespace WPMailSMTP\Vendor\Google; |
| 19 | |
| 20 | use WPMailSMTP\Vendor\Google\Exception as GoogleException; |
| 21 | use ReflectionObject; |
| 22 | use ReflectionProperty; |
| 23 | use stdClass; |
| 24 | /** |
| 25 | * This class defines attributes, valid values, and usage which is generated |
| 26 | * from a given json schema. |
| 27 | * http://tools.ietf.org/html/draft-zyp-json-schema-03#section-5 |
| 28 | * |
| 29 | */ |
| 30 | #[\AllowDynamicProperties] |
| 31 | class Model implements \ArrayAccess |
| 32 | { |
| 33 | /** |
| 34 | * If you need to specify a NULL JSON value, use Google\Model::NULL_VALUE |
| 35 | * instead - it will be replaced when converting to JSON with a real null. |
| 36 | */ |
| 37 | const NULL_VALUE = "{}gapi-php-null"; |
| 38 | protected $internal_gapi_mappings = []; |
| 39 | protected $modelData = []; |
| 40 | protected $processed = []; |
| 41 | /** |
| 42 | * Polymorphic - accepts a variable number of arguments dependent |
| 43 | * on the type of the model subclass. |
| 44 | */ |
| 45 | public final function __construct() |
| 46 | { |
| 47 | if (\func_num_args() == 1 && \is_array(\func_get_arg(0))) { |
| 48 | // Initialize the model with the array's contents. |
| 49 | $array = \func_get_arg(0); |
| 50 | $this->mapTypes($array); |
| 51 | } |
| 52 | $this->gapiInit(); |
| 53 | } |
| 54 | /** |
| 55 | * Getter that handles passthrough access to the data array, and lazy object creation. |
| 56 | * @param string $key Property name. |
| 57 | * @return mixed The value if any, or null. |
| 58 | */ |
| 59 | public function __get($key) |
| 60 | { |
| 61 | $keyType = $this->keyType($key); |
| 62 | $keyDataType = $this->dataType($key); |
| 63 | if ($keyType && !isset($this->processed[$key])) { |
| 64 | if (isset($this->modelData[$key])) { |
| 65 | $val = $this->modelData[$key]; |
| 66 | } elseif ($keyDataType == 'array' || $keyDataType == 'map') { |
| 67 | $val = []; |
| 68 | } else { |
| 69 | $val = null; |
| 70 | } |
| 71 | if ($this->isAssociativeArray($val)) { |
| 72 | if ($keyDataType && 'map' == $keyDataType) { |
| 73 | foreach ($val as $arrayKey => $arrayItem) { |
| 74 | $this->modelData[$key][$arrayKey] = new $keyType($arrayItem); |
| 75 | } |
| 76 | } else { |
| 77 | $this->modelData[$key] = new $keyType($val); |
| 78 | } |
| 79 | } elseif (\is_array($val)) { |
| 80 | $arrayObject = []; |
| 81 | foreach ($val as $arrayIndex => $arrayItem) { |
| 82 | $arrayObject[$arrayIndex] = new $keyType($arrayItem); |
| 83 | } |
| 84 | $this->modelData[$key] = $arrayObject; |
| 85 | } |
| 86 | $this->processed[$key] = \true; |
| 87 | } |
| 88 | return isset($this->modelData[$key]) ? $this->modelData[$key] : null; |
| 89 | } |
| 90 | /** |
| 91 | * Initialize this object's properties from an array. |
| 92 | * |
| 93 | * @param array $array Used to seed this object's properties. |
| 94 | * @return void |
| 95 | */ |
| 96 | protected function mapTypes($array) |
| 97 | { |
| 98 | // Hard initialise simple types, lazy load more complex ones. |
| 99 | foreach ($array as $key => $val) { |
| 100 | if ($keyType = $this->keyType($key)) { |
| 101 | $dataType = $this->dataType($key); |
| 102 | if ($dataType == 'array' || $dataType == 'map') { |
| 103 | $this->{$key} = []; |
| 104 | foreach ($val as $itemKey => $itemVal) { |
| 105 | if ($itemVal instanceof $keyType) { |
| 106 | $this->{$key}[$itemKey] = $itemVal; |
| 107 | } else { |
| 108 | $this->{$key}[$itemKey] = new $keyType($itemVal); |
| 109 | } |
| 110 | } |
| 111 | } elseif ($val instanceof $keyType) { |
| 112 | $this->{$key} = $val; |
| 113 | } else { |
| 114 | $this->{$key} = new $keyType($val); |
| 115 | } |
| 116 | unset($array[$key]); |
| 117 | } elseif (\property_exists($this, $key)) { |
| 118 | $this->{$key} = $val; |
| 119 | unset($array[$key]); |
| 120 | } elseif (\property_exists($this, $camelKey = $this->camelCase($key))) { |
| 121 | // This checks if property exists as camelCase, leaving it in array as snake_case |
| 122 | // in case of backwards compatibility issues. |
| 123 | $this->{$camelKey} = $val; |
| 124 | } |
| 125 | } |
| 126 | $this->modelData = $array; |
| 127 | } |
| 128 | /** |
| 129 | * Blank initialiser to be used in subclasses to do post-construction initialisation - this |
| 130 | * avoids the need for subclasses to have to implement the variadics handling in their |
| 131 | * constructors. |
| 132 | */ |
| 133 | protected function gapiInit() |
| 134 | { |
| 135 | return; |
| 136 | } |
| 137 | /** |
| 138 | * Create a simplified object suitable for straightforward |
| 139 | * conversion to JSON. This is relatively expensive |
| 140 | * due to the usage of reflection, but shouldn't be called |
| 141 | * a whole lot, and is the most straightforward way to filter. |
| 142 | */ |
| 143 | public function toSimpleObject() |
| 144 | { |
| 145 | $object = new stdClass(); |
| 146 | // Process all other data. |
| 147 | foreach ($this->modelData as $key => $val) { |
| 148 | $result = $this->getSimpleValue($val); |
| 149 | if ($result !== null) { |
| 150 | $object->{$key} = $this->nullPlaceholderCheck($result); |
| 151 | } |
| 152 | } |
| 153 | // Process all public properties. |
| 154 | $reflect = new ReflectionObject($this); |
| 155 | $props = $reflect->getProperties(ReflectionProperty::IS_PUBLIC); |
| 156 | foreach ($props as $member) { |
| 157 | $name = $member->getName(); |
| 158 | $result = $this->getSimpleValue($this->{$name}); |
| 159 | if ($result !== null) { |
| 160 | $name = $this->getMappedName($name); |
| 161 | $object->{$name} = $this->nullPlaceholderCheck($result); |
| 162 | } |
| 163 | } |
| 164 | return $object; |
| 165 | } |
| 166 | /** |
| 167 | * Handle different types of values, primarily |
| 168 | * other objects and map and array data types. |
| 169 | */ |
| 170 | private function getSimpleValue($value) |
| 171 | { |
| 172 | if ($value instanceof Model) { |
| 173 | return $value->toSimpleObject(); |
| 174 | } elseif (\is_array($value)) { |
| 175 | $return = []; |
| 176 | foreach ($value as $key => $a_value) { |
| 177 | $a_value = $this->getSimpleValue($a_value); |
| 178 | if ($a_value !== null) { |
| 179 | $key = $this->getMappedName($key); |
| 180 | $return[$key] = $this->nullPlaceholderCheck($a_value); |
| 181 | } |
| 182 | } |
| 183 | return $return; |
| 184 | } |
| 185 | return $value; |
| 186 | } |
| 187 | /** |
| 188 | * Check whether the value is the null placeholder and return true null. |
| 189 | */ |
| 190 | private function nullPlaceholderCheck($value) |
| 191 | { |
| 192 | if ($value === self::NULL_VALUE) { |
| 193 | return null; |
| 194 | } |
| 195 | return $value; |
| 196 | } |
| 197 | /** |
| 198 | * If there is an internal name mapping, use that. |
| 199 | */ |
| 200 | private function getMappedName($key) |
| 201 | { |
| 202 | if (isset($this->internal_gapi_mappings, $this->internal_gapi_mappings[$key])) { |
| 203 | $key = $this->internal_gapi_mappings[$key]; |
| 204 | } |
| 205 | return $key; |
| 206 | } |
| 207 | /** |
| 208 | * Returns true only if the array is associative. |
| 209 | * @param array $array |
| 210 | * @return bool True if the array is associative. |
| 211 | */ |
| 212 | protected function isAssociativeArray($array) |
| 213 | { |
| 214 | if (!\is_array($array)) { |
| 215 | return \false; |
| 216 | } |
| 217 | $keys = \array_keys($array); |
| 218 | foreach ($keys as $key) { |
| 219 | if (\is_string($key)) { |
| 220 | return \true; |
| 221 | } |
| 222 | } |
| 223 | return \false; |
| 224 | } |
| 225 | /** |
| 226 | * Verify if $obj is an array. |
| 227 | * @throws \Google\Exception Thrown if $obj isn't an array. |
| 228 | * @param array $obj Items that should be validated. |
| 229 | * @param string $method Method expecting an array as an argument. |
| 230 | */ |
| 231 | public function assertIsArray($obj, $method) |
| 232 | { |
| 233 | if ($obj && !\is_array($obj)) { |
| 234 | throw new GoogleException("Incorrect parameter type passed to {$method}(). Expected an array."); |
| 235 | } |
| 236 | } |
| 237 | /** @return bool */ |
| 238 | #[\ReturnTypeWillChange] |
| 239 | public function offsetExists($offset) |
| 240 | { |
| 241 | return isset($this->{$offset}) || isset($this->modelData[$offset]); |
| 242 | } |
| 243 | /** @return mixed */ |
| 244 | #[\ReturnTypeWillChange] |
| 245 | public function offsetGet($offset) |
| 246 | { |
| 247 | return isset($this->{$offset}) ? $this->{$offset} : $this->__get($offset); |
| 248 | } |
| 249 | /** @return void */ |
| 250 | #[\ReturnTypeWillChange] |
| 251 | public function offsetSet($offset, $value) |
| 252 | { |
| 253 | if (\property_exists($this, $offset)) { |
| 254 | $this->{$offset} = $value; |
| 255 | } else { |
| 256 | $this->modelData[$offset] = $value; |
| 257 | $this->processed[$offset] = \true; |
| 258 | } |
| 259 | } |
| 260 | /** @return void */ |
| 261 | #[\ReturnTypeWillChange] |
| 262 | public function offsetUnset($offset) |
| 263 | { |
| 264 | unset($this->modelData[$offset]); |
| 265 | } |
| 266 | protected function keyType($key) |
| 267 | { |
| 268 | $keyType = $key . "Type"; |
| 269 | // ensure keyType is a valid class |
| 270 | if (\property_exists($this, $keyType) && $this->{$keyType} !== null && \class_exists($this->{$keyType})) { |
| 271 | return $this->{$keyType}; |
| 272 | } |
| 273 | } |
| 274 | protected function dataType($key) |
| 275 | { |
| 276 | $dataType = $key . "DataType"; |
| 277 | if (\property_exists($this, $dataType)) { |
| 278 | return $this->{$dataType}; |
| 279 | } |
| 280 | } |
| 281 | public function __isset($key) |
| 282 | { |
| 283 | return isset($this->modelData[$key]); |
| 284 | } |
| 285 | public function __unset($key) |
| 286 | { |
| 287 | unset($this->modelData[$key]); |
| 288 | } |
| 289 | /** |
| 290 | * Convert a string to camelCase |
| 291 | * @param string $value |
| 292 | * @return string |
| 293 | */ |
| 294 | private function camelCase($value) |
| 295 | { |
| 296 | $value = \ucwords(\str_replace(['-', '_'], ' ', $value)); |
| 297 | $value = \str_replace(' ', '', $value); |
| 298 | $value[0] = \strtolower($value[0]); |
| 299 | return $value; |
| 300 | } |
| 301 | } |
| 302 |