CakePHP DTO Plugin
The goal of this package is to structure "unstructured data", replacing simple (associative) arrays with a more speaking solution.
- By making all fields typeable, we can be sure that their values are never something we didn't expect. Especially with PHP 7.1 and more strict typehinting moving forward this is rather important to detect and fail early.
- We can have full IDE autocomplete and typehinting.
- We can use tools like PHPStan to statically analyze the code (more strictly).
- We can simplify the logic where required fields will now just throw a meaningful exception.
- We can work with different inflections of field names more easily.
- Easy way of immutable DTOs with required fields, to trust them in following code.
For more see Motivation and Background.
This plugin will provide you with a tool to quickly generate custom and optimized DTOs for your special use cases.
This branch is for use with CakePHP 3.7+. For details see version map.
Live examples in the Sandbox: Github Demo.
Note: They are 5.6+ as per min requirement, but you can easily generate them as 7.1+ and strict types on as well :) In fact, that is the default if your PHP setup matches locally.
You can install this plugin into your CakePHP application using Composer.
The recommended way to install is:
composer require dereuromark/cakephp-dto
Then load the plugin with the following command:
bin/cake plugin load CakeDto -b
The bootstrap is needed for the TwigView events to be set up. If you already included TwigView, you might not need the bootstrap.
See Docs for details.