File tree 9 files changed +32
-45
lines changed
9 files changed +32
-45
lines changed Original file line number Diff line number Diff line change @@ -16,13 +16,14 @@ composer require hareland/laravel-immutable-attributes
16
16
## Define Immutable Attributes
17
17
18
18
Define the attributes to be immutable on your model:
19
+
19
20
``` php
20
21
<?php
21
22
22
23
namespace App\Models;
23
24
24
25
use Illuminate\Database\Eloquent\Model;
25
- use Hareland\Immutable \Traits\HasImmutableAttributes;
26
+ use Hareland\LaravelImmutableAttributes \Traits\HasImmutableAttributes;
26
27
27
28
class Product extends Model
28
29
{
Original file line number Diff line number Diff line change 5
5
"license" : " MIT" ,
6
6
"authors" : [
7
7
{
8
- "name" : " Kristian Hareland" ,
9
-
8
+ "name" : " Hareland" ,
9
+
10
+ "homepage" : " https://github.com/hareland"
10
11
}
11
12
],
12
13
"require" : {
23
24
},
24
25
"autoload" : {
25
26
"psr-4" : {
26
- "Hareland\\ Immutable \\ " : " src/"
27
+ "Hareland\\ LaravelImmutableAttributes \\ " : " src/"
27
28
}
28
29
},
29
30
"autoload-dev" : {
30
31
"psr-4" : {
31
- "Hareland\\ Immutable \\ Tests\\ " : " tests/"
32
+ "Hareland\\ LaravelImmutableAttributes \\ Tests\\ " : " tests/"
32
33
}
33
34
},
34
35
"extra" : {
35
36
"laravel" : {
36
37
"providers" : [
37
- " Hareland\\ Immutable \\ ImmutableServiceProvider "
38
+ " Hareland\\ LaravelImmutableAttributes \\ LaravelImmutableAttributesServiceProvider "
38
39
]
39
40
}
40
41
},
Original file line number Diff line number Diff line change 1
1
<?php
2
2
3
- namespace Hareland \Immutable ;
3
+ namespace Hareland \LaravelImmutableAttributes ;
4
4
5
5
6
6
use Illuminate \Support \ServiceProvider ;
Original file line number Diff line number Diff line change 1
1
<?php
2
2
3
- namespace Hareland \Immutable \Traits ;
3
+ namespace Hareland \LaravelImmutableAttributes \Traits ;
4
4
5
5
use Illuminate \Database \Eloquent \Model ;
6
6
7
7
trait HasImmutableAttributes
8
8
{
9
- public static function bootImmutableAttributes (): void
9
+ public static function bootHasImmutableAttributes (): void
10
10
{
11
11
static ::updating (
12
- function (Model $ model ) {
13
- $ this ->resetImmutableAttributes ($ model );
14
- }
12
+ fn (Model $ model ) => $ model ->resetImmutableAttributes ($ model ),
15
13
);
16
14
}
17
15
18
16
private function resetImmutableAttributes (Model $ model ): void
19
17
{
20
- collect ($ this ->getImmutableAttributes ())
21
- ->each (
22
- function (string $ attribute ) use ($ model ) {
23
- if (!is_null ($ model ->getOriginal ($ attribute ))
24
- && $ model ->getOriginal ($ attribute ) !== $ model ->{$ attribute }) {
25
- $ model ->{$ attribute } = $ model ->getOriginal ($ attribute );
26
- }
27
- }
28
- );
18
+ foreach ($ this ->getImmutableAttributes () as $ attribute ) {
19
+ if (!is_null ($ model ->getOriginal ($ attribute ))
20
+ && $ model ->getOriginal ($ attribute ) !== $ model ->{$ attribute }) {
21
+ $ model ->{$ attribute } = $ model ->getOriginal ($ attribute );
22
+ }
23
+ }
29
24
}
30
25
31
26
public function setAttribute ($ key , $ value )
Original file line number Diff line number Diff line change 1
1
<?php
2
2
3
- namespace Hareland \Immutable \Tests ;
3
+ namespace Hareland \LaravelImmutableAttributes \Tests ;
4
4
5
5
6
- use Hareland \Immutable \Traits \HasImmutableAttributes ;
6
+ use Hareland \LaravelImmutableAttributes \Traits \HasImmutableAttributes ;
7
7
use Illuminate \Database \Eloquent \Model ;
8
8
9
9
/**
Original file line number Diff line number Diff line change 1
1
<?php
2
2
3
- namespace Hareland \Immutable \Tests ;
3
+ namespace Hareland \LaravelImmutableAttributes \Tests ;
4
4
5
- use Hareland \Immutable \LaravelImmutableAttributesServiceProvider ;
5
+ use Hareland \LaravelImmutableAttributes \LaravelImmutableAttributesServiceProvider ;
6
6
use Orchestra \Testbench \TestCase as Orchestra ;
7
7
8
- class TestCase extends Orchestra
8
+ class LaravelTestCase extends Orchestra
9
9
{
10
10
protected function setUp (): void
11
11
{
Original file line number Diff line number Diff line change 1
1
<?php
2
2
3
- use Hareland \Immutable \Tests \TestCase ;
3
+ use Hareland \LaravelImmutableAttributes \Tests \LaravelTestCase ;
4
4
5
- uses (TestCase ::class)->in (__DIR__ );
5
+ uses (LaravelTestCase ::class)->in (__DIR__ );
Original file line number Diff line number Diff line change 1
1
<?php
2
2
3
- use Hareland \Immutable \Tests \ExampleImmutableModel ;
3
+ use Hareland \LaravelImmutableAttributes \Tests \ExampleImmutableModel ;
4
4
5
5
const TEST_LABEL = 'Test ' ;
6
6
const TEST_PRICE = 123.45 ;
7
+ const TEST_OTHER_LABEL = 'other-Test ' ;
7
8
const TEST_INVALID_PRICE = 456.78 ;
8
9
9
-
10
- test ('can fill a new model ' , function () {
11
- $ model = new ExampleImmutableModel ([
12
- 'label ' => TEST_LABEL ,
13
- 'price ' => TEST_PRICE ,
14
- ]);
15
-
16
- $ model ->save ();
17
-
18
- expect ($ model ->exists )->toBe (true );
19
-
20
- });
21
-
22
- test ('cannot fill immutable on update ' , function () {
10
+ it ('cannot fill immutable attributes on update ' , function () {
23
11
$ model = new ExampleImmutableModel ([
24
12
'label ' => TEST_LABEL ,
25
13
'price ' => TEST_PRICE ,
30
18
expect ($ model ->label )->toBe (TEST_LABEL );
31
19
expect ($ model ->price )->toBe (TEST_PRICE );
32
20
21
+ $ model ->label = TEST_OTHER_LABEL ;
33
22
$ model ->price = TEST_INVALID_PRICE ;
34
23
$ model ->save ();
35
- $ model = $ model ->refresh ();
36
24
25
+ //We expect the label to not be immutable, only the price.
26
+ expect ($ model ->label )->toBe (TEST_OTHER_LABEL );
37
27
expect ($ model ->price )->not ()->toBe (TEST_INVALID_PRICE );
38
28
});
Original file line number Diff line number Diff line change @@ -23,6 +23,6 @@ public function up()
23
23
24
24
public function down ()
25
25
{
26
- //..
26
+ Schema:: drop ( ' example_immutable_model ' );
27
27
}
28
28
};
You can’t perform that action at this time.
0 commit comments