Final Class Yiisoft\Yii\Gii\Validator\NewClassHandler
| Inheritance | Yiisoft\ |
|---|---|
| Implements | Yiisoft\ |
Public Methods
| Method | Description | Defined By |
|---|---|---|
| __construct() | Yiisoft\ |
|
| validate() | An inline validator that checks if the attribute value refers to a valid namespaced class name. | Yiisoft\ |
Method Details
| public mixed __construct ( \ | ||
| $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 \ | ||
| $value | mixed |
Being validated |
| $rule | object | |
| $context | \ |
|
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;
}
User Contributed Notes
Leave a comment
Join the conversation to share a note.