Download the PHP package rogerthomas84/dtoinflator without Composer
On this page you can find all versions of the php package rogerthomas84/dtoinflator. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download rogerthomas84/dtoinflator
More information about rogerthomas84/dtoinflator
Files in rogerthomas84/dtoinflator
Package dtoinflator
Short Description DtoInflator is a library to inflate arrays or objects into their corresponding models.
License MIT
Homepage https://github.com/rogerthomas84/dtoinflator
Informations about the package dtoinflator
DTO Inflator
DtoInflator is a helpful library for converting arrays or generic objects into DTOs.
This was originally written for helping ease the management of responses from API services.
Usage
Create your model, extending DtoInflatorAbstract
If you require a sub model, add the property of protected $keyToClassMap
to your parent model mapping the key name
to the fully qualified class name. This tells the library to identify the key and that it needs to inflate a specific
model.
All models should extend DtoInflatorAbstract
.
If you have a property within a DTO called 'favourite' and it requires a sub model, you could map it with this:
If you require an array of models (for example someone can have various favourite items), you can simply
append []
onto the class name in the map.
So, if you have a property called 'favourites' and it's an array of child models, the key to class map should look like this:
Sometimes you might want to change the name of a property to something else, for example in the case of API
responses, you might want to change underscored keys with camelcase. To do this, simply expose the fieldToFieldMap
variable in your model. Where the key is the original name, and the value is the new key to use in the DTO.
Please note, the newly named key is only used in inflation.
You might also want to shorten keys every now and then. To do this, you can pass a second parameter into the inflate methods, defining where these keys need to be mapped.
Examples
There are example models in the tests/DtoInflatorTests/TestModels
directory.
Inflating
You can inflate a single record by calling inflateSingleArray
passing the array of data or alternatively,
if you've got an object (for example stdClass
) you can use inflateSingleObject
Likewise, you can inflate a multiple records by calling inflateMultipleArrays
passing the array of arrays,
or again if you're using objects like above, the inflateMultipleObjects
method.
Mapping fields
If a key passed into the inflate methods isn't found in the object, it gets added to the unmappedFields
array.
Likewise, if you try to request data from the object after inflating and the property isn't found, internally the
abstract method __get($name)
will check the unmappedFields
for a corresponding value.
Theoretically you don't have to declare variables. But this obviously isn't advised. It does however mean that
properties don't actually have to be public
, they could be protected
but never (ever) private
.
More advanced models
If you needed more advanced models, you could use something like this:
Running unit tests
./vendor/bin/phpunit -c phpunit.xml
All versions of dtoinflator with dependencies
ext-json Version *