2014-01-16 16:56:24 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* ManiaPlanet dedicated server Xml-RPC client
|
|
|
|
*
|
|
|
|
* @license http://www.gnu.org/licenses/lgpl.html LGPL License 3
|
|
|
|
*/
|
2014-05-08 19:32:14 +02:00
|
|
|
|
2014-01-16 16:56:24 +01:00
|
|
|
namespace Maniaplanet\DedicatedServer\Structures;
|
|
|
|
|
|
|
|
abstract class AbstractStructure
|
|
|
|
{
|
|
|
|
static public function fromArray($array)
|
|
|
|
{
|
|
|
|
if(!is_array($array))
|
|
|
|
return $array;
|
2014-05-08 19:32:14 +02:00
|
|
|
|
2014-01-16 16:56:24 +01:00
|
|
|
$object = new static;
|
|
|
|
foreach($array as $key => $value)
|
|
|
|
$object->{lcfirst($key)} = $value;
|
|
|
|
return $object;
|
|
|
|
}
|
2014-05-08 19:32:14 +02:00
|
|
|
|
2014-01-16 16:56:24 +01:00
|
|
|
static public function fromArrayOfArray($array)
|
|
|
|
{
|
|
|
|
if(!is_array($array))
|
|
|
|
return $array;
|
2014-05-08 19:32:14 +02:00
|
|
|
|
2014-01-16 16:56:24 +01:00
|
|
|
$result = array();
|
|
|
|
foreach($array as $key => $value)
|
|
|
|
$result[$key] = static::fromArray($value);
|
|
|
|
return $result;
|
|
|
|
}
|
2014-05-08 19:32:14 +02:00
|
|
|
|
2014-01-16 16:56:24 +01:00
|
|
|
static public function getPropertyFromArray($array, $property)
|
|
|
|
{
|
|
|
|
return array_map(get_called_class().'::extractProperty', $array, array_fill(0, count($array), $property));
|
|
|
|
}
|
2014-05-08 19:32:14 +02:00
|
|
|
|
2014-01-16 16:56:24 +01:00
|
|
|
static protected function extractProperty($element, $property)
|
|
|
|
{
|
|
|
|
if(!is_a($element, get_called_class()) || !property_exists($element, $property))
|
|
|
|
throw new \InvalidArgumentException('property '.$property.' does not exists in class: '.get_called_class());
|
2014-05-08 19:32:14 +02:00
|
|
|
|
2014-01-16 16:56:24 +01:00
|
|
|
return $element->$property;
|
|
|
|
}
|
2014-05-08 19:32:14 +02:00
|
|
|
|
2014-01-16 16:56:24 +01:00
|
|
|
function toArray()
|
|
|
|
{
|
|
|
|
$out = array();
|
|
|
|
foreach(get_object_vars($this) as $key => $value)
|
|
|
|
$out[ucfirst($key)] = $value;
|
|
|
|
return $out;
|
|
|
|
}
|
|
|
|
}
|
2014-05-08 19:32:14 +02:00
|
|
|
|
|
|
|
?>
|