PHP Class Garden\Schema

Inheritance: implements JsonSerializable
Datei anzeigen Open project: vanilla/vanilla

Protected Properties

Property Type Description
$description string
$schema
$types
$validationBehavior integer
$validators An array of callbacks that will custom validate the schema.

Public Methods

Method Description
__construct ( array $schema = [] ) Initialize an instance of a new {@link Schema} class.
addValidator ( string $fieldname, callable $callback ) : Schema Add a custom validator to to validate the schema.
create ( array $schema = [] ) : Schema Create a new schema and return it.
dumpSpec ( ) : array Build an OpenAPI-compatible specification of the current schema.
getDescription ( ) : string Grab the schema's current description.
getValidationBehavior ( ) : integer Return a Schema::VALIDATE_* constant representing the currently configured validation behavior.
isValid ( array &$data, Validation &$validation = null ) : boolean Validate data against the schema and return the result.
jsonSerialize ( ) : mixed Specify data which should be serialized to JSON.
parseSchema ( array $arr ) : array Parse a schema in short form into a full schema array.
parseShortParam ( string $str, array $other = [] ) : array Parse a short parameter string into a full array parameter.
requireOneOf ( array $fieldnames, integer $count = 1 ) : Schema Require one of a given set of fields in the schema.
setDescription ( string $description ) : Schema Set the description for the schema.
setValidationBehavior ( integer $validationBehavior ) : Schema Set the validation behavior for the schema, which determines how invalid properties are handled.
validate ( array &$data, Validation &$validation = null ) : boolean Validate data against the schema.

Protected Methods

Method Description
filterData ( array &$data, array $schema, Validation &$validation, string $path = '' ) : Schema Filter fields not in the schema. The action taken is determined by the configured validation behavior.
isValidInternal ( array &$data, array $schema, Validation &$validation = null, string $path = '' ) : boolean Validate data against the schema and return the result.
validateArray ( &$value, array $field, Validation $validation ) : boolean Validate an array.
validateBase64 ( &$value, array $field, Validation $validation ) : boolean Validate a base64 string.
validateBoolean ( &$value, array $field, Validation $validation ) : boolean Validate a boolean value.
validateDatetime ( &$value, array $field, Validation $validation ) : boolean Validate a date time.
validateField ( &$value, array $field, Validation $validation ) : boolean Validate a field.
validateFloat ( &$value, array $field, Validation $validation ) : boolean Validate a float.
validateInteger ( &$value, array $field, Validation $validation ) : boolean Validate and integer.
validateObject ( &$value, array $field, Validation $validation ) : boolean Validate an object.
validateRequired ( &$value, array $field, Validation $validation ) : boolean | null Validate a required field.
validateString ( &$value, array $field, Validation $validation ) : boolean Validate a string.
validateTimestamp ( &$value, array $field, Validation $validation ) : boolean Validate a unix timestamp.

Private Methods

Method Description
arraySelect ( array $keys, array $array, mixed $default = null ) : mixed Select the first non-empty value from an array.
strEnds ( string $haystack, string $needle ) : boolean Returns whether or not a string ends with another string.

Method Details

__construct() public method

Initialize an instance of a new {@link Schema} class.
public __construct ( array $schema = [] )
$schema array The array schema to validate against.

addValidator() public method

Add a custom validator to to validate the schema.
public addValidator ( string $fieldname, callable $callback ) : Schema
$fieldname string The name of the field to validate, if any.
$callback callable The callback to validate with.
return Schema Returns `$this` for fluent calls.

create() public static method

Create a new schema and return it.
public static create ( array $schema = [] ) : Schema
$schema array The schema array.
return Schema Returns the newly created and parsed schema.

dumpSpec() public method

Build an OpenAPI-compatible specification of the current schema.
public dumpSpec ( ) : array
return array

filterData() protected method

Filter fields not in the schema. The action taken is determined by the configured validation behavior.
protected filterData ( array &$data, array $schema, Validation &$validation, string $path = '' ) : Schema
$data array
$schema array The schema array. Its configured parameters are used to filter $data.
$validation Validation
$path string The path to current parameters for nested objects.
return Schema Returns the current instance for fluent calls.

getDescription() public method

Grab the schema's current description.
public getDescription ( ) : string
return string

getValidationBehavior() public method

Return a Schema::VALIDATE_* constant representing the currently configured validation behavior.
public getValidationBehavior ( ) : integer
return integer Returns a Schema::VALIDATE_* constant to indicate this instance's validation behavior.

isValid() public method

Validate data against the schema and return the result.
public isValid ( array &$data, Validation &$validation = null ) : boolean
$data array
$validation Validation
return boolean Returns true if the data is valid. False otherwise.

isValidInternal() protected method

Validate data against the schema and return the result.
protected isValidInternal ( array &$data, array $schema, Validation &$validation = null, string $path = '' ) : boolean
$data array
$schema array The schema array to validate against.
$validation Validation
$path string The path to the current path for nested objects.
return boolean Returns true if the data is valid. False otherwise.

jsonSerialize() public method

Specify data which should be serialized to JSON.
public jsonSerialize ( ) : mixed
return mixed data which can be serialized by json_encode, which is a value of any type other than a resource.

parseSchema() public static method

Parse a schema in short form into a full schema array.
public static parseSchema ( array $arr ) : array
$arr array The array to parse into a schema.
return array The full schema array.

parseShortParam() public static method

Parse a short parameter string into a full array parameter.
public static parseShortParam ( string $str, array $other = [] ) : array
$str string The short parameter string to parse.
$other array An array of other information that might help resolve ambiguity.
return array Returns an array in the form [name, [param]].

requireOneOf() public method

Require one of a given set of fields in the schema.
public requireOneOf ( array $fieldnames, integer $count = 1 ) : Schema
$fieldnames array The field names to require.
$count integer The count of required items.
return Schema Returns `$this` for fluent calls.

setDescription() public method

Set the description for the schema.
public setDescription ( string $description ) : Schema
$description string
return Schema

setValidationBehavior() public method

Set the validation behavior for the schema, which determines how invalid properties are handled.
public setValidationBehavior ( integer $validationBehavior ) : Schema
$validationBehavior integer One of the Schema::VALIDATE_* constants.
return Schema Returns the current instance for fluent calls.

validate() public method

Validate data against the schema.
public validate ( array &$data, Validation &$validation = null ) : boolean
$data array
$validation Validation
return boolean Returns true if the data is valid, false otherwise.

validateArray() protected method

Validate an array.
protected validateArray ( &$value, array $field, Validation $validation ) : boolean
$field array The field definition.
$validation Validation The validation results to add.
return boolean Returns true if {@link $value} is valid or false otherwise.

validateBase64() protected method

Validate a base64 string.
protected validateBase64 ( &$value, array $field, Validation $validation ) : boolean
$field array The field definition.
$validation Validation The validation results to add.
return boolean Returns true if {@link $value} is valid or false otherwise.

validateBoolean() protected method

Validate a boolean value.
protected validateBoolean ( &$value, array $field, Validation $validation ) : boolean
$field array The field definition.
$validation Validation The validation results to add.
return boolean Returns true if {@link $value} is valid or false otherwise.

validateDatetime() protected method

Validate a date time.
protected validateDatetime ( &$value, array $field, Validation $validation ) : boolean
$field array The field definition.
$validation Validation The validation results to add.
return boolean Returns true if {@link $value} is valid or false otherwise.

validateField() protected method

Validate a field.
protected validateField ( &$value, array $field, Validation $validation ) : boolean
$field array Parameters on the field.
$validation Validation A validation object to add errors to.
return boolean Returns true if the field is valid, false otherwise.

validateFloat() protected method

Validate a float.
protected validateFloat ( &$value, array $field, Validation $validation ) : boolean
$field array The field definition.
$validation Validation The validation results to add.
return boolean Returns true if {@link $value} is valid or false otherwise.

validateInteger() protected method

Validate and integer.
protected validateInteger ( &$value, array $field, Validation $validation ) : boolean
$field array The field definition.
$validation Validation The validation results to add.
return boolean Returns true if {@link $value} is valid or false otherwise.

validateObject() protected method

Validate an object.
protected validateObject ( &$value, array $field, Validation $validation ) : boolean
$field array The field definition.
$validation Validation The validation results to add.
return boolean Returns true if {@link $value} is valid or false otherwise.

validateRequired() protected method

Validate a required field.
protected validateRequired ( &$value, array $field, Validation $validation ) : boolean | null
$field array The field definition.
$validation Validation A {@link Validation} object to collect errors.
return boolean | null Returns one of the following: - null: The field is not required. - true: The field is required and {@link $value} is not empty. - false: The field is required and {@link $value} is empty.

validateString() protected method

Validate a string.
protected validateString ( &$value, array $field, Validation $validation ) : boolean
$field array The field definition.
$validation Validation The validation results to add.
return boolean Returns true if {@link $value} is valid or false otherwise.

validateTimestamp() protected method

Validate a unix timestamp.
protected validateTimestamp ( &$value, array $field, Validation $validation ) : boolean
$field array The field definition.
$validation Validation The validation results to add.
return boolean Returns true if {@link $value} is valid or false otherwise.

Property Details

$description protected_oe property

protected string $description
return string

$schema protected_oe property

protected $schema

$types protected_oe static_oe property

protected static $types

$validationBehavior protected_oe property

protected int $validationBehavior
return integer

$validators protected_oe property

An array of callbacks that will custom validate the schema.
protected $validators