Download the PHP package emirhanbalkac/utf8-supported-mapper-php without Composer
On this page you can find all versions of the php package emirhanbalkac/utf8-supported-mapper-php. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package utf8-supported-mapper-php
PHP Model Mapper
Feature list:
- Map PHP class properties from json, xml, arrays and other objects.
- Unmap from model to json, xml, array and stdClass
- Nested models supported
- Annotations provide custom naming, types and rules for model properties
- Properly annotated, mapped models can be validated for different types and rules
Installation
How it works
The mapper goes trough all the property names (with @name you could change property name mappings) of your model and maps the ones with the same name from the source (json/xml/array/stdClass), if available. The mapped model can be used as is, but it is recommended you validate it after mapping. Required properties (@required) must always be set and of the appropriate type (if set with @var or @rule). Not required properties may not be set (null), but if set, must match defined types or rules (if set with @var or @rule) to pass validation. So, if you have:
You could make a model like this:
Validation would fail if:
- You use createSomeGuy action, without (null) $type (required only on createSomeGuy action)
- Without (null) $name (always required)
- You use createSomething action, and $type is not a string
- $name is not a string
- $age is set (not null), but not an integer, or between 1 and 150 (rule)
- $isEmployed is set (not null) but not a boolean
Note that $description would get mapped because it has the @name annotation set, and there is a property in the source matching that name (custom-desc).
Annotations
Heres a list of annotations we can use in models:
annotation | used above | description |
---|---|---|
@var | property | Used to declare the type of the property (validation). This is an alias for @rule annotation. See below for a list of pre-defined types (rules) for model validation |
@name | property | Used to change the property name, while mapping and unmapping (can be used with special characters, but be careful if youre working with XML) |
@required | property | Used to declare that a property is required for a specific action (validation). Use without the action to make the property always required |
@rule | property | Used to enforce specific rules and filters on the property value (validation). You can use the predefined rules or create and import your own |
@xmlRoot | class | Used to name the xml root name of the element (only for xml mapping) |
@xmlVersion | class | Version of xml document (only for xml mapping) |
@xmlEncoding | class | Encoding document. Default utf-8. (only for xml mapping) |
@xmlNamespaces | class | Document namespaces. Without prefix xmlns. Example : "http://www.sitemaps.org/schemas/sitemap/0.9" xhtml="http://www.w3.org/1999/xhtml". (only for xml mapping) |
@xmlAttribute | property | Used to declare that a property is a xml attribute for the element. Also used for namespaces (only for xml mapping) |
@xmlNodeValue | property | Used to declare that a property contains the text node value (only for xml mapping). Please see the models in tests/ (XmlTestModel) |
@var annotation type list:
type | description |
---|---|
any | anything |
string | string type |
integer | integer type |
boolean | boolean type |
double | double type |
float | float type |
array | array type |
[] | array type |
object | stdClass for example |
string[] | string array type |
integer[] | integer array type |
boolean[] | boolean array type |
object[] | object array type |
NamedModel | any named class (model) (if from another namespace, make sure you include it!) |
NamedModel[] | any model array |
Usage
Mapping
Use MappableTrait inside your model to call methods from the model itself:
Or by using the ModelMapper class, providing it with the instance of the model you want mapped and the source object:
Unmapping
Use ConvertibleTrait inside your model to call methods from the model itself:
Or by using the ModelMapper class, providing it with the mapped model (converts to stdClass):
Validation
Validation will check your mapped model's property types, custom rules, and whether the property is required or not.
Validate your mapped models with ValidatableTrait, by providing it with the desired validation action:
Or if you want to validate only the properties that are always required:
You can also use the validator itself:
Rules
Rules are used for custom validation of mapped property values.
This would check if the property value contains a valid email string.
You can pass additional parameters for rules.
If setup properly this custom rule would check if the property value is between 0 and 99.
Rule setup
Lets use the limit rule example from above, to create a new custom rule:
In the example above, we can configure a new rule, by:
- Defining an array of names (aliases) which serve as the name of the rule in the annotation (getNames)
- Providing a validation definition and throwing an exception if it doesnt pass (validate)
- The rule parameters (0,99) come in through the $params array, in order in which they were provided
For more information please take a look at the pre-defined rule classes and the IRule interface.
Loading your rules
In the example above we can use custom rules by providing them to the validator one by one (useRule) or providing a path to a directory containing rules (loadRules).
Code examples
Please see the tests and models used in it.