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