phramework doc
  • Namespace
  • Class
  • Tree
  • Deprecated
  • Todo

Namespaces

  • Phramework
    • Authentication
    • Database
      • Operations
    • Exceptions
    • Extensions
    • Models
    • URIStrategy
    • Validate
    • Viewers

Classes

  • AllOf
  • AnyOf
  • ArrayValidator
  • BaseValidator
  • BooleanValidator
  • DatetimeValidator
  • DateValidator
  • EmailValidator
  • EnumValidator
  • IntegerValidator
  • NumberValidator
  • ObjectValidator
  • OneOf
  • StringValidator
  • UnsignedIntegerValidator
  • URLValidator
  • UsernameValidator
  • Validate
  • ValidateResult

Class Validate

Provides various methods for validating data for varius datatypes.

Namespace: Phramework\Validate
Deprecated: since 0.0.0
License: Apache-2.0
Author: Xenofon Spafaridis nohponex@gmail.com
Located at Validate.php

Methods summary

public static
# registerCustomType( string $type, function $callback )

Register a custom data type

Register a custom data type

It can be used to validate models

Parameters

$type
$callback

Throws

Exception
public static
# validateCustomType( string $type, mixed $value, string $field_name, array $model = [] )

Validate a custom data type.

Validate a custom data type.

This method uses previous custom-defined datatype to validate it's data.

Parameters

$type
Custom type's name
$value
Value to test
$field_name
[optional] field's name
$model
[optional]

Throws

Exception
type_not_found
IncorrectParameters
if validation fails
public static boolean
# model( array & $parameters, array $model )

Validate a model

Validate a model

This method accepts a request model, and validates. The values of $parameters array might be changed due to type casting.

Parameters

$parameters
Request parameters
$model
Model used for the validation

Returns

boolean

Throws

Exception
IncorrectParameters
If any field is incorrect
MissingParameters
If any required field is missing
public boolean
# isValidJsonpCallback( string $subject )

Check if callback is valid

Check if callback is valid

Parameters

$subject

Returns

boolean

Link

source
public static integer
# int( string|integer $input, integer|null $min = null, integer|null $max = null, string $field_name = 'int' )

Validate a signed integer

Validate a signed integer

Parameters

$input
Input value
$min
Minimum value. [optional] Default is NULL, if NULL then the minum value is skipped
$max
Maximum value. [optional] Default is NULL, if NULL then the maximum value is skipped
$field_name
[optional] Field's name, used in IncorrectParametersException. Optional default is int

Returns

integer
Returns the value of the input value as int

Throws

IncorrectParameters
If valu type is not correct.
public static integer
# uint( string|integer $input, integer $min = 0, integer|null $max = null, String $field_name = 'uint' )

Validate an unsigned integer

Validate an unsigned integer

Parameters

$input
Input value
$min
Minimum value. Optional default is 0.
$max
Maximum value. Optional default is NULL, if NULL then the maximum value is skipped
$field_name
Field's name, used in IncorrectParametersException. Optional default is uint

Returns

integer
Returns the value of the input value as int

Throws

IncorrectParameters
If valu type is not correct.
public static float
# float( string|float|integer $input, float|null $min = null, float|null $max = null, string $field_name = 'float' )

Validate a floating point number

Validate a floating point number

Parameters

$input
$min
Minimum value. Optional default is NULL, if NULL then the minum value is skipped
$max
value. Optional default is NULL, if NULL then the maximum value is skipped
$field_name

[optional] Field's name, used in IncorrectParametersException. Optional default is number

Returns

float
Returns the input value as float

Throws

IncorrectParameters
If value type is not correct.
public static float
# double( string|float|float|integer $input, float|null $min = null, float|null $max = null, string $field_name = 'double' )

Validate a double presision floating point number

Validate a double presision floating point number

Parameters

$input
$min
Minimum value. Optional default is NULL, if NULL then the minum value is skipped
$max
value. Optional default is NULL, if NULL then the maximum value is skipped
$field_name

[optional] Field's name, used in IncorrectParametersException. Optional default is number

Returns

float
Returns the input value as double

Throws

IncorrectParameters
If value type is not correct.
public static string
# email( string $input, string $field_name = 'email' )

Validate an email address

Validate an email address

Parameters

$input
$field_name

[optional] Field's name, used in IncorrectParametersException. Optional default is email

Returns

string
Return the email address

Throws

IncorrectParameters
If value type is not correct.
public static string
# url( string $input, string $field_name = 'url' )

Validate a url

Validate a url

Parameters

$input
$field_name

[optional] Field's name, used in IncorrectParametersException. Optional default is url

Returns

string
Return the url

Throws

IncorrectParameters
If value type is not correct.
public static string
# permalink( string $input, string $field_name = 'permalink' )

Validate a permalink id

Validate a permalink id

Parameters

$input
$field_name

[optional] Field's name, used in IncorrectParametersException. Optional default is permalink

Returns

string
Return the permalink

Throws

IncorrectParameters
If value type is not correct.
public static string
# token( string $input, string $field_name = 'token' )

Parameters

$input
$field_name

[optional] Field's name, used in IncorrectParametersException. Optional default is token

Returns

string
Return the token

Throws

IncorrectParameters
If value type is not correct.
public static returns
# enum( string|integer $input, array $values, string $field_name = 'enum' )

Check if input value is in allowed values

Check if input value is in allowed values

Parameters

$input
Input array to check
$values
Array of strings or number, defines the allowed input values
$field_name

[optional] Field's name, used in IncorrectParametersException. Optional default is enum

Returns

returns
the value of the input value

Throws

IncorrectParameters
If valu type is not correct.
public static string
# sqlDate( string $date, string $field_name = 'date' )

Validate SQL date, datetime

Validate SQL date, datetime

Parameters

$date
Input date
$field_name

[optional] Field's name, used in IncorrectParametersException. Optional default is date

Returns

string

Throws

IncorrectParameters
If value type is not correct.
public static string
# color( type $input, string $type = Validate::COLOR_HEX, string $field_name = 'color' )

Validate color

Validate color

Parameters

$input
Input color
$type
Color value type. Optional, default is hex
$field_name

[optional] Field's name, used in IncorrectParametersException. Optional default is color

Returns

string

Throws

IncorrectParameters
If value type is not correct.

Todo

Implement additional types
public static string
# operator( string $operator, string $field_name = 'operator' )

Validate an operator

Validate an operator

Parameters

$operator
$field_name
[optional]

Returns

string

Throws

IncorrectParameters
If value type is not correct.
public static string
# regexp( string $input, string $regexp, string $field_name = 'regexp' )

Validate a regexp

Validate a regexp

Parameters

$input
input
$regexp
regexp
$field_name
[optional]

Returns

string

Throws

IncorrectParameters
If value type is not correct.
public static boolean
# boolean( mixed $input, string $field_name = 'boolean' )

Validate a regexp

Validate a regexp

Parameters

$input
input
$field_name
[optional]

Returns

boolean

Throws

IncorrectParameters
If value type is not correct.

Constants summary

string REQUIRED

Required field

Required field

# 'required'
string TYPE_TEXT

Text type

Text type

# 'text'
string TYPE_STRING

Alias of Text type

Alias of Text type

# 'text'
string TYPE_TEXTAREA

Multiline text type

Multiline text type

# 'textarea'
string TYPE_INT

Signed integer type

Signed integer type

# 'int'
string TYPE_UINT

Unsigned integer type

Unsigned integer type

# 'uint'
string TYPE_FLOAT

Floating point number type

Floating point number type

# 'float'
string TYPE_DOUBLE

Double presision floating point number type

Double presision floating point number type

# 'double'
string TYPE_BOOLEAN

boolean type

boolean type

# 'boolean'
string TYPE_COLOR

Color type

Color type

# 'color'
string TYPE_USERNAME
# 'username'
string TYPE_EMAIL
# 'email'
string TYPE_PASSWORD
# 'password'
string TYPE_TOKEN
# 'token'
string TYPE_URL
# 'url'
string TYPE_PERMALINK
# 'permalink'
string TYPE_DATE
# 'date'
string TYPE_DATETIME
# 'datetime'
string TYPE_REGEXP
# 'regexp'
string TYPE_UNIX_TIMESTAMP

Unix timestamp (unsigned integer)

Unix timestamp (unsigned integer)

# 'unix_timestamp'
string TYPE_ENUM

This type allows only specific values

This type allows only specific values

when used in model an array named values MUST be set for example 'values' => [1, 2, 'abc']

# 'enum'
string TYPE_JSON
# 'json'
string TYPE_JSON_ARRAY
# 'json_array'
string TYPE_ARRAY
# 'array'
string TYPE_ARRAY_CSV

Comma separated array

Comma separated array

When used in a Validate::model it splits the values , validates the subtype and returns as array

Example

aaa,bb,cc,1,2 Example parameter data
# 'array_csv'
string REGEXP_RESOURCE_ID

Regular expression of resource ID

Regular expression of resource ID

# '/^d+$/'
string REGEXP_USERNAME

Regular expresion of username

Regular expresion of username

# '/^[A-Za-z0-9_\.]{3,64}$/'
string REGEXP_TOKEN

Regular expresion of a token

Regular expresion of a token

# '/^[A-Za-z0-9_]{3,48}$/'
string REGEXP_PERMALINK

Regular expresion of a permalink

Regular expresion of a permalink

# '/^[A-Za-z0-9_]{3,32}$/'
string COLOR_HEX
# 'hex'
phramework doc API documentation generated by ApiGen