Download the PHP package samleybrize/valoa without Composer
On this page you can find all versions of the php package samleybrize/valoa. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download samleybrize/valoa
More information about samleybrize/valoa
Files in samleybrize/valoa
Package valoa
Short Description Easy value objects and entities
License MIT
Homepage https://github.com/samleybrize/valoa
Informations about the package valoa
Valoa - Easy value objects and entities
Installation
For a quick install with Composer use :
$ composer require samleybrize/valoa
Requirements
- PHP 5.4+
Usage
Traditionnaly, when you build value objects (or entities), you define properties as private and then you create a getter (and maybe a setter) for each of them.
Valoa handles get and set operations without the need to create any method. All you need is to use the Samleybrize\Valoa\ValueObject\ValueObjectTrait
trait.
Additionally, you can define constraints to your properties. On the example above, $var1
only accepts integer values, as specified by the @var
tag.
Validation strictness
By default, validation is non-strict. For some validators, non-strict validation allows you to give a value of a different type that will be converted.
For an integer property, you can set a string 123azerty
that will be converted to the integer 123
.
Strict validation is enabled with the @strict
tag. If strict validation is enabled on an integer property, only integers will be accepted.
Define the validator used
By default, the @var
tag define which validator will be used. If for somme reason you need to put some funky value into that tag, you have to specify
the validator with the @validator
tag. If there is a @validator
tag, the @var
tag is always ignored.
If you don't want any validation on a property, use the any
validator.
Immutable property
A property can be made immutable with the @immutable
tag. Immutable properties can't be setted from outside.
You can make all properties immutable at once by setting the @immutable
tag on the class.
Nullable property
A property can accept a null
value with the @nullable
tag.
Array types
Array types can be specified in several ways. The simplest method is to add []
to the tag. This way you can also validate multidimensional arrays,
by adding as many []
as you want.
The other way of specifying array types is @var array
and @validator
setted to the underlying type. If there is no @validator
tag, the validator any
is used by default. This method does not allow to validate multidimensional arrays.
Enum validator
The enum validator allows you to define a list of accepted values with the @enum
tag. Each allowed value should be scalar.
Class constants validator
The class constants validator define a set of class constants as allowed values. The @classname
tag specify the full class name that contains
allowed class constants. By default, all class constants in that class are allowed. You can restrict allowed values with the followin options :
- The
@contain
tag is a filter on the class constants names - The
@beginWith
tag is a filter on the beginning of class constants names - The
@endWith
tag is a filter on the end of class constants names
Validator list
Validator | Options | Description | Non strict |
---|---|---|---|
Any | Allow any value | ||
Boolean | Allow boolean values | Any scalar value | |
ClassConstants | Allow constants values of a given class | Any scalar value | |
beginWith |
Filter constant names | ||
endWith |
Filter constant names | ||
contain |
Filter constant names | ||
Allow email adresses | |||
Enum | Allow a predefined list of values | Any scalar value | |
Float | Allow decimal values | Any scalar value | |
min |
Min valid value | ||
max |
Max valid value | ||
Integer | Allow integer values | Any scalar value | |
min |
Min valid value | ||
max |
Max valid value | ||
Ip | Allow IP addresses | ||
String | Allow strings | Any scalar value | |
minLength |
Min valid string length | ||
maxLength |
Max valid string length | ||
regex |
Validation regex (ex: ^[0-9]+$) |
Write your own validator
Custom validators can be created by implementing the Samleybrize\Valoa\ValueObject\Validator\ValidatorInterface
interface.
To use it, you have to specify the full class name on the @validator
tag.
Lazy loaders
Sometimes you want to retrieve some data on demand. All properties accept an instance of the Samleybrize\Valoa\ValueObject\ValueObjectLazyLoaderInterface
interface
regardless of its associated validator. When you first try to get its value, the lazy loader is used to retrieve the effective value
and validates it using the validator. Next times, the lazy loader is no longer used.
Known limitations
Array types can't be directly modified from outside. Instead, you have two workarounds :
- Retrieve and modify the array externally then set the whole array
- Create a method that modify the array from inside
Author
This project is authored and maintained by Stephen Berquet.
License
Valoa is licensed under the MIT License - see the LICENSE
file for details.