Final Class Yiisoft\Definitions\Helpers\DefinitionValidator
| Inheritance | Yiisoft\Definitions\Helpers\DefinitionValidator |
|---|
Definition validator checks if definition is valid.
Public Methods
| Method | Description | Defined By |
|---|---|---|
| validate() | Validates that definition is valid. Throws exception otherwise. | Yiisoft\Definitions\Helpers\DefinitionValidator |
| validateArrayDefinition() | Validates that array definition is valid. Throws exception otherwise. | Yiisoft\Definitions\Helpers\DefinitionValidator |
Method Details
Validates that definition is valid. Throws exception otherwise.
| public static void validate ( mixed $definition, string|null $id = null ) | ||
| $definition | mixed |
Definition to validate. |
| $id | string|null | |
| throws | Yiisoft\Definitions\Exception\InvalidConfigException |
If definition is not valid. |
|---|---|---|
| throws | ReflectionException | |
public static function validate(mixed $definition, ?string $id = null): void
{
// Reference or ready object
if (is_object($definition) && self::isValidObject($definition)) {
return;
}
// Class
if (is_string($definition)) {
self::validateString($definition);
return;
}
// Callable definition
if (is_callable($definition, true)) {
return;
}
// Array definition
if (is_array($definition)) {
self::validateArrayDefinition($definition, $id);
return;
}
throw new InvalidConfigException(
'Invalid definition: '
. ($definition === '' ? 'empty string.' : var_export($definition, true)),
);
}
Validates that array definition is valid. Throws exception otherwise.
| public static void validateArrayDefinition ( array $definition, string|null $id = null ) | ||
| $definition | array |
Array definition to validate. |
| $id | string|null | |
| throws | Yiisoft\Definitions\Exception\InvalidConfigException |
If definition is not valid. |
|---|---|---|
| throws | ReflectionException | |
public static function validateArrayDefinition(array $definition, ?string $id = null): void
{
/** @var class-string $className */
$className = $definition[ArrayDefinition::CLASS_NAME] ?? $id ?? throw new InvalidConfigException(
'Invalid definition: no class name specified.',
);
self::validateString($className);
$classReflection = new ReflectionClass($className);
$classPublicMethods = [];
foreach ($classReflection->getMethods() as $reflectionMethod) {
if ($reflectionMethod->isPublic() && !self::isMagicMethod($reflectionMethod->getName())) {
$classPublicMethods[] = $reflectionMethod->getName();
}
}
$classPublicProperties = [];
foreach ($classReflection->getProperties() as $reflectionProperty) {
if (self::isPublicWritableProperty($reflectionProperty)) {
$classPublicProperties[] = $reflectionProperty->getName();
}
}
foreach ($definition as $key => $value) {
if (!is_string($key)) {
throw ExceptionHelper::invalidArrayDefinitionKey($key);
}
// Class
if ($key === ArrayDefinition::CLASS_NAME) {
continue;
}
// Constructor arguments
if ($key === ArrayDefinition::CONSTRUCTOR) {
self::validateConstructor($value);
continue;
}
// Methods and properties
if ((count($methodArray = explode('()', $key)) === 2) && !empty($methodArray[0])) {
self::validateMethod($methodArray[0], $classReflection, $classPublicMethods, $className, $value);
continue;
}
if (str_starts_with($key, '$')) {
self::validateProperty($key, $classReflection, $classPublicProperties, $className);
continue;
}
$possibleOptionsMessage = self::generatePossibleMessage(
$key,
$classPublicMethods,
$classPublicProperties,
$classReflection,
$className,
);
throw new InvalidConfigException(
"Invalid definition: key \"$key\" is not allowed. $possibleOptionsMessage",
);
}
}
Signup or Login in order to comment.