Req #81203 [Opn->Wfx]: [ FEATURE ] StructType like interface for arrays
| From: | requinix@php.net | Date: | Sun, 27 Jun 2021 20:57:41 +0000 |
| Subject: | Req #81203 [Opn->Wfx]: [ FEATURE ] StructType like interface for arrays | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-234651@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=81203&edit=1
ID: 81203
Updated by: requinix@php.net
Reported by: 6562680 at gmail dot com
Summary: [ FEATURE ] StructType like interface for arrays
-Status: Open
+Status: Wont fix
Type: Feature/Change Request
Package: *General Issues
Operating System: Win10
PHP Version: Irrelevant
Block user comment: N
Private report: N
New Comment:
I can't tell what this "struct" thing is supposed to be, but the bulk of what you
wrote (as far as I could understand it) pertains to validation. Data validation is not a job that
should be performed by the PHP language itself, and there are plenty of frameworks and libraries and
design patterns that can handle the job.
Previous Comments:
------------------------------------------------------------------------
[2021-06-27 14:08:48] 6562680 at gmail dot com
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.
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=81203&edit=1