Skip to content

Commit

Permalink
internal casting implementation use Collections for settings
Browse files Browse the repository at this point in the history
  • Loading branch information
yarobadtrak committed Feb 26, 2017
1 parent 371585f commit 9635256
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 26 deletions.
37 changes: 23 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ Add settings feature to Eloquent models in Laravel 5.

## Background

This has been developed to simplify adding "settings" feature to any eloquent model on your laravel project. Settings WILL be stored in `json` format, which has it's pros and cons!
This has been developed to simplify adding "settings" feature to any eloquent model on your laravel project.

Settings WILL be stored in `json` format/object! Is `json` the best way? Well it's your call! pros: super flexible, you can add/remove settings without code alteration (apart from `app/config/model-settings.php` see below). cons: expensive when querying/searching for certain `key => value` in `settings` per model.

`settings` will be casted into Laravel `Collection` for maximum functionality and usage.

## Installation
To install the package via Composer:
Expand All @@ -29,7 +33,7 @@ php artisan vendor:publish --provider="Yarob\LaravelModelSettings\ServiceProvide
## Updating your Eloquent Models

Your models should use the `hasSettings` trait.
You must also add `settings` to your `fillable` array, and then cast `settings` to `json` as shown in the example below
You must also add `settings` to your `fillable` array as shown in the example below

```php
use Yarob\LaravelModelSettings\HasSettings;
Expand All @@ -46,21 +50,15 @@ class User extends Model
protected $fillable = [
'name', 'email', 'settings'
];


/**
* The attributes that should be casted.
*
* @var array
*/
protected $casts = [
'settings' =>'json',
];

}
```

Your model and database must have column named `settings` in the database to store the settings values. You can add this manually via a migration on the intended model. The column should be text and big enough to store all settings after json encoded.
## Migration
Your model MUST have column named `settings` in the database to store the settings values.

You can add this manually via a migration on the intended model ```php $table->json('settings')->nullable(); ```.
The column should be big enough to accommodate all settings after json encoded.

## Usage

Expand All @@ -74,7 +72,7 @@ $user->settings()->save(array(
'phone_number' => '0123456789'
));

dd($user->settings);
print_r($user->settings);
```
## Configuration

Expand All @@ -95,7 +93,18 @@ return [
];
```

Pay attention that Model name in `model-settings.php` is case sensitive! so if you have a `foo` Model, then

```php
return [
'foo' => [
'key1',
'key2',
...
],
...
];
```

## Copyright and License

Expand Down
26 changes: 23 additions & 3 deletions src/HasSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Yarob\LaravelModelSettings;

use ErrorException;
use Yarob\LaravelModelSettings\Services\ModelSettingsService;

/**
Expand Down Expand Up @@ -44,13 +45,32 @@ public static function settingsSaved($callback)
}

/**
* Return the sluggable configuration array for this model.
* Must be implemented at the model class
*
* @return array
* @return \Yarob\LaravelModelSettings\Services\ModelSettingsService
*/
public function settings()
{
return new ModelSettingsService($this);
}

/**
* Define `settings` Accessor for Models
*
* @param $settings
* @return collection
* @throws ErrorException
*/
public function getSettingsAttribute($settings)
{
$jsonDecodedSettings = json_decode($settings);

if(json_last_error() == JSON_ERROR_NONE)
{
return collect( $jsonDecodedSettings );
}
else
{
throw new ErrorException('None json format encountered in `settings` column on `'.$this->getTable().'` table on database!');
}
}
}
23 changes: 14 additions & 9 deletions src/Services/ModelSettingsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,34 +33,39 @@ public function __construct(Model $model=null)
/**
* Save settings for current model.
*
* @param null $settings
* @return bool
* @param array $rawSettings
* @return bool/collection
* @throws ErrorException
* @internal param Model $model
*/
public function save($settings=null)
public function save($rawSettings=array())
{
if(!empty($settings) and !empty($this->model))
if(!empty($this->model) and is_array($rawSettings) and !empty($rawSettings))
{
if (!Schema::hasColumn($this->model->getTable(), 'settings')) {
throw new ErrorException('"settings" column seems to be missing on "'.class_basename($this->model).'" table on database!');
throw new ErrorException('`settings` column seems to be missing on `'.$this->model->getTable().'` table on database!');
}
else
{
$allowedSettingsKeys = $this->getConfiguration();
$oldSettings = is_array($this->model->settings)?$this->model->settings:array();

$settings = array_merge(
$oldSettings = $this->model->settings->toArray();

$newSettings = array_merge(
$oldSettings,
array_only(
$settings,
$rawSettings,
$allowedSettingsKeys
)
);

return $this->model->update(compact('settings'));
if($this->model->update( array( 'settings' => json_encode($newSettings) ) ))
{
return collect($newSettings);
}
}
}
return false;
}

/**
Expand Down

0 comments on commit 9635256

Please sign in to comment.