-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Md. Touhidur Rahman
authored and
Md. Touhidur Rahman
committed
Aug 23, 2021
0 parents
commit 4116da8
Showing
17 changed files
with
845 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
.DS_Store | ||
/vendor | ||
/build | ||
composer.lock | ||
.phpunit.result.cache |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
language: php | ||
|
||
php: | ||
- 7.3 | ||
- 7.4 | ||
- 8.0 | ||
|
||
env: | ||
matrix: | ||
- COMPOSER_FLAGS="--prefer-lowest" | ||
- COMPOSER_FLAGS="" | ||
|
||
before_script: | ||
- travis_retry composer update ${COMPOSER_FLAGS} | ||
|
||
script: | ||
- vendor/bin/phpunit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# Changelog | ||
All notable changes to this project will be documented in this file. | ||
|
||
## [Unreleased] | ||
|
||
|
||
## [1.0.0] - 2021-08-14 | ||
- Initial release |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) Touhidur Rahman Abir | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
# Laravel Model UUID | ||
|
||
A simple package to sanitize model data to create/update table records. | ||
|
||
## Installation | ||
|
||
Require the package using composer: | ||
|
||
```bash | ||
composer require touhidurabir/laravel-model-sanitize | ||
``` | ||
|
||
## What is does ? | ||
The **Sanitize** package sanitize the passed **attributes** to proper model fillables at create or update. | ||
|
||
A model has multiple table schema based attributed associated with it. When we try to create a new model record or update an existing model record, we must provide the an array attributes that is propelry mapped to those arrtibute or table columns names . For example | ||
|
||
```php | ||
$user = User::create([ | ||
'email' => '[email protected]', | ||
'password' => Hash::make('password') | ||
]); | ||
``` | ||
|
||
The above code will run without any issue as both the **email** and **password** column presents in the users table . But for the following code | ||
|
||
```php | ||
User::create([ | ||
'email' => '[email protected]', | ||
'password' => 'password', | ||
'data' => 'some data' | ||
]); | ||
``` | ||
|
||
It will throw an **\Illuminate\Database\QueryException** if the **data** column not present in the users table. | ||
|
||
```bash | ||
Illuminate\Database\QueryException with message 'SQLSTATE[HY000] [2002] Connection refused (SQL: insert into `users` (`email`, `password`, `updated_at`, `created_at`) values ([email protected], password, 2021-08-23 10:15:25, 2021-08-23 10:15:25))' | ||
``` | ||
|
||
The **Sanitize** package target to make it easier to handle such case as follow by including the **Sanitizable** trait in the models | ||
|
||
```php | ||
$data = [ | ||
'email' => '[email protected]', | ||
'password' => 'password', | ||
'data' => 'some data' | ||
]; | ||
|
||
User::create($data); | ||
``` | ||
The above code will work if the **Sanitizable** trait is used in the **User** model class. it will sanitize the passed attributed to model fillables and table columns, thus removing the extra or non useable attributes from it . | ||
|
||
## How it will be helpful ? | ||
|
||
A great use case of this package is where one need to create multiple model instances from validated request data . For example | ||
|
||
```php | ||
$validated = $request->validated(); | ||
|
||
$user = User::create($validated); | ||
|
||
$profile = $user->profile->create($validated); | ||
``` | ||
I personally use this appraoch in many of my laravel apps . | ||
|
||
## Usage | ||
|
||
Use the trait **Sanitizable** in model where uuid needed to attach | ||
|
||
```php | ||
use Touhidurabir\ModelSanitize\Sanitizable; | ||
use Illuminate\Database\Eloquent\Model; | ||
|
||
class User extends Model { | ||
|
||
use Sanitizable; | ||
} | ||
``` | ||
|
||
And thats all . it will automatically work for all the following methods | ||
- **updateOrCreate** | ||
- **firstOrCreate** | ||
- **firstOrNew** | ||
- **create** | ||
- **forceCreate** | ||
- **update** | ||
|
||
This package also includes some helper methods that can be used to handle the sanitization process manually. | ||
|
||
The **sanitize** static method will sanitize the given attributes list and retuen back the useable and valid attributes as an array | ||
|
||
```php | ||
$data = [ | ||
'email' => '[email protected]', | ||
'password' => 'password', | ||
'data' => 'some data', | ||
'name' => 'Test User' | ||
]; | ||
|
||
User::sanitize($data); | ||
``` | ||
|
||
This will return back as such : | ||
```php | ||
[ | ||
'email' => '[email protected]', | ||
'password' => 'password', | ||
'name' => 'Test User' | ||
] | ||
``` | ||
|
||
The **gibberish** static method will sanitize the given attributes list and retuen back the gibberish/non userbale attributes as an array | ||
|
||
```php | ||
$data = [ | ||
'email' => '[email protected]', | ||
'password' => 'password', | ||
'data' => 'some data', | ||
'name' => 'Test User' | ||
]; | ||
|
||
User::gibberish($data); | ||
``` | ||
|
||
This will return back as such : | ||
```php | ||
[ | ||
'data' => 'some data', | ||
] | ||
``` | ||
|
||
The **sanitize** and **gibberish** methods can be used to check or manually sanitize and evaluate the in valid data that can be passed to create/update model records. | ||
|
||
## Contributing | ||
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change. | ||
|
||
Please make sure to update tests as appropriate. | ||
|
||
## License | ||
[MIT](./LICENSE.md) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
{ | ||
"name": "touhidurabir/laravel-model-sanitize", | ||
"description": "A laravel package to handle sanitize process of model data to create/update model records.", | ||
"type": "library", | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "Touhidur Rahman", | ||
"email": "[email protected]" | ||
} | ||
], | ||
"require": { | ||
"php": ">=7.2.0" | ||
}, | ||
"autoload" : { | ||
"psr-4" : { | ||
"Touhidurabir\\ModelSanitize\\": "src/" | ||
} | ||
}, | ||
"autoload-dev" : { | ||
"psr-4" : { | ||
"Touhidurabir\\ModelSanitize\\Tests\\": "tests/" | ||
} | ||
}, | ||
"require-dev": { | ||
"phpunit/phpunit": "^9.5", | ||
"orchestra/testbench": "^6.20", | ||
"illuminate/support": "^8.54", | ||
"illuminate/container": "^8.54", | ||
"illuminate/database": "^8.54", | ||
"illuminate/events": "^8.54" | ||
}, | ||
"extra": { | ||
"laravel": { | ||
"providers": [ | ||
"Touhidurabir\\ModelSanitize\\ModelSanitizeServiceProvider" | ||
], | ||
"aliases": { | ||
"ModelUuid": "Touhidurabir\\ModelSanitize\\Facades\\ModelSanitize" | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<phpunit bootstrap="vendor/autoload.php" | ||
backupGlobals="false" | ||
backupStaticAttributes="false" | ||
colors="true" | ||
verbose="true" | ||
convertErrorsToExceptions="true" | ||
convertNoticesToExceptions="true" | ||
convertWarningsToExceptions="true" | ||
processIsolation="false" | ||
stopOnFailure="false"> | ||
<testsuites> | ||
<testsuite name="Laravel Model Sanitize TestSuite"> | ||
<directory>tests</directory> | ||
</testsuite> | ||
</testsuites> | ||
<coverage processUncoveredFiles="true"> | ||
<include> | ||
<directory suffix=".php">src/</directory> | ||
</include> | ||
</coverage> | ||
<php> | ||
<env name="APP_ENV" value="testing"/> | ||
<env name="DB_CONNECTION" value="sqlite"/> | ||
<env name="DB_DATABASE" value=":memory:"/> | ||
<env name="CACHE_DRIVER" value="array"/> | ||
<env name="SESSION_DRIVER" value="array"/> | ||
<env name="QUEUE_DRIVER" value="sync"/> | ||
</php> | ||
<logging> | ||
<log type="tap" target="build/report.tap"/> | ||
<log type="junit" target="build/report.junit.xml"/> | ||
<log type="coverage-text" target="build/coverage.txt"/> | ||
<log type="coverage-clover" target="build/logs/clover.xml"/> | ||
</logging> | ||
</phpunit> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<?php | ||
|
||
namespace Touhidurabir\ModelSanitize\Builder; | ||
|
||
use Illuminate\Database\Eloquent\Builder as BaseBuilder; | ||
|
||
class SanitizableQueryBuilder extends BaseBuilder { | ||
|
||
/** | ||
* Create or update a record matching the attributes, and fill it with values. | ||
* | ||
* @param array $attributes | ||
* @param array $values | ||
* @return \Illuminate\Database\Eloquent\Model|static | ||
*/ | ||
public function updateOrCreate(array $attributes, array $values = []) | ||
{ | ||
return parent::updateOrCreate($attributes, $this->model->sanitizeToModelFillable($values)); | ||
} | ||
|
||
|
||
/** | ||
* Get the first record matching the attributes or create it. | ||
* | ||
* @param array $attributes | ||
* @param array $values | ||
* @return \Illuminate\Database\Eloquent\Model|static | ||
*/ | ||
public function firstOrCreate(array $attributes = [], array $values = []) | ||
{ | ||
return parent::firstOrCreate($attributes, $this->model->sanitizeToModelFillable($values)); | ||
} | ||
|
||
|
||
/** | ||
* Get the first record matching the attributes or instantiate it. | ||
* | ||
* @param array $attributes | ||
* @param array $values | ||
* @return \Illuminate\Database\Eloquent\Model|static | ||
*/ | ||
public function firstOrNew(array $attributes = [], array $values = []) | ||
{ | ||
return parent::firstOrNew($attributes, $this->model->sanitizeToModelFillable($values)); | ||
} | ||
|
||
|
||
/** | ||
* Save a new model and return the instance. | ||
* | ||
* @param array $attributes | ||
* @return \Illuminate\Database\Eloquent\Model|$this | ||
*/ | ||
public function create(array $attributes = []) | ||
{ | ||
return parent::create($this->model->sanitizeToModelFillable($attributes)); | ||
} | ||
|
||
|
||
/** | ||
* Save a new model and return the instance. Allow mass-assignment. | ||
* | ||
* @param array $attributes | ||
* @return \Illuminate\Database\Eloquent\Model|$this | ||
*/ | ||
public function forceCreate(array $attributes) | ||
{ | ||
return parent::forceCreate($this->model->sanitizeToModelFillable($attributes)); | ||
} | ||
|
||
|
||
/** | ||
* Update records in the database. | ||
* | ||
* @param array $values | ||
* @return int | ||
*/ | ||
public function update(array $values) | ||
{ | ||
return parent::update($this->model->sanitizeToModelFillable($values)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
namespace Touhidurabir\ModelSanitize\Facades; | ||
|
||
use Illuminate\Support\Facades\Facade; | ||
|
||
class ModelSanitize extends Facade { | ||
|
||
/** | ||
* Get the registered name of the component. | ||
* | ||
* @return string | ||
*/ | ||
protected static function getFacadeAccessor() { | ||
|
||
return 'model-sanitize'; | ||
} | ||
} |
Oops, something went wrong.