Skip to content

Commit 484e1e4

Browse files
committed
cleanups & restructuring
1 parent 2c385a1 commit 484e1e4

File tree

6 files changed

+99
-44
lines changed

6 files changed

+99
-44
lines changed

composer.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,15 @@
2222
"scripts" : {
2323
"analyse" : "vendor/bin/phpstan analyse",
2424
"test" : "vendor/bin/phpunit",
25-
"coverage" : " php -d xdebug.mode=coverage vendor/bin/phpunit ./tests --coverage-clover='reports/coverage/coverage.xml' --coverage-html='reports/coverage'"
25+
"coverage" : "php -d xdebug.mode=coverage vendor/bin/phpunit ./tests --coverage-clover='reports/coverage/coverage.xml' --coverage-html='reports/coverage'"
2626
},
2727
"minimum-stability": "dev",
2828
"prefer-stable": true,
2929
"require": {
3030
"php": "^8.1",
3131
"laravel/framework": "^10 | ^11",
32-
"ext-pdo": "*"
32+
"ext-pdo": "*",
33+
"doctrine/dbal": "^3.8"
3334
},
3435
"require-dev": {
3536
"orchestra/testbench": "^8.0",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
use Matteoc99\LaravelPreference\Models\Preference;
7+
8+
return new class extends Migration {
9+
10+
public function up()
11+
{
12+
$preferenceTable = (new Preference())->getTable();
13+
14+
Schema::table($preferenceTable, function (Blueprint $table) {
15+
$table->text('policy')->change();
16+
$table->text('cast')->change();
17+
$table->text('rule')->change();
18+
});
19+
}
20+
21+
public function down()
22+
{
23+
$preferenceTable = (new Preference())->getTable();
24+
25+
Schema::table($preferenceTable, function (Blueprint $table) {
26+
$table->json('policy')->change();
27+
$table->json('cast')->change();
28+
$table->json('rule')->change();
29+
});
30+
}
31+
};

src/Contracts/PreferencePolicy.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
interface PreferencePolicy
99
{
10-
public function index(Authenticatable $user, Preference $preference, mixed $value): bool;
10+
public function index(Authenticatable $user, string $preferences): bool;
1111

1212
public function get(Authenticatable $user, Preference $preference, mixed $value): bool;
1313

src/Factory/PreferenceBuilder.php

+8-39
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,8 @@ public function create(): Preference
9696

9797
public function updateOrCreate(): Preference
9898
{
99-
if (isset($this->preference->default_value)) {
100-
ValidationHelper::validatePreference($this->preference);
101-
}
99+
ValidationHelper::validatePreference($this->preference);
100+
102101

103102
$this->preference = Preference::updateOrCreate(
104103
$this->preference->toArrayOnly(['name', 'group']),
@@ -116,45 +115,15 @@ public static function initBulk(array $preferences, bool $nullable = false): voi
116115
throw new InvalidArgumentException("no preferences provided");
117116
}
118117

119-
foreach ($preferences as $key => &$preferenceData) {
118+
foreach ($preferences as $index => &$preferenceData) {
120119
if (empty($preferenceData['cast'])) {
121120
$preferenceData['cast'] = Cast::STRING;
122121
}
123122
if (!array_key_exists('nullable', $preferenceData)) {
124123
$preferenceData['nullable'] = $nullable;
125124
}
126125

127-
if (empty($preferenceData['name']) || !($preferenceData['name'] instanceof PreferenceGroup)) {
128-
throw new InvalidArgumentException(
129-
sprintf("index: #%s name is required and needs to be a PreferenceGroup", $key)
130-
);
131-
}
132-
if (empty($preferenceData['cast']) || !($preferenceData['cast'] instanceof CastableEnum)) {
133-
throw new InvalidArgumentException(
134-
sprintf("index: #%s cast is required and needs to implement 'CastableEnum'", $key)
135-
);
136-
}
137-
if (!empty($preferenceData['rule']) && !$preferenceData['rule'] instanceof ValidationRule) {
138-
throw new InvalidArgumentException(
139-
sprintf("index: #%s validation rule musst implement ValidationRule", $key)
140-
);
141-
}
142-
143-
if (!empty($preferenceData['default_value'])) {
144-
ValidationHelper::validateValue(
145-
$preferenceData['default_value'],
146-
$preferenceData['cast'] ?? null,
147-
$preferenceData['rule'] ?? null,
148-
$preferenceData['nullable'],
149-
);
150-
}
151-
152-
153-
if (array_key_exists('group', $preferenceData)) {
154-
throw new InvalidArgumentException(
155-
sprintf("index: #%s group has been deprecated", $key)
156-
);
157-
}
126+
ValidationHelper::validatePreferenceData($preferenceData, $index);
158127

159128
SerializeHelper::conformNameAndGroup($preferenceData['name'], $preferenceData['group']);
160129

@@ -166,14 +135,14 @@ public static function initBulk(array $preferences, bool $nullable = false): voi
166135
$preferenceData['default_value'] = $valueCaster->set(null, '', $preferenceData['default_value'], []);
167136
}
168137

169-
170138
$preferenceData['cast'] = serialize($preferenceData['cast']);
171139

172140
// Ensure Defaults
173141
$preferenceData = array_merge([
174142
'group' => 'general',
175143
'default_value' => null,
176144
'description' => '',
145+
'policy' => null,
177146
'rule' => null,
178147
'nullable' => false,
179148
], $preferenceData);
@@ -189,10 +158,10 @@ public static function deleteBulk(array $preferences): int
189158
}
190159
$query = Preference::query();
191160

192-
foreach ($preferences as $key => $preferenceData) {
193-
if (empty($preferenceData['name'])) {
161+
foreach ($preferences as $index => $preferenceData) {
162+
if (empty($preferenceData['name']) || !($preferenceData['name'] instanceof PreferenceGroup)) {
194163
throw new InvalidArgumentException(
195-
sprintf("index: #%s name is required", $key)
164+
sprintf("index: #%s name is required and must implement PreferenceGroup", $index)
196165
);
197166
}
198167

src/Utils/ValidationHelper.php

+53-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55
use Illuminate\Contracts\Validation\ValidationRule;
66
use Illuminate\Support\Facades\Validator;
77
use Illuminate\Validation\ValidationException;
8+
use InvalidArgumentException;
89
use Matteoc99\LaravelPreference\Contracts\CastableEnum;
10+
use Matteoc99\LaravelPreference\Contracts\PreferenceGroup;
11+
use Matteoc99\LaravelPreference\Contracts\PreferencePolicy;
912
use Matteoc99\LaravelPreference\Models\Preference;
1013

1114
class ValidationHelper
@@ -15,6 +18,7 @@ class ValidationHelper
1518
* @param mixed $value
1619
* @param CastableEnum|null $cast
1720
* @param ValidationRule|null $rule
21+
* @param bool $nullable
1822
*
1923
* @throws ValidationException
2024
*/
@@ -34,7 +38,55 @@ public static function validateValue(mixed $value, ?CastableEnum $cast, ?Validat
3438
*/
3539
public static function validatePreference(Preference $preference)
3640
{
37-
self::validateValue($preference->default_value, $preference->cast, $preference->rule, $preference->nullable);
41+
if (isset($preference->default_value)) {
42+
self::validateValue($preference->default_value, $preference->cast, $preference->rule, $preference->nullable);
43+
}
44+
}
45+
46+
/**
47+
* @param array $preferenceData
48+
* @param int $index
49+
*
50+
* @throws ValidationException
51+
*/
52+
public static function validatePreferenceData(array $preferenceData, int $index): void
53+
{
54+
if (empty($preferenceData['name']) || !($preferenceData['name'] instanceof PreferenceGroup)) {
55+
throw new InvalidArgumentException(
56+
sprintf("index: #%s name is required and needs to be a PreferenceGroup", $index)
57+
);
58+
}
59+
if (empty($preferenceData['cast']) || !($preferenceData['cast'] instanceof CastableEnum)) {
60+
throw new InvalidArgumentException(
61+
sprintf("index: #%s cast is required and needs to implement 'CastableEnum'", $index)
62+
);
63+
}
64+
if (!empty($preferenceData['rule']) && !$preferenceData['rule'] instanceof ValidationRule) {
65+
throw new InvalidArgumentException(
66+
sprintf("index: #%s validation rule musst implement ValidationRule", $index)
67+
);
68+
}
69+
if (!empty($preferenceData['policy']) && !$preferenceData['policy'] instanceof PreferencePolicy) {
70+
throw new InvalidArgumentException(
71+
sprintf("index: #%s policy musst implement PreferencePolicy", $index)
72+
);
73+
}
74+
75+
if (!empty($preferenceData['default_value'])) {
76+
ValidationHelper::validateValue(
77+
$preferenceData['default_value'],
78+
$preferenceData['cast'],
79+
$preferenceData['rule'] ?? null,
80+
$preferenceData['nullable'],
81+
);
82+
}
83+
84+
85+
if (array_key_exists('group', $preferenceData)) {
86+
throw new InvalidArgumentException(
87+
sprintf("index: #%s group has been deprecated", $index)
88+
);
89+
}
3890
}
3991

4092
private static function getValidationRules(?CastableEnum $cast, ?ValidationRule $rule, bool $nullable = false): array

tests/TestCase.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Illuminate\Database\Schema\Blueprint;
66
use Illuminate\Database\Schema\Builder;
7+
use Illuminate\Foundation\Application;
78
use Illuminate\Foundation\Testing\RefreshDatabase;
89
use Illuminate\Support\Facades\Auth;
910
use Illuminate\Support\Facades\Schema;
@@ -55,9 +56,10 @@ protected function getPackageProviders($app)
5556
/**
5657
* Define environment setup.
5758
*
58-
* @param \Illuminate\Foundation\Application $app
59+
* @param Application $app
5960
*
6061
* @return void
62+
* @throws \Exception
6163
*/
6264
protected function getEnvironmentSetUp($app)
6365
{

0 commit comments

Comments
 (0)