1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-15 15:42:51 +01:00
invoiceninja/app/Services/EDocument/Standards/Settings/PropertyResolver.php
2024-08-22 16:45:06 +10:00

46 lines
1.2 KiB
PHP

<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2024. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Services\EDocument\Standards\Settings;
class PropertyResolver
{
public static function resolve($object, string $propertyPath)
{
$pathSegments = explode('.', $propertyPath);
return self::traverse($object, $pathSegments);
}
private static function traverse($object, array $pathSegments)
{
if (empty($pathSegments)) {
return null;
}
$currentProperty = array_shift($pathSegments);
if (is_object($object) && isset($object->{$currentProperty})) {
$nextObject = $object->{$currentProperty};
} elseif (is_array($object) && array_key_exists($currentProperty, $object)) {
$nextObject = $object[$currentProperty];
} else {
return null;
}
if (empty($pathSegments)) {
return $nextObject;
}
return self::traverse($nextObject, $pathSegments);
}
}