Final Class Yiisoft\Yii\Gii\Validator\NewClassHandler
| Inheritance | Yiisoft\Yii\Gii\Validator\NewClassHandler |
|---|---|
| Implements | Yiisoft\Validator\RuleHandlerInterface |
Public Methods
| Method | Description | Defined By |
|---|---|---|
| __construct() | Yiisoft\Yii\Gii\Validator\NewClassHandler | |
| validate() | An inline validator that checks if the attribute value refers to a valid namespaced class name. | Yiisoft\Yii\Gii\Validator\NewClassHandler |
Method Details
| public mixed __construct ( \Yiisoft\Aliases\Aliases $aliases ) | ||
| $aliases | \Yiisoft\Aliases\Aliases | |
public function __construct(
private readonly Aliases $aliases,
) {
}
An inline validator that checks if the attribute value refers to a valid namespaced class name.
The validator will check if the directory containing the new class file exist or not.
| public \Yiisoft\Validator\Result validate ( mixed $value, object $rule, \Yiisoft\Validator\ValidationContext $context ) | ||
| $value | mixed |
Being validated |
| $rule | object | |
| $context | \Yiisoft\Validator\ValidationContext | |
public function validate(mixed $value, object $rule, ValidationContext $context): Result
{
if (!$rule instanceof NewClassRule) {
throw new UnexpectedRuleException(NewClassRule::class, $rule);
}
$result = new Result();
if (!is_string($value)) {
$result->addError(sprintf('Value must be a string, %s given.".', gettype($value)));
return $result;
}
$class = ltrim($value, '\\');
if (($pos = strrpos($class, '\\')) !== false) {
$ns = substr($class, 0, $pos);
try {
$path = $this->aliases->get('@' . str_replace('\\', '/', $ns));
if (!is_dir($path)) {
$result->addError("Please make sure the directory containing this class exists: $path");
}
} catch (InvalidArgumentException) {
$result->addError("The class namespace is invalid: $ns");
}
}
return $result;
}
Signup or Login in order to comment.