Download the PHP package piggly/php-payload without Composer
On this page you can find all versions of the php package piggly/php-payload. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download piggly/php-payload
More information about piggly/php-payload
Files in piggly/php-payload
Package php-payload
Short Description A simple payload data manager.
License MIT
Homepage https://github.com/piggly-dev/php-payload
Informations about the package php-payload
Payloads
Payloads, how name says, are about data. That tradicional model with getters and setters. But, we don't need models anymore. Thanks to (Eloquent Model)[https://laravel.com/docs/8.x/eloquent] which solved that.
But, there are still Request and Response data, they are not any kind of storage data, they are just data. Adopt some kind of ORM Model to them... it's kind of overthinking, making these datas too complex.
This simples library cames to solve it. Payloads are tradicional arrays
with more flexibily. Same flexibility we can see at Eloquent Models, but not complicated or overpowerfull at all. It's just a way to standatize dataa across Requests and Responses.
See below how it works.
PayloadArray
The Person
object:
The Address
object
Usage
PayloadMap
The PayloadMap
class force to payload use a map. This mapping will strict define all fields allowed to payload and, even, fields validations. Each field inside a PayloadMap
will be a Field
object. The Field
object will have following properties:
Method | Description |
---|---|
getKey() |
Field key name. |
exportKeyAs() and getKeyToExport() |
Alias to field key name while exporting. (e.g. A field country may be exported as country_id ). |
value() and getValue() |
Field value. |
label() and getLabel() |
Field label. |
defaults() and getDefault() |
Field default value. |
required() , optional() and isRequired() |
If field is required or optional. |
acessible() , hidden() and isAcessible() |
If field is accessible or hidden. |
allowsNull() , notAllowsNull() and isAllowingNull() |
If NULL is a valid value for field and it should accept it for exporting. (e.g. if country does not allows NULL and country is NULL , then it won't be exported). |
validator() |
A Respect\Validation\Validator object to validate field value. |
custom() and getCustom() |
To manager custom properties. |
There also methods:
export()
will export field value;validate()
will return aboolean
which validates field value;assert()
will throw anInvalidDataException
if cannot validate field value;props()
set all props once;back()
to navigation purpose it will goes back toPayloadMap
object, sinceField
is inside one.
Creating a PayloadMap
works the same way as creating a PayloadArray
, but it's required the _map()
method, which will contains the mapping to fields:
You may use setter{key}()
and getter{key}()
methods to mutate field value before set and after get. Mutators, be them setters or getters, should always return the value mutated.
Changelog
See the CHANGELOG file for information about all code changes.
Testing the code
This library uses the PHPUnit. We carry out tests of all the main classes of this application.
Contributions
See the CONTRIBUTING file for information before submitting your contribution.
Credits
- Caique Araujo
- All contributors
Support the project
Piggly Studio is an agency located in Rio de Janeiro, Brazil. If you like this library and want to support this job, be free to donate any value to BTC wallet 3DNssbspq7dURaVQH6yBoYwW3PhsNs8dnK
❤.
License
MIT License (MIT). See LICENSE.