Documentation

DocValidator
in package
implements ValidatorInterface

Use '@var' comments to validate object properties.

If using a class type:

  1. include the namespace\to\Class
  2. OR indicate where to find it with the namespaces() function

E.g.

  // @var Class
  public $property;

  public function namespaces() {
      return [ 'namespace\\to\\' ];
  }

Table of Contents

Interfaces

ValidatorInterface
This class is a validator.

Properties

$errors  : array<string|int, mixed>
$namespaces  : array<string|int, string>
$target  : object

Methods

__construct()  : mixed
Create a new validator.
checkRequired()  : bool
Check if the doc type is required.
checkTypes()  : bool
Check the validity of a doc type.
getDocTypes()  : Generator<string|int, DocType>
Get a list of 'doc types' of an object.
getErrors()  : array<string|int, mixed>
Get a list of all errors, indexed by property name.
hasErrors()  : bool
True if there were any validation errors.
validate()  : bool
Validate the target.
isValid()  : bool
Determine if the 'value' and 'actual' type match the 'expected' type.
isValidArray()  : bool
Determine if all items of an array match the expected type.
lookupClass()  : string|null
Determine the full namespaced version of this class.

Properties

$namespaces

protected array<string|int, string> $namespaces

Methods

__construct()

Create a new validator.

public __construct(object $target) : mixed
Parameters
$target : object

Object to validate.

checkRequired()

Check if the doc type is required.

public checkRequired(DocType $type) : bool

This will add invalid types to the errors.

Parameters
$type : DocType
Return values
bool —

True if valid.

checkTypes()

Check the validity of a doc type.

public checkTypes(DocType $type) : bool

This will add invalid types to the errors.

Parameters
$type : DocType
Return values
bool —

True if valid.

getDocTypes()

Get a list of 'doc types' of an object.

public static getDocTypes(object $target) : Generator<string|int, DocType>
Parameters
$target : object
Return values
Generator<string|int, DocType>

getErrors()

Get a list of all errors, indexed by property name.

public getErrors() : array<string|int, mixed>

Field may have multiple errors defined.

Return values
array<string|int, mixed>

hasErrors()

True if there were any validation errors.

public hasErrors() : bool
Return values
bool

validate()

Validate the target.

public validate() : bool
Return values
bool —

True if valid. False if there were errors.

isValid()

Determine if the 'value' and 'actual' type match the 'expected' type.

protected isValid(string $expected, mixed $value) : bool

This does not modify the errors.

Parameters
$expected : string

type name

$value : mixed

real value

Return values
bool —

True if valid.

isValidArray()

Determine if all items of an array match the expected type.

protected isValidArray(string $expected, array<string|int, mixed> $values) : bool

This does not modify the errors.

Parameters
$expected : string

type name

$values : array<string|int, mixed>

list of real array values.

Return values
bool —

True if valid.

lookupClass()

Determine the full namespaced version of this class.

protected lookupClass(string $name) : string|null

Unfortunately, if the typed comment has no namespace we can't magically get the namespace of that class. At least, not without triggering the autoloader in some way.

So users must specify a namespaces() method in which classes may exist.

Alternatively, put the whole namespaced class name in the doc comment.

Parameters
$name : string
Return values
string|null —

null if not found.

On this page

Search results