|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2021-06-27 20:57 UTC] requinix@php.net
-Status: Open
+Status: Wont fix
[2021-06-27 20:57 UTC] requinix@php.net
[2021-06-27 22:18 UTC] 6562680 at gmail dot com
[2021-06-27 22:40 UTC] rtrtrtrtrt at dfdfdfdf dot dfd
[2021-06-28 00:23 UTC] 6562680 at gmail dot com
[2021-06-28 05:50 UTC] rtrtrtrtrt at dfdfdfdf dot dfd
[2021-06-28 08:52 UTC] 6562680 at gmail dot com
[2021-06-28 12:03 UTC] requinix@php.net
-Block user comment: No
+Block user comment: Yes
[2021-06-28 12:03 UTC] requinix@php.net
|
|||||||||||||||||||||||||||
Copyright © 2001-2026 The PHP GroupAll rights reserved. |
Last updated: Wed Oct 07 07:00:01 2026 UTC |
Description: ------------ Every project every day we implement Serializer to convert incoming JSON data into nested object tree with validate the data. Main idea is about spliting your project into modules. I give an example: You have two modules - Auth and Company Auth module needs something like User or Identity or User interface But the Users by the task is depend from... maybe Agency or business network. So you need User class inside Company module Also you need User class inside Auth module (or some of its representation) Once you extend Company's User you create Hard link that should be resolved with maybe Interfaces. But your Auth User class still not implement Interface from Company class, so even if you create interface - you still need to create class that extend or realise original one. Everywhere that problem is solved using maybe Doctrine - there is a Entities (with damn annotations, relations and so on, required only for DAL layer) - and now you project is working only if you have DAL layer with all Entities. You cannot build small module with ValueObjects + Interfaces - because DAL layer needs then you interfaces implemented on Doctrine entities. But Doctine entities forbid to set `id` attribute and also have additional Event handlers like prePersist/postPersist, constructors, cloners and stuff that is required ONLY in DAL layer. So every application becomes a monolith, linked, related and coupled. Somebody decides to use Microservices then. They write own Message Bus... same like base PHP stack trace. Somebody decides to implement EventDispatcher and then everyday tries to build event call tree to control what exactly was first, second... etc. Main problem here is YOU CANNOT JUST VALIDATE objects or arrays in 2-3 lines of code. Serializer solves it like walk recursive and json and then maps incoming array into your entities/requests/valueObjects, then we need annotations to validate, and then - if some task expects little different data from global one - you need to implement almost layer to do that. How about implement global type (like interface) named Struct (YourObjectStruct) and create validator that can: 1. validate array 2. return errors in nested array 3. return array or map array to structs like serializer does ``` class UserStruct { public $prop1; public $prop2; public $prop3; public static authorize() : array { return []; } public static required() : array { return []; } public static rules() : array { return []; } } ``` ``` class UserRequestStruct extends UserStruct { public static authorize() : array { return []; } } ``` that structs could be controlled with next service: ``` interface StructerInterface { // returns bool public function fails(value, rules, messages, labels) : ?ValidationInterface; // returns validation if failed public function verify(value, rules, messages, labels) : ?ValidationInterface; // returns validation if succeeded // throws exception on fail public function validate(value, rules, messages, labels) : ValidationInterface; // returns errors array (you can pass existing validation to extract errors from) public function errors(valueOrValidation, rules, messages, labels) : ?ValidationInterface; // returns tree of struct object // throws exception on validation failed public function unserialize(value, rules, messages, labels) : object; } ``` Main thing of the concept - create local interfaces to check incoming object with properties. You can pass your object without any interfaces, object would be converted to array, validated and then used like "match our local interface" It works on frontend for example typescript. There isnt "data interface", ts just inspects on IDE (that is dangerous, but allows to use StdObject without creating Domain class, you just sure you pass that data like interface So you can use your arrays or convert it into object tree, and you need interfaces only for services instead everywhere everysecond. Thank you.