Download the PHP package livaa/custom-datatypes without Composer
On this page you can find all versions of the php package livaa/custom-datatypes. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package custom-datatypes
Install :
PHP Custom Datatypes
- An efficient oriented-object pattern to process the users inputs & to represent the data into your app.
- Does avoid the duplication of your validation & normalization rules.
- A clear view of all the data that traverse & compose your app, they are part of the app architecture !
- Data integrity, data integrity and data integrity.
- Force the developper to make a strong use of type hinting with all the benefits that come with it.
The main idea
Every each data must be wrap into it's own relative object. An age, an url, a city, a login, a height, a width, an rgb color, an id... type them all !
How to use ?
To build a custom datatype just extend Livaa\CustomDatatypes\CustomDatatype and write the validate() method.
So you can use it in your controllers :
This is what the NewsletterManager class would eventually looks like.\ That's just an example but the idea is to type-hint your classes to receive custom datatypes objects rather that natives typed variables. It does make the code more readable, easier to maintain and secure. Also it makes the whole app more reliable as the received values are always good. Eventually you'll finally get rid off all those dirty validation processes you have there and there (Utils::iKnowYouKnowWhatImTalkingAbout() !)
To extract the data from the custom datatype, use the getValue() method
Thanks to the __toString() method, a custom datatype can behave as a string when needed.\ Note that PDO will trigger the toString() method, so you can directly send your datatypes objects into your prepared requests.
If you find this a bit mind boggling, just remember to use getValue() everytime except
Keep it simple, avoid writing more methods into your datatypes. They are just supposed to verify and represent the value it does encapsulate, nothing else.
Error handling
Call $this->error("error_message") inside the validate() method in case of error, a CustomDatatypeException will be throw by default.
If the datatype is called with the second parameter ($throw_exceptions) to false, the exception(s) won't be throw but collected then accessible via $this->getErrors().\ Note that in this case, you may be collecting multiple exceptions (depends how is coded the validation process), that's why getErrors() is plural.