Type classes with built-in validation logic
PHP classes that encapsulate data validation around less precise types, (*1)
types is a library for PHP 7.0+ that encapsulates validation logic in the constructor of small classes, allowing these already-validated, well-defined instances to be passed around. A "type" here can be anything that has reasonable validation logic, such as an IP address class that wraps a string. By centralizing validation to that class constructor, the whole application can accept and emit an IP address instance knowing the sender and consumer (respectively) don't need to do any validation on what would have been previously a string., (*3)
This is not a new concept; ancarda/types tries to define many common, useful types that can be extended in your application., (*4)
These classes can be used with any framework and have no dependencies other than the built-in JSON extension. This library may be installed via composer with the following command:, (*5)
composer require ancarda/types
Types can then be initalized, like so:, (*6)
<?php use \Ancarda\Type\Math\UInt8; $b = new UInt8(102); // $b can now be passed to a function that accepts UInt8. // Consumer doesn't need to check $b is 0 to 255.