Download the PHP package bit-mx/data-entities without Composer
On this page you can find all versions of the php package bit-mx/data-entities. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package data-entities
Data Entities
Execute stored procedures in Laravel from Sqlserver without all the boilerplate code.
Table of Contents
- Introduction
- Installation
- Setup
- Compatibility
- Getting Started
- Create a Data Entity
- Connection
- Execute the Data Entity
- Mutators
- Available mutators
- Custom mutators
- Accessors
- Available accessors
- Custom accessor
- Response useful methods
- data
- Data with a key
- Data with a key and a default value
- Add data value
- Merge data
- As object
- As collection
- success
- failed
- throw
- Boot
- Middlewares
- Plugins
- AlwaysThrowOnError
- HasCache
- Data Transfer objects
- Debugging
- Testing
- Mocking the Data Entity
- Assertions
- Using factories
- Response type
Introduction
Data Entities is a library that allows you to execute stored procedures in Sqlserver easily. It is a wrapper around the Laravel's DB Facade.
Installation
You can install the package via composer:
Setup
You need to publish the configuration file to set the connection name.
This command will create a new configuration file in the config
directory.
Compatibility
This package is compatible with Laravel 10.x and above.
Due laravel 11 requires php 8.2, this package is compatible with php 8.2 and above.
Getting Started
Create a Data Entity
To create a Data Entity, you need to extend the DataEntity class and implement the resolveStoreProcedure method with the name of the stored procedure you want to execute.
You can also override the defaultParameters method to set the default parameters for the stored procedure.
You can also use the parameters method to set the parameters for the stored procedure.
The ResponseType enum has two options: SINGLE and COLLECTION.
SINGLE is used when the stored procedure returns a single row, and COLLECTION is used when the stored procedure returns multiple rows.
You can use the artisan command to create a new Data Entity:
This command will create a new Data Entity in the app/DataEntities
directory.
Connection
You can set the connection name overriding the resolveDatabaseConnection method.
Execute the Data Entity
To execute the Data Entity, you need to call the execute method on the Data Entity instance.
The execute method returns a Response object that contains the data returned by the stored procedure.
Mutators
You can use the mutators method to transform the parameters before sending them to the Store Procedure.
This will transform the date parameter to a formatted date string before sending it to the stored procedure.
Available mutators
-
datetime: : Converts the value to a datetime string using the specified format. You can pass a format as an argument to the cast. Examples:
datetime
Returns Y-m-d H:i:sdatetime:Y-m-d
datetime:H:i:s
datetime:Y-m-d H:i:s
-
date: : Converts the value to a date
Y-m-d
-
bool: : Converts the value to a boolean in int. Example: If the value is true, it will be converted to 1, and if it is false, it will be converted to 0.
-
int: : Converts the value to an integer.
-
float: : Converts the value to a float. You can pass the number of decimals as an argument to the cast. Example:
float
Returns a float with 2 decimals.float:4
Returns a float with 4 decimals.float:0
Returns an integer.
-
string: : Converts the value to a string.
-
json: : Converts the value to a json string. : Example:
: - if you pass an array, it will be converted to a json string.
- [1, 2,4] will be converted to "[1,2,4]"
- ['name' => 'John'] will be converted to '{"name":"John"}'
- You can pass the JSON options as an argument to the cast.
'json:'. JSON_PRETTY_PRINT
will return the json string with the JSON_PRETTY_PRINT option.
Custom mutators
You can create custom mutators by implementing the Mutable interface.
You can create a new cast using the artisan command.
Accessors
You can use the accessors method to transform the data returned by the stored procedure.
This will transform the contact_id key to an integer before returning the data.
Available accessors
-
datetime: : Converts the value to a DateTime instance.
-
datetime_immutable: : Converts the value to a DateTimeImmutable instance.
-
bool: : Converts the value to a boolean Example: If the value is 1, it will be converted true
-
int: : Converts the value to an integer.
-
float: : Converts the value to a float
-
string: : Converts the value to a string.
-
array: : Converts the value from a json string to an array.
- object: : Converts the value from a json string to an object.
- collection: : Converts the value from a json string to a Laravel Collection.
Custom accessor
You can create custom accessors by implementing the Accessable interface.
You can create a new accessor using the artisan command.
Response useful methods
The Response object has some useful methods to work with the data returned by the stored procedure.
data
The data method returns the data returned by the stored procedure as an array.
Data with a key
You can get the data with a key
Data with a key and a default value
You can get the data with a key and a default value
Add data value
You can add a value to the data array
You can add as an array to the data array
Merge data
You can merge an array with the data array
As object
You can get the data as an object
As collection
You can get the data as a collection
success
The success method returns true if the stored procedure was executed successfully, and false otherwise.
failed
The fail method returns true if the stored procedure failed, and false otherwise.
throw
By default, the Response object won't throw an exception if the stored procedure fails. You can throw an exception manually using the throw method.
Boot
You can use the boot method to execute code before and after the stored procedure is executed.
Traits
You can use traits to add functionality to your Data Entities. Add the method bootTrait to the Data Entity to use the trait.
The bootTaggable method will be called before the stored procedure is executed.
Middlewares
You can use middlewares to execute code before and after the stored procedure is executed.
You can alse use a invokable class as a middleware. This class should implement the QueryMiddleware or ResponseMiddleware interface.
Plugins
You can use plugins to add functionality to your Data Entities.
AlwaysThrowOnError
The AlwaysThrowOnError plugin will throw an exception if the stored procedure fails.
HasCache
The HasCache plugin will cache the data returned by the stored procedure.
Data Entity shloud implement the Cacheable interface.
You can invalidate the cache using the invalidateCache method.
Or you can disable temporarily the cache using the disableCaching method.
Response object will have a isCached method to check if the data was cached.
Data Transfer objects
You can use Data Transfer objects to map the data returned by the stored procedure to a PHP object.
You can get the dto from the response using the dto method.
Debugging
You cal call dd and ddRaw methods to debug the query sent to the database.
Testing
You can create integration tests for your Data Entities easily.
Mocking the Data Entity
You can mock the Data Entity using the DataEntity::fake method.
When using the fake method, the execute method will return the data specified in the MockResponse::make method and won't execute the stored procedure.
Assertions
You can use the assert method to assert that the Data Entity was executed.
Assertions
You can use the following assertions:
- assertExecuted: Assert that the Data Entity was executed.
- assertNotExecuted: Assert that the Data Entity was not executed.
- assertExecutedCount: Assert that the Data Entity was executed a specific number of times.
- assertExecutedOnce: Assert that the Data Entity was executed once.
Using factories
You can use factories to create fake data for your Data Entities.
To create a factory you should extend the DataEntityFactory class and implement the definition method.
You can use the faker property to generate fake data.
You can pass directly the factory to the MockResponse::make method. or you can create an array with the create method.
You can also use the count method to create an array of fake data.
You can use the state method to change the default values of the factory.
Or create a new method in the factory to change the default values.
You can create a fake with an exception
Response type
You can set the response type using the responseType method.
You can change the response type on MockResponse
You can create a new factory using the artisan command.
This command will create a new factory in the tests/DataEntityFactories
directory.
All versions of data-entities with dependencies
illuminate/support Version ^10.0 || ^11.0
illuminate/database Version ^10.0 || ^11.0
fakerphp/faker Version ^1.23