39 lines
1.2 KiB
PHP
39 lines
1.2 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace KupShop\KupShopBundle\Util;
|
|
|
|
class ObjectUtil
|
|
{
|
|
public static function getUninitializedRequiredProperties(object $object, int $filter = \ReflectionProperty::IS_PUBLIC): array
|
|
{
|
|
$properties = static::getProperties($object, $filter);
|
|
|
|
$uninitializedRequiredProperties = [];
|
|
|
|
foreach ($properties as $property) {
|
|
$reflect = new \ReflectionProperty(get_class($object), $property);
|
|
if (!$reflect->getType()->allowsNull() && !$reflect->isInitialized($object)) {
|
|
$uninitializedRequiredProperties[] = $property;
|
|
}
|
|
}
|
|
|
|
return $uninitializedRequiredProperties;
|
|
}
|
|
|
|
public static function isPropertyInitialized(object $object, string $propertyName): bool
|
|
{
|
|
$reflect = new \ReflectionProperty(get_class($object), $propertyName);
|
|
|
|
return $reflect->isInitialized($object);
|
|
}
|
|
|
|
public static function getProperties(object $object, int $filter = \ReflectionProperty::IS_PUBLIC): array
|
|
{
|
|
$reflect = new \ReflectionClass($object);
|
|
|
|
return array_map(fn ($x) => $x->getName(), $reflect->getProperties($filter));
|
|
}
|
|
}
|