dev-master
9999999-devUtility traits for type-safe & immutable entity in PHP
MIT
The Development Requires
by fsubal
0.1
0.1.0.0Utility traits for type-safe & immutable entity in PHP
MIT
The Development Requires
by fsubal
Utility traits for type-safe & immutable entity in PHP
Utility traits for type-safe & immutable entity in PHP., (*1)
https://packagist.org/packages/fsubal/donphan, (*2)
composer require fsubal/donphan
In PHP application without certain frameworks, array
s are often used as domain models., (*3)
<?php $user = [ 'user_id' => validateInt($_POST['user_id']), 'name' => validateString($_POST['name']), ]; ... function doSomethingForUser(array $user) { $name = $user['name']; ... }
When refactoring these mess (or migrating to completely new framework) seems too hard, Donphan may help you., (*4)
It provides two utility traits., (*5)
\Donphan\Validatable
: provides ::validate
method, and lifecycle hooks.\Donphan\Immutable
: provides ::from
factory method.<?php final class User { use \Donphan\Immutable; const REQUIRED = [ 'user_id' => 'numeric', 'name' => 'string' ]; } // and then $user = User::from([ 'user_id' => $_POST['user_id'], 'name' => $_POST['name'] ]); function doSomethingForUser(User $user) { $name = $user->name; ... }
Not perfect, but now you have type safety with ease., (*6)
use \Donphan\Immutable
insideconst REQUIRED = [...]
, and const OPTIONAL = [...]
if you want.static function
s beforeTypeCheck
or afterTypeCheck
(See Lifecycle methods)Then, you can use YourClass::from(array $params)
or YourClass::validate(array $params)
., (*7)
Donphan supports these types., (*8)
mixed
int
float
numeric
string
boolean
array
class
name.numeric
looks like an original type ? Yes, but it is just validated by is_numeric
( https://secure.php.net/manual/en/function.is-numeric.php )., (*9)
It is almost like int|string
, useful for some ids in $_GET
or $_POST
or something., (*10)
These types are not supported., (*11)
null
object
resource
callable
Closure
Exception
Error
If you want to have default value ? Or if you allow additional validation to your Immutable
object ?, (*12)
Then, the lifecycle methods might be needed., (*13)
\Donphan\Validatable
provides two lifecycle methods., (*14)
beforeTypeCheck
: Executed just before type checking. It gets original params, and you must return an array
.afterTypeCheck
: Executed just after type checking. It gets original params, and you must NOT return anything.beforeTypeCheck
is useful for mutating the original array, and afterTypeCheck
is useful for performing additional validations., (*15)
Example below, (*16)
final class User { use \Donphan\Immutable; const REQUIRED = [ 'user_id' => 'numeric', 'name' => 'string', 'birthday' => '\DateTimeImmutable' ]; const OPTIONAL = [ 'url' => 'string' ]; public static function beforeTypeCheck(array $params) { if (!isset($params['url'])) { $params['url'] = 'https://example.com'; } return $params; } public static function afterTypeCheck(array $params) { if (strlen($params['name']) == 0) { throw new \InvalidArgumentException('params[name] must not be empty!'); } } }
Note that the url
added in beforeTypeCheck
is also type checked (if it is written in REQUIRED
or OPTIONAL
)., (*17)
PHP 5.6+ ( Needs to be writable const
with array in classes ), (*18)
This is licensed under MIT License., (*19)
Utility traits for type-safe & immutable entity in PHP
MIT
Utility traits for type-safe & immutable entity in PHP
MIT