Skip to content

Commit

Permalink
Laravel 6 support (#23)
Browse files Browse the repository at this point in the history
* Laravel 6 support
  • Loading branch information
glorand authored Sep 3, 2019
1 parent d9eb194 commit 0a106d7
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 64 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

All notable changes to `glorand/laravel-model-settings` will be documented in this file

## 3.3.1 - 2019-09-04
### Added
- Laravel 6 support

## 3.3.0 - 2019-08-29
### Added
- Default settings on Model
Expand Down
23 changes: 14 additions & 9 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,20 @@
],
"require": {
"php": ">=7.1.3",
"illuminate/config": "~5.5.0|~5.6.0|~5.7.0|~5.8.0",
"illuminate/database": "~5.5.0|~5.6.0|~5.7.0|~5.8.0",
"illuminate/support": "~5.5.0|~5.6.0|~5.7.0|~5.8.0",
"illuminate/console": "~5.5.0|~5.6.0|~5.7.0|~5.8.0",
"illuminate/filesystem": "~5.5.0|~5.6.0|~5.7.0|~5.8.0"
"illuminate/config": "~5.5.0|~5.6.0|~5.7.0|~5.8.0|^6.0",
"illuminate/database": "~5.5.0|~5.6.0|~5.7.0|~5.8.0|^6.0",
"illuminate/support": "~5.5.0|~5.6.0|~5.7.0|~5.8.0|^6.0",
"illuminate/console": "~5.5.0|~5.6.0|~5.7.0|~5.8.0|^6.0",
"illuminate/filesystem": "~5.5.0|~5.6.0|~5.7.0|~5.8.0|^6.0"
},
"require-dev": {
"phpunit/phpunit": "^7.5",
"orchestra/testbench": "~3.8.0",
"friendsofphp/php-cs-fixer": "^2.16@dev"
"phpunit/phpunit": "^7.5|^8.0",
"orchestra/testbench": "~3.8.0|^4.0",
"friendsofphp/php-cs-fixer": "^2.16@dev",
"predis/predis": "^1.1"
},
"suggest": {
"predis/predis": "Required to use settings with Redis"
},
"autoload": {
"psr-4": {
Expand All @@ -51,5 +55,6 @@
]
}
},
"minimum-stability": "dev"
"minimum-stability": "dev",
"prefer-stable": true
}
23 changes: 20 additions & 3 deletions src/Managers/AbstractSettingsManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Glorand\Model\Settings\Exceptions\ModelSettingsException;
use Glorand\Model\Settings\Traits\HasSettings;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Arr;

/**
* Class AbstractSettingsManager
Expand Down Expand Up @@ -46,7 +47,7 @@ public function all(): array
*/
public function has(string $path): bool
{
return array_has($this->all(), $path);
return Arr::has($this->all(), $path);
}

/**
Expand All @@ -56,7 +57,7 @@ public function has(string $path): bool
*/
public function get(string $path = null, $default = null)
{
return $path ? array_get($this->all(), $path, $default) : $this->all();
return $path ? Arr::get($this->all(), $path, $default) : $this->all();
}

/**
Expand Down Expand Up @@ -100,9 +101,25 @@ public function setMultiple(iterable $values): SettingsManagerContract
{
$settings = $this->all();
foreach ($values as $path => $value) {
array_set($settings, $path, $value);
Arr::set($settings, $path, $value);
}

return $this->apply($settings);
}

/**
* @param iterable $paths
* @return \Glorand\Model\Settings\Contracts\SettingsManagerContract
*/
public function deleteMultiple(iterable $paths): SettingsManagerContract
{
$settings = $this->all();
foreach ($paths as $path) {
Arr::forget($settings, $path);
}

$this->apply($settings);

return $this;
}
}
21 changes: 3 additions & 18 deletions src/Managers/FieldSettingsManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Glorand\Model\Settings\Managers;

use Glorand\Model\Settings\Contracts\SettingsManagerContract;
use Illuminate\Support\Arr;

/**
* Class FieldSettingsManager
Expand Down Expand Up @@ -35,23 +36,7 @@ public function delete(string $path = null): SettingsManagerContract
$settings = [];
} else {
$settings = $this->all();
array_forget($settings, $path);
}

$this->apply($settings);

return $this;
}

/**
* @param iterable $paths
* @return \Glorand\Model\Settings\Contracts\SettingsManagerContract
*/
public function deleteMultiple(iterable $paths): SettingsManagerContract
{
$settings = $this->all();
foreach ($paths as $path) {
array_forget($settings, $path);
Arr::forget($settings, $path);
}

$this->apply($settings);
Expand All @@ -67,7 +52,7 @@ public function deleteMultiple(iterable $paths): SettingsManagerContract
public function set(string $path, $value): SettingsManagerContract
{
$settings = $this->all();
array_set($settings, $path, $value);
Arr::set($settings, $path, $value);

return $this->apply($settings);
}
Expand Down
21 changes: 3 additions & 18 deletions src/Managers/TableSettingsManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Glorand\Model\Settings\Contracts\SettingsManagerContract;
use Glorand\Model\Settings\Models\ModelSettings;
use Illuminate\Support\Arr;

/**
* Class TableSettingsManager
Expand Down Expand Up @@ -42,29 +43,13 @@ public function delete(string $path = null): SettingsManagerContract
}
} else {
$settings = $this->all();
array_forget($settings, $path);
Arr::forget($settings, $path);
$this->apply($settings);
}

return $this;
}

/**
* @param iterable $paths
* @return \Glorand\Model\Settings\Contracts\SettingsManagerContract
*/
public function deleteMultiple(iterable $paths): SettingsManagerContract
{
$settings = $this->all();
foreach ($paths as $path) {
array_forget($settings, $path);
}

$this->apply($settings);

return $this;
}

/**
* @param string $path
* @param mixed $value
Expand All @@ -73,7 +58,7 @@ public function deleteMultiple(iterable $paths): SettingsManagerContract
public function set(string $path, $value): SettingsManagerContract
{
$settings = $this->all();
array_set($settings, $path, $value);
Arr::set($settings, $path, $value);

return $this->apply($settings);
}
Expand Down
6 changes: 3 additions & 3 deletions src/Traits/HasSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Glorand\Model\Settings\Traits;

use Glorand\Model\Settings\Managers\AbstractSettingsManager;
use Illuminate\Support\Arr;

trait HasSettings
Expand All @@ -15,8 +16,7 @@ public function getDefaultSettings(): array
return [];
}

/**
* @return array
*/
abstract public function getSettingsValue(): array;

abstract public function settings(): AbstractSettingsManager;
}
3 changes: 2 additions & 1 deletion src/Traits/HasSettingsField.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Glorand\Model\Settings\Contracts\SettingsManagerContract;
use Glorand\Model\Settings\Exceptions\ModelSettingsException;
use Glorand\Model\Settings\Managers\FieldSettingsManager;
use Illuminate\Support\Arr;

/**
* Trait HasSettingsField
Expand Down Expand Up @@ -36,7 +37,7 @@ public function getSettingsValue(): array
{
$settingsFieldName = $this->getSettingsFieldName();
$attributes = $this->getAttributes();
if (!array_has($attributes, $settingsFieldName)) {
if (!Arr::has($attributes, $settingsFieldName)) {
throw new ModelSettingsException("Unknown field ($settingsFieldName) on table {$this->getTable()}");
}

Expand Down
7 changes: 3 additions & 4 deletions tests/FieldSettingsManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Glorand\Model\Settings\Tests;

use Glorand\Model\Settings\Exceptions\ModelSettingsException;
use Glorand\Model\Settings\Tests\Models\UserWithField as User;
use Glorand\Model\Settings\Traits\HasSettingsField;

Expand Down Expand Up @@ -54,12 +55,10 @@ public function testDefaultValue()
$this->assertEquals($this->model->settings()->all(), array_merge($this->defaultSettingsTestArray, $this->testArray));
}

/**
* @expectedException \Glorand\Model\Settings\Exceptions\ModelSettingsException
* @expectedExceptionMessage Unknown field
*/
public function testSettingsMissingSettingsField()
{
$this->expectException(ModelSettingsException::class);
$this->expectExceptionMessage('Unknown field');
$this->model->settingsFieldName = 'test';
$this->model->settings()->all();
}
Expand Down
3 changes: 2 additions & 1 deletion tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Glorand\Model\Settings\Tests\Models\UserWithField;
use Glorand\Model\Settings\Tests\Models\WrongUser;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Schema;
use Orchestra\Testbench\TestCase as OrchestraTestCase;

Expand All @@ -26,7 +27,7 @@ public function setUp(): void
protected function checkRequirements()
{
collect($this->getAnnotations())->filter(function ($location) {
return in_array('!Travis', array_get($location, 'requires', []));
return in_array('!Travis', Arr::get($location, 'requires', []));
})->each(function ($location) {
$this->markTestSkipped('Travis will not run this test.');
});
Expand Down
13 changes: 6 additions & 7 deletions tests/TestWrongModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,27 @@

namespace Glorand\Model\Settings\Tests;

use Glorand\Model\Settings\Exceptions\ModelSettingsException;
use Glorand\Model\Settings\Managers\FieldSettingsManager;
use Glorand\Model\Settings\Tests\Models\WrongUser;
use Glorand\Model\Settings\Tests\Models\WrongUserWithField;

class TestWrongModelTest extends TestCase
{
/**
* @throws \Exception
* @expectedException \Glorand\Model\Settings\Exceptions\ModelSettingsException
* @expectedExceptionMessage missing HasSettings
* @throws \Glorand\Model\Settings\Exceptions\ModelSettingsException
*/
public function testSettingsFieldUndefined()
{
$this->expectException(ModelSettingsException::class);
$this->expectExceptionMessage('missing HasSettings');
new FieldSettingsManager(WrongUser::first());
}

/**
* @expectedException \Glorand\Model\Settings\Exceptions\ModelSettingsException
* @expectedExceptionMessage Unknown field
*/
public function testSettingsMissingSettingsField()
{
$this->expectException(ModelSettingsException::class);
$this->expectExceptionMessage('Unknown field');
$model = WrongUserWithField::first();
$model->settings()->all();
}
Expand Down

0 comments on commit 0a106d7

Please sign in to comment.