TrackManiaControl/libs/Maniaplanet/DedicatedServer/Structures/AbstractStructure.php

61 lines
1.3 KiB
PHP
Raw Normal View History

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:38:15 +02:00
2014-01-16 16:56:24 +01:00
namespace Maniaplanet\DedicatedServer\Structures;
abstract class AbstractStructure
{
2024-08-25 22:30:06 +02:00
public static function fromArrayOfArray($array)
2014-01-16 16:56:24 +01:00
{
2024-08-25 22:30:06 +02:00
if (!is_array($array)) {
2014-01-16 16:56:24 +01:00
return $array;
2024-08-25 22:30:06 +02:00
}
2014-05-08 19:38:15 +02:00
2024-08-25 22:30:06 +02:00
$result = [];
foreach ($array as $key => $value) {
$result[$key] = static::fromArray($value);
}
return $result;
2014-01-16 16:56:24 +01:00
}
2014-05-08 19:38:15 +02:00
2024-08-25 22:30:06 +02:00
public static function fromArray($array)
2014-01-16 16:56:24 +01:00
{
2024-08-25 22:30:06 +02:00
if (!is_array($array)) {
2014-01-16 16:56:24 +01:00
return $array;
2024-08-25 22:30:06 +02:00
}
2014-05-08 19:38:15 +02:00
2024-08-25 22:30:06 +02:00
$object = new static;
foreach ($array as $key => $value) {
$object->{lcfirst($key)} = $value;
}
return $object;
2014-01-16 16:56:24 +01:00
}
2014-05-08 19:38:15 +02:00
2024-08-25 22:30:06 +02:00
public static function getPropertyFromArray($array, $property)
2014-01-16 16:56:24 +01:00
{
2024-08-25 22:30:06 +02:00
return array_map(get_called_class() . '::extractProperty', $array, array_fill(0, count($array), $property));
2014-01-16 16:56:24 +01:00
}
2014-05-08 19:38:15 +02:00
2024-08-25 22:30:06 +02:00
protected static function extractProperty($element, $property)
2014-01-16 16:56:24 +01:00
{
2024-08-25 22:30:06 +02:00
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:38:15 +02:00
2014-01-16 16:56:24 +01:00
return $element->$property;
}
2014-05-08 19:38:15 +02:00
2014-01-16 16:56:24 +01:00
function toArray()
{
2024-08-25 22:30:06 +02:00
$out = [];
foreach (get_object_vars($this) as $key => $value) {
2014-01-16 16:56:24 +01:00
$out[ucfirst($key)] = $value;
2024-08-25 22:30:06 +02:00
}
2014-01-16 16:56:24 +01:00
return $out;
}
}