\Disco\classesDataModel

The DataModel class is used to ensure the validity and integrity of data. This is done by a defining a definition for each datum in the data model. The class ensures that any data defined by the definition conforms to its specified conditions, and when it does not it sets errors specific to each datum which can then be used to communicate what type of datum should have been passed.

Data models are primarly used to verify incoming user data from forms in the browser, but can also be used as contracts in class communications.

The only thing necessary to implement a data model is defining the data models definition in protected $definition = Array(). You would then use it in real code like:

$dm = new MyDataModel($data);
$dm->someField = 'foo';
$dm['otherField'] = 'bar';
if(!$dm->verify()){
     $errors = $dm->getErrors();
}

Notice that data models can be accessed via object sytanx and array access sytax, this is because the data model class implements \ArrayAccess and implements the necessary methods for object access.

Summary

Methods
Properties
Constants
__construct()
verify()
verifyOnly()
verifySetData()
verifyData()
getError()
getErrors()
clearErrors()
getAllDefinitions()
getDefinition()
setDefinition()
getDefinitionValue()
getData()
__get()
__set()
__isset()
__unset()
offsetGet()
offsetSet()
offsetExists()
offsetUnset()
No public properties found
No constants found
setError()
_postMassage()
_isValidInt()
_isValidFloat()
_isBetweenMinAndMax()
$definition
$data
$errors
$defaultErrorMessage
N/A
No private methods found
No private properties found
N/A

Properties

$definition

$definition : array

Type

array — Definitions of the data this data model contains. A definition is defined by a key, which is the name of the data/field, and an array which defines the conditions verifying the data/field. Conditions used to define a data/field: - `type` : `string` (int, uint, float, ufloat, string, boolean, char, array, object, closure) - `nullable` : `boolean` (optional) Data can be null. - `truthy` : `boolean` (optional) Data can be a boolean. - `in` : `array` (optional) Data must be contained in this array. - `notin` : `array` (optional) Data must NOT be contained in this array. - `instanceof` : `string` (optional)(only applies to object) Data must be an instance of class. - `max` : `numeric` (optional)(only applies to int, uint, float, ufloat) Max value of data. - `min` : `numeric` (optional)(only applies to int, uint, float, ufloat) Min value of data. - `maxlen` : `int` (optional)(only applies to string) Max length of data. - `minlen` : `int` (optional)(only applies to string) Min length of data. Conditions that when set will disregard all conditions listed above (aka are mutually exclusive with any other condition): - `regexp` : `string` A regular expression that the data must match, can be one of the default matching conditions provided by Disco. - `method` : `string` The name of a method of the class that is passed the value of the data and returns a boolean value indicating whether the data is valid. Conditions that aren't mutually exclusive to any data type and can be used to augment a piece of data: - `required` : `boolean` Whether the data is required in the data model. - `default` : `mixed` A default value to give the data when its not set, mutually exclusive with `required`. - `premassage` : `string` The name of the method of the class that can massage the data value prior to validation. - `postmassage` : `string` The name of the method of the calss that can massage the data value after successfull validation. - `error` : `string` The error message returned when the data doesn't validate. If this is not set a custom one will be generated for you based on the conditions.

$data

$data : array

Type

array — The data set in the data model.

$errors

$errors : array

Type

array — The errors that occured while validating the data.

$defaultErrorMessage

$defaultErrorMessage : string

Type

string — The default error message used for general data errors. Use `%1\$s` in the string to get the key of the data the error message is regarding.

Methods

__construct()

__construct(array  $data = array()) 

Set initial data in the data model. Any keys set in the passed data that do not exist in the definition will be disregarded.

Parameters

array $data

Initial data to set in the model, will be merged with any values already set in data.

verify()

verify() : boolean

Verify the data in the data model. If the data model failed to verify use the method `getErrors()` to get a list of the errors in the model.

Returns

boolean —

Whether the data in the data model passed verification.

verifyOnly()

verifyOnly(array  $fields) : boolean

Verify only the passed fields data. This comes in handy when you only want to verify a subset of the data models definition.

Parameters

array $fields

The field data keys to verify.

Returns

boolean —

Whether the data in the passed fields passed verification.

verifySetData()

verifySetData() : boolean

Verify only data that has been set in the data model and not all data defined by the definitions.

Returns

boolean —

Whether the set data passed verification.

verifyData()

verifyData(string  $key) : boolean

Verify a single piece of data in the model. If the data fails to validate its corresponding error message will be set in the errors by the same key.

Parameters

string $key

The key of the data.

Throws

\Disco\exceptions\Exception

When the data model doesn't define the field specified by $key.

Returns

boolean —

Whether the data passed verification.

getError()

getError(string  $key) : boolean|string

Get an error.

Parameters

string $key

The data key.

Returns

boolean|string —

False if no error for $key, otherwise the error string.

getErrors()

getErrors() : array

Get all the errors in the data model. Errors being set are dependent on calling the methods `verify` and `verifyData`.

Returns

array

clearErrors()

clearErrors() 

Clear all errors from the data model.

getAllDefinitions()

getAllDefinitions() : array

Get all the data models definitions.

Returns

array —

The entire data model definition.

getDefinition()

getDefinition(string  $key) : array

Get a datums definition.

Parameters

string $key

The data key.

Returns

array —

The definition.

setDefinition()

setDefinition(string  $key, array  $value) 

Set a datums definition. Cannot overwrite previously defined definitions.

Parameters

string $key

The definition key.

array $value

The definition.

Throws

\Disco\exceptions\Exception

When setting the definition would override a frozen data model definition.

getDefinitionValue()

getDefinitionValue(string  $key, string  $condition) 

Get a single value that resides within a single data definition.

Parameters

string $key

The definition key.

string $condition

The definition key child key.

getData()

getData() : array

Get all the data set in the data model.

Returns

array

__get()

__get(string  $key) : mixed

Get a field of the data model using object syntax.

Parameters

string $key

The key.

Returns

mixed

__set()

__set(string  $key, mixed  $value) 

Set a field of the data model using object syntax.

Parameters

string $key

The key.

mixed $value

The value.

Throws

\Disco\exceptions\Exception

When the data model doesn't support that field.

__isset()

__isset(string  $key) : boolean

Determine whether a field exists in the data model.

Parameters

string $key

The key.

Returns

boolean

__unset()

__unset(string  $key) 

Delete a field from the data model (Set it to null).

Parameters

string $key

The key.

offsetGet()

offsetGet(string  $key) : mixed

Get a field of the data model using array access syntax.

Parameters

string $key

The key.

Returns

mixed

offsetSet()

offsetSet(string  $key, mixed  $value) 

Set a field of the data model using array access syntax.

Parameters

string $key

The key.

mixed $value

The value.

Throws

\Disco\exceptions\Exception

When the data model doesn't support that field.

offsetExists()

offsetExists(string  $key) : boolean

Determine whether a field exists in the data model.

Parameters

string $key

The key.

Returns

boolean

offsetUnset()

offsetUnset(string  $key) 

Delete a field from the data model (Set it to null).

Parameters

string $key

The key.

setError()

setError(string  $key,   $error = null) : boolean

Set an error.

Parameters

string $key

The data key.

$error

Returns

boolean —

Always returns false, which eases chaining in the verifyData method.

_postMassage()

_postMassage(string  $key, mixed  $value) 

Apply a post massage function to a data value.

Parameters

string $key

The data key.

mixed $value

The data value.

_isValidInt()

_isValidInt(string  $key, mixed  $value) : boolean

Determine if the value is a valid int.

Parameters

string $key

The data key.

mixed $value

The data value.

Returns

boolean

_isValidFloat()

_isValidFloat(string  $key, mixed  $value) : boolean

Determine if the value is a valid float.

Parameters

string $key

The data key.

mixed $value

The data value.

Returns

boolean

_isBetweenMinAndMax()

_isBetweenMinAndMax(string  $key, mixed  $value) : boolean

Determine if the value is greater than `min` if set, and less than `max` if set.

Parameters

string $key

The data key.

mixed $value

The data value.

Returns

boolean