cherry pick maniaplanet lib 6.1

This commit is contained in:
Beu
2024-08-25 22:30:06 +02:00
parent 3044e466b9
commit c8bbb3d7c6
18 changed files with 1626 additions and 1698 deletions

View File

@ -9,46 +9,52 @@ namespace Maniaplanet\DedicatedServer\Structures;
abstract class AbstractStructure
{
static public function fromArray($array)
public static function fromArrayOfArray($array)
{
if(!is_array($array))
if (!is_array($array)) {
return $array;
}
$object = new static;
foreach($array as $key => $value)
$object->{lcfirst($key)} = $value;
return $object;
}
static public function fromArrayOfArray($array)
{
if(!is_array($array))
return $array;
$result = array();
foreach($array as $key => $value)
$result = [];
foreach ($array as $key => $value) {
$result[$key] = static::fromArray($value);
}
return $result;
}
static public function getPropertyFromArray($array, $property)
public static function fromArray($array)
{
return array_map(get_called_class().'::extractProperty', $array, array_fill(0, count($array), $property));
if (!is_array($array)) {
return $array;
}
$object = new static;
foreach ($array as $key => $value) {
$object->{lcfirst($key)} = $value;
}
return $object;
}
static protected function extractProperty($element, $property)
public static function getPropertyFromArray($array, $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());
return array_map(get_called_class() . '::extractProperty', $array, array_fill(0, count($array), $property));
}
protected static 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());
}
return $element->$property;
}
function toArray()
{
$out = array();
foreach(get_object_vars($this) as $key => $value)
$out = [];
foreach (get_object_vars($this) as $key => $value) {
$out[ucfirst($key)] = $value;
}
return $out;
}
}