Download the PHP package peroks/model without Composer
On this page you can find all versions of the php package peroks/model. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Package model
Short Description Models: Classes with built-in data validation
License MIT
Informations about the package model
Models: Classes with built-in data validation
The Model
class extends the built-in PHP ArrayObject
class with internal
data validation and json support.
Reason why
Models can be used in any context, but they are especially useful in REST API applications, where validating incoming and outgoing data is often time-consuming, inconsistent, error-prone and hard to read/maintain.
Instead of bloating your project with code for validating data, you can let models validate themselves based on their property definition. You just define the properties and constraints of each model in one single place, and then validate model instances throughout your application in a consistent, efficient and clear manner.
Models are similar to database tables, where each model property corresponds to a table column. Install the Model Store package to automatically create database tables based on your models.
How to use
Create a model class
You define the model properties and constraints in a separate class for each
model. Each model class must contain the static $properties
property and
extend the Model
class or a subclass.
In the example below, we create a geo point model with two properties: latitude and longitude. Both properties are required, and they must be floating numbers in the range of -90 to 90 (latitude) and -180 to 180 (longitude).
You can extend models like any other class. The properties are inherited from the parent classes.
Create a model instance
There are several ways to create a model instance. The model constructor takes an assoc array, an object (including a model instance) or a json string. All the options below produce the same result.
Or you just create an empty model and add the property values later on.
Just like the ArrayObject
parent class, you can also set (and get) model
properties like an array.
Model validation
Since each model knows its property definitions and constraints, validating a model is a breeze.
Alternatively, you can let the validation throw a ModelException
on failure.
Models are not validated on creation, only when Model::validate()
is called.
Getting the model data
You can access the model data as an object or array
or get the model data as an assoc array.
JSON encoding
You can easily convert a model to JSON.
Nested models
Models can contain other models. You just add model
with a class name to an
object
or array
property.
If you add default values for sub-models, they are also created when the main model is created. On validation, sub-models are validated recursively too.
Nested models are especially useful for importing complex data structures from external sources. With models, decoding, converting and validating external data is a one-liner.
Supported property items
The PRIMARY
, INDEX
, UNIQUE
, FOREIGN
and MATCH
property items are only
used by peroks/model-store
, a separate package for storing models in databases.
There, they are used for creating indices and constraints.
VALUE
and PROPERTIES
are ony used for exporting model data
including the model definition with Model::data( ModelData::PROPERTIES )
.
Supported property types
Installing
You need composer to download and install this package.
Just run composer require peroks/model
in your project.