Skip to content

Commit

Permalink
new feature added and tests updated
Browse files Browse the repository at this point in the history
  • Loading branch information
touhidurabir committed Nov 14, 2021
1 parent 207e575 commit 265c8b7
Show file tree
Hide file tree
Showing 10 changed files with 354 additions and 5 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,10 @@ All notable changes to this project will be documented in this file.
- Initial release

## [1.0.1] - 2021-10-29
- Bug fix.
- Bug fix.

## [1.1.0] - 2021-11-15
- Bug fix.
- Readme Update
- Tests update.
- Sanitization runtime disable/enable feature added.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ User::create([
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 (somemail@test.com, password, 2021-08-23 10:15:25, 2021-08-23 10:15:25))'
Illuminate\Database\QueryException: SQLSTATE[HY000]: General error: 1 table users has no column named data (SQL: insert into "users" ("email", "password", "data", "updated_at", "created_at") values (somemail@mail.com, password, data, 2021-11-14 20:11:04, 2021-11-14 20:11:04))
```

The **Sanitize** package target to make it easier to handle such case as follow by including the **Sanitizable** trait in the models
Expand Down Expand Up @@ -132,6 +132,13 @@ This will return back as such :

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.

It is also possible to **disable/enable** the sanitization process at the runtime using the static methods **disableSanitization** and **enableSanitization** . For example,

```php
User::disableSanitization(); // disable the sanitization process
User::enableSanitization(); // enable the sanitization process if previously disabled
```

## Contributing
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Expand Down
42 changes: 39 additions & 3 deletions src/Sanitizable.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,42 @@
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Touhidurabir\ModelSanitize\Builder\SanitizableQueryBuilder;

trait Sanitizable {

/**
* Shoud the sanitize be enabled
*
* @var bool
*/
protected static $sanitizationEnabled = true;


/**
* Disbale the model sanitation
*
* @return void
*/
public static function disableSanitization() {

static::$sanitizationEnabled = false;
}


/**
* Enable the model sanitation
*
* @return void
*/
public static function enableSanitization() {

static::$sanitizationEnabled = true;
}


/**
* Sanitize data list to model fillables
*
Expand Down Expand Up @@ -76,11 +107,16 @@ public static function gibberish(array $attributes = []) {
* Create a new Eloquent query builder for the model.
*
* @param \Illuminate\Database\Query\Builder $query
* @return \Touhidurabir\ModelSanitize\Builder\SanitizableQueryBuilder|static
* @return \Touhidurabir\ModelSanitize\Builder\SanitizableQueryBuilder|\Illuminate\Database\Eloquent\Builder|static
*/
public function newEloquentBuilder($query) {

return new SanitizableQueryBuilder($query);

if ( static::$sanitizationEnabled ) {

return new SanitizableQueryBuilder($query);
}

return new Builder($query);
}


Expand Down
32 changes: 32 additions & 0 deletions tests/App/Address.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Touhidurabir\ModelSanitize\Tests\App;

use Touhidurabir\ModelSanitize\Sanitizable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Address extends Model {

use SoftDeletes;

use Sanitizable;

/**
* The model associated table
*
* @var string
*/
protected $table = 'addresses';


/**
* The attributes that are not mass assignable.
*
* @var array
*/
protected $guarded = [
'extras',
];

}
33 changes: 33 additions & 0 deletions tests/App/Nation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Touhidurabir\ModelSanitize\Tests\App;

use Touhidurabir\ModelSanitize\Sanitizable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Nation extends Model {

use SoftDeletes;

use Sanitizable;

/**
* The model associated table
*
* @var string
*/
protected $table = 'nations';


/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name',
'code',
];

}
43 changes: 43 additions & 0 deletions tests/App/State.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Touhidurabir\ModelSanitize\Tests\App;

use Touhidurabir\ModelSanitize\Sanitizable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class State extends Model {

use SoftDeletes;

use Sanitizable;

/**
* The model associated table
*
* @var string
*/
protected $table = 'states';


/**
* The attributes that are not mass assignable.
*
* @var array
*/
protected $guarded = [
'city_counts',
];


/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name',
'code',
];

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;

class CreateAddressesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
DB::connection()->getSchemaBuilder()->create('addresses', function (Blueprint $table) {
$table->increments('id');
$table->string('address_line_1');
$table->string('address_line_2')->nullable();
$table->string('nation')->nullable();
$table->string('state')->nullable();
$table->string('city')->nullable();
$table->string('zip')->nullable();
$table->text('extras')->nullable();
$table->boolean('status')->default(true);
$table->timestamps();
$table->softDeletes();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
DB::connection()->getSchemaBuilder()->dropIfExists('addresses');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;

class CreateNationsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
DB::connection()->getSchemaBuilder()->create('nations', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('code');
$table->string('description')->nullable();
$table->boolean('status')->default(true);
$table->timestamps();
$table->softDeletes();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
DB::connection()->getSchemaBuilder()->dropIfExists('nations');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;

class CreateStatesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
DB::connection()->getSchemaBuilder()->create('states', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('code');
$table->string('description')->nullable();
$table->integer('city_counts')->nullable();
$table->boolean('status')->default(true);
$table->timestamps();
$table->softDeletes();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
DB::connection()->getSchemaBuilder()->dropIfExists('states');
}
}
Loading

0 comments on commit 265c8b7

Please sign in to comment.