Package Data | |
---|---|
Maintainer Username: | nWidart |
Maintainer Contact: | n.widart@gmail.com (Nicolas Widart) |
Package Create Date: | 2016-07-26 |
Package Last Update: | 2016-07-28 |
Language: | PHP |
License: | MIT |
Last Refreshed: | 2024-12-19 03:19:36 |
Package Statistics | |
---|---|
Total Downloads: | 55 |
Monthly Downloads: | 0 |
Daily Downloads: | 0 |
Total Stars: | 12 |
Total Watchers: | 3 |
Total Forks: | 2 |
Total Open Issues: | 0 |
This package helps you normalize your data in order to save them into the database. The Goal is to having separate classes that handle the data normalization, and thus can be tested independently.
Via Composer
$ composer require nwidart/laravel-normalizer
Add the Nwidart\LaravelNormalizer\Traits\CanNormalizeData
trait on the model(s) you wish data to be normalized.
Your normalizers classes need to implement the Nwidart\LaravelNormalizer\Contracts\Normalizer
interface. This interface will add the normalize(array $data)
method.
Example:
use Nwidart\LaravelNormalizer\Contracts\Normalizer;
final class CustomNormalizer implements Normalizer
{
/**
* Normalize the given data
* @param array $data
* @return array
*/
public function normalize(array $data)
{
if (array_key_exists('name', $data)) {
$data['name'] = strtoupper($data['name']);
}
return $data;
}
}
This method needs to return the $data
array. In here you can change the received data as you please.
normalizers
class propertyOn that same model, add a protected $normalizers
property. This is where you list your normalizers, in an array.
Example:
use Illuminate\Database\Eloquent\Model;
use Nwidart\LaravelNormalizer\Traits\CanNormalizeData;
class Product extends Model
{
use CanNormalizeData;
protected $normalizers = [CustomNormalizer::class];
}
Now you can start normalizing your data. This can for instance be done in your repository class.
Example:
public function create($data)
{
$data = $this->model->normalize($data);
return $this->model->create($data);
}
Please see CHANGELOG for more information what has changed recently.
$ composer test
Please see CONTRIBUTING and CONDUCT for details.
If you discover any security related issues, please email n.widart@gmail.com instead of using the issue tracker.
The MIT License (MIT). Please see License File for more information.