Download the PHP package bfg/dto without Composer
On this page you can find all versions of the php package bfg/dto. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package dto
Dto
The data transfer object pattern
Installation
after install your can create a new dto by use this command
Usage
- Introduction
- First steps
- Constructors
- fromEmpty
- fromArray
- fromAnything
- fromGet
- fromPost
- fromHttp
- fromRequest
- fromJson
- fromSerialize
- fromModel
- fromStaticCache
- fromCollection
- fromCache
- Nested DTO calls
- Binding
- Binding to the model
- Binding to the Carbon
- Property extends
- Property casting
- Property hidden
- Property rules
- Property encrypted
- Property mutations
- Computed properties
- Lazy properties
- Method property access
- Collection hinting
- Property by method with
- Property logsEnabled
- Meta
- Attributes
- DtoName
- DtoFromConfig
- DtoFromRequest
- DtoFromRoute
- DtoFromCache
- DtoToResource
- Events
- creating
- created
- updating
- updated
- mutating
- mutated
- serialize
- unserialize
- clone
- fromModel
- fromEmpty
- fromArray
- fromRequest
- fromJson
- fromSerialize
- prepareModel
- prepareSerialize
- prepareJson
- prepareRequest
- prepareArray
- Reflection
- explain
- vars
- getModifiedFields
- getRelationNames
- getPropertyNames
- getNames
- getReflection
- Convert DTO to
- ToArray
- ToJson
- ToResponse
- ToCollection
- ToBase64
- ToModel
- ToSerialize
- ToString
- DTO Collection
- insertToDatabase
- insertToModel
- Commands
- make:dto-cast
- make:enum
- make:dto-docs
- Helpers
- Customize http request
- Default Laravel Support
Introduction
Variety of constructs for creating DTOs
The package provides a variety of methods for creating DTOs (Data Transfer Objects), which significantly improves flexibility. Methods such as fromArray, fromModel, fromRequest, fromJson, fromSerialize and others allow you to conveniently create DTOs from different data sources.
Support for nested DTOs
The package supports nested DTOs with typing, which makes it easy to work with complex data, such as addresses or comments in the user example. This simplifies data processing in cases with dependencies between objects.
Rich customization of properties
Support for data casting, such as datetime, bool, as well as property extension through methods and attributes, such as DtoName, DtoFromConfig, DtoFromRequest are useful and powerful tools that make DTOs even more versatile.
Diving into events
The ability to handle various events (creating, created, mutating and others) provides greater flexibility in managing the state of the DTO. This can be useful for implementing validation logic or transforming data before or after it is used.
DTO Collections Support
Including support for DTO collections with methods for saving them to the database or models is a great addition that makes it easier to work with multiple objects.
Additional Features
Convenient helpers for working with DTOs, such as validate, restore, cache, as well as methods for working with metadata and logs, make this package a great tool for organizing the data structure in large projects.
Ease of Use
The ease of creating DTOs via the php artisan make:dto command and obvious typing of properties make the package developer-friendly, ensuring both clean code and good integration with the Laravel framework.
Overall, this package looks like a powerful tool for structuring data in Laravel, suitable for use in projects with large amounts of data and complex models. Everything looks logical and flexible, which allows you to quickly adapt it to various needs.
First steps
Before you can use the DTO, you need to create a new class that extends the Bfg\Dto\Dto
class.
All you dto properties must be public and have a type hint.
Also you must use the constructor for the DTO. And you can use the dependency injection in the constructor.
After creating the DTO, you can use it in your code.
Constructors
fromEmpty
You can create a new DTO with empty properties.
fromArray
You can create a new DTO from an array.
fromAnything
You can create a new DTO from anything.
fromGet
You can create a new DTO from the get request.
fromPost
You can create a new DTO from the post request.
fromHttp
You can create a new DTO from the http request. `
fromRequest
You can create a new DTO from the request.
fromJson
You can create a new DTO from json.
fromSerialize
You can create a new DTO from serialize.
fromModel
You can create a new DTO from model.
fromStaticCache
You can create a new DTO from static cache.
fromCollection
You can create a new DTO from collection.
fromCache
You can create a new DTO from cache.
fromSource
You can create a new DTO from source.
And after that you can create a new DTO from source.
Nested DTO calls
For nested DTO calls, you can use type hinting.
Now you can use nested DTOs.
Binding
You can use binding for the DTO.
Binding to the model
You can bind the DTO property to the model.
If model User with id 1
exists, it will be bound to the DTO property. If not, the property will be null.
Binding to the Carbon
You can bind the DTO property to the Carbon.
Default format for the Carbon is Y-m-d H:i:s
.
You can change the format using the $dateFormat
property.
Property extends
You can use property extends for extending the DTO.
This way you will expand the properties for DTO.
All properties have identical behavior to properties in the property promotion constructor.
Property casting
You can use property casting like in Laravel models. ` Also, this casting support class casting. You can create a new class casting using the artisan command:
After creating the class casting, you can use it in the DTO.
And casting support enum casting. You can create a new enum casting using the artisan command:
After creating the enum casting, you can use it in the DTO.
Property hidden
You can use property hidden like in Laravel models.
Property rules
You can use property rules like in Laravel requests.
Property encrypted
You can use property encrypted for getting encrypted data.
Property mutations
You can use property mutations like in Laravel models.
fromArrayName
method will be called when you create a new DTO data from an array.toArrayName
method will be called when you convert DTO data to an array.
This method gets the property value and must return the new value.
Computed properties
You can use computed properties.
Now you can use computed properties.
Lazy properties
You can use lazy like properties any property or computed property. If you add the prefix "lazy" before the name of a property or a computed property, you will get access to lazy execution. When you request a property, the value that this property receives is cached and all subsequent times the result for the lazy property is taken from the cache and if in DTO the property changes, then in Lazy it will remain the same since it is taken from the cache.
Method property access
You can use the method property access.
Method default for field
You can use the default
method for setting the default value for the field.
After that, you can escape the name field when creating a DTO.
Collection hinting
You can use collection hinting.
Property by method with
You can use the with...
method for adding data to the DTO array.
Property logsEnabled
You can use property logsEnabled for logging data in the DTO memory.
You can write your own logs to the DTO memory using the log
method.
Meta
You can use meta.
Attributes
You can use attributes.
DtoName
You can use the DtoName
attribute to add the name of the DTO.
Here we have added an additional field name to the "name" field and we can now use 2 fields to insert data.
For property extends:
Since we cannot write an attribute to a separate element of the array, we need to specify the name of the field to which we need to assign an additional name as the second parameter.
DtoFromConfig
You can use the DtoFromConfig
attribute to add the property value from the config.
DtoFromRequest
You can use the DtoFromRequest
attribute to add the property value from the request.
DtoFromRoute
You can use the DtoFromRoute
attribute to add the property value from the route.
DtoFromCache
You can use the DtoFromCache
attribute to add the property value from the cache.
DtoToResource
You can use the DtoToResource
attribute to convert the property for adding to the array.
Create a new resource:
Address will be converted to the array using the UserAddressResource
resource.
Events
You can use events.
creating
You can use the creating
event.
created
You can use the created
event.
updating
You can use the updating
event.
updated
You can use the updated
event.
mutating
You can use the mutating
event.
mutated
You can use the mutated
event.
serialize
You can use the serialize
event.
unserialize
You can use the unserialize
event.
clone
You can use the clone
event.
fromModel
You can use the fromModel
event.
fromEmpty
You can use the fromEmpty
event.
fromArray
You can use the fromArray
event.
fromRequest
You can use the fromRequest
event.
fromJson
You can use the fromJson
event.
fromSerialize
You can use the fromJson
event.
prepareModel
You can use the prepareModel
event.
prepareSerialize
You can use the prepareSerialize
event.
prepareJson
You can use the prepareJson
event.
prepareRequest
You can use the prepareRequest
event.
prepareArray
You can use the prepareArray
event.
prepareEmpty
You can use the prepareArray
event.
destruct
You can use the destruct
event.
Reflection
You can use reflection.
explain
You can use the explain
method for getting the DTO information.
vars
You can use the vars
method for getting the DTO properties.
getModifiedFields
You can use the getModifiedFields
method for getting the modified fields.
getRelationNames
You can use the getRelationNames
method for getting the relation names.
getPropertyNames
You can use the getPropertyNames
method for getting the property names.
getNames
You can use the getNames
method for getting the names.
getReflection
You can use the getReflection
method for getting the reflection.
Convert DTO to
ToArray
You can convert DTO to array.
ToJson
You can convert DTO to json.
ToResponse
You can convert DTO to response.
ToCollection
You can convert DTO to collection.
ToBase64
You can convert DTO to base64.
ToModel
You can convert DTO to model.
ToSerialize
You can convert DTO to serialize.
ToApi
You can convert DTO to api.
ToString
You can convert DTO to string.
DTO Collection
Dto collection have couple additional methods.
insertToDatabase
For save collection to database you can use saveToDatabase
method.
insertToModel
For save collection to model you can use saveToModel
method.
Commands
You can use commands.
Make dto
You can create a new DTO using the artisan command.
Make dto cast
You can create a new DTO cast using the artisan command.
Make dto docs
You can build the DTO documentation for extended fields using the artisan command.
Add this command to the
composer.json
file for auto-generating DTO documentation after thecomposer update
andcomposer dump-autoload
command.
Helpers
You can use helpers.
new
You can use the new
helper for creating a new DTO.
version
You can use the version
helper for getting the DTO version.
pipeline
You can use the pipeline
helper for creating a new DTO pipeline.
camelKeys
You can use the camelKeys
helper for converting the DTO keys to camel case when converting to an array.
snakeKeys
You can use the snakeKeys
helper for converting the DTO keys to snake case when converting to an array.
cache
You can use the cache
helper for caching the DTO.
cacheKey
You can use the cacheKey
helper for getting the cache key.
getCachedKey
You can use the getCachedKey
helper for getting the cached key.
cacheKeyClear
You can use the cacheKeyClear
helper for clearing the cache key.
validate
You can use the validate
helper for validating the DTO.
restore
You can use the restore
helper for restoring the DTO from the original data.
originals
You can use the originals
helper for getting the original data.
equals
You can use the equals
helper for comparing the DTOs.
fill
You can use the fill
helper for filling the DTO.
length
You can use the length
helper for getting the length of the DTO in bytes.
count
You can use the count
helper for getting the count of the DTO.
has
You can use the has
helper for checking the property in the DTO.
You also can use isset
for checking the property in the DTO.
dataHash
You can use the dataHash
helper for getting the data hash of the DTO.
hash
You can use the hash
helper for getting the hash of the DTO.
clone
You can use the clone
helper for cloning the DTO.
str
You can use the str
helper for getting the string representation of the DTO.
collect
You can use the collect
helper for wrapping the DTO properties in the collection.
boolable
You can use the boolable
helper for inverting the boolean property in the DTO.
toggleBool
You can use the toggleBool
helper for toggling the boolean property in the DTO.
increment
You can use the increment
helper for incrementing the property in the DTO.
decrement
You can use the decrement
helper for decrementing the property in the DTO.
set
You can use the set
helper for setting the property in the DTO.
get
You can use the get
helper for getting the property in the DTO.
map
You can use the map
helper for mapping the DTO properties.
isEmpty
You can use the isEmpty
helper for checking the DTO is property empty.
isNotEmpty
You can use the isNotEmpty
helper for checking the DTO is property not empty.
isNull
You can use the isNull
helper for checking the DTO is property null.
isNotNull
You can use the isNotNull
helper for checking the DTO is property not null.
isCanNull
You can use the isCanNull
helper for checking the DTO is property can be null.
isTrue
You can use the isTrue
helper for checking the DTO is property true.
isFalse
You can use the isFalse
helper for checking the DTO is property false.
isBool
You can use the isBool
helper for checking the DTO is property bool.
isEquals
You can use the isEquals
helper for checking the DTO is property equals.
isNotEquals
You can use the isNotEquals
helper for checking the DTO is property not equals.
isInstanceOf
You can use the isInstanceOf
helper for checking the DTO is property instance of.
isNotInstanceOf
You can use the isNotInstanceOf
helper for checking the DTO is property not instance of.
isString
You can use the isString
helper for checking the DTO is property string.
isNotString
You can use the isNotString
helper for checking the DTO is property not string.
isInt
You can use the isInt
helper for checking the DTO is property int.
isNotInt
You can use the isNotInt
helper for checking the DTO is property not int.
isFloat
You can use the isFloat
helper for checking the DTO is property float.
isNotFloat
You can use the isNotFloat
helper for checking the DTO is property not float.
isArray
You can use the isArray
helper for checking the DTO is property array.
isNotArray
You can use the isNotArray
helper for checking the DTO is property not array.
isObject
You can use the isObject
helper for checking the DTO is property object.
isNotObject
You can use the isNotObject
helper for checking the DTO is property not object.
isInstanceOfArray
You can use the isInstanceOfArray
helper for checking the DTO is property instance of array.
isNotInstanceOfArray
You can use the isNotInstanceOfArray
helper for checking the DTO is property not instance of array.
Customize http request
You can customize the http request.
Or you can customize only headers:
Also you can customize what fromAnything
can be used for the request POST by default
And you can customize the parameters data for the request by default
Default Laravel Support
DTO class use a famous Laravel support, such as Illuminate\Support\Traits\Conditionable
, Illuminate\Support\Traits\Dumpable
,
Illuminate\Support\Traits\Macroable
and Illuminate\Support\Traits\Tappable
.
Changelog
Please see CHANGELOG for more information on what has changed recently.
Security
Please see SECURITY for more information about security.
Credits
- Xsaven
License
The MIT License (MIT). Please see License File for more information.