@@ -19,8 +19,8 @@ |
||
19 | 19 | * |
20 | 20 | * @param string $message |
21 | 21 | */ |
22 | - public function __construct( string $message = null ) |
|
22 | + public function __construct(string $message = null) |
|
23 | 23 | { |
24 | - parent::__construct( $message ?: 'You cannot persist a model with applied adjustments and save protection enabled' ); |
|
24 | + parent::__construct($message ?: 'You cannot persist a model with applied adjustments and save protection enabled'); |
|
25 | 25 | } |
26 | 26 | } |
27 | 27 | \ No newline at end of file |
@@ -49,6 +49,6 @@ |
||
49 | 49 | */ |
50 | 50 | public function adjustable():MorphTo |
51 | 51 | { |
52 | - return $this->morphTo( config( 'adjuster.adjustable_column' ) ); |
|
52 | + return $this->morphTo(config('adjuster.adjustable_column')); |
|
53 | 53 | } |
54 | 54 | } |
55 | 55 | \ No newline at end of file |
@@ -36,8 +36,8 @@ discard block |
||
36 | 36 | */ |
37 | 37 | protected static function bootCanBeAdjusted() |
38 | 38 | { |
39 | - static::saving( function ( Adjustable $model ) { |
|
40 | - if ( $model->isAdjusted() && $model->hasSaveProtection() ) { |
|
39 | + static::saving(function(Adjustable $model) { |
|
40 | + if ($model->isAdjusted() && $model->hasSaveProtection()) { |
|
41 | 41 | throw new ModelAdjustedException(); |
42 | 42 | } |
43 | 43 | } ); |
@@ -52,25 +52,25 @@ discard block |
||
52 | 52 | * @param array $attributes |
53 | 53 | * @return Model|null |
54 | 54 | */ |
55 | - public function adjust( array $changes, array $attributes = [ ] ) |
|
55 | + public function adjust(array $changes, array $attributes = [ ]) |
|
56 | 56 | { |
57 | - $changesColumn = config( 'adjuster.changes_column' ); |
|
58 | - $adjustment = $this->adjustment()->exists() ? $this->adjustment : app( 'adjuster.model' ); |
|
59 | - $existingChanges = collect( $adjustment->$changesColumn ); |
|
57 | + $changesColumn = config('adjuster.changes_column'); |
|
58 | + $adjustment = $this->adjustment()->exists() ? $this->adjustment : app('adjuster.model'); |
|
59 | + $existingChanges = collect($adjustment->$changesColumn); |
|
60 | 60 | |
61 | 61 | // We will fetch any existing changes from the adjustment and then filter them down |
62 | 62 | // based on certain criterias. If the value is null or the adjusted value equals |
63 | 63 | // the original value we will remove the value before persisting the changes. |
64 | - $changes = $existingChanges->merge( $changes )->filter( function ( $value, $attribute ) { |
|
65 | - return ! is_null( $value ) && $this->$attribute !== $value; |
|
64 | + $changes = $existingChanges->merge($changes)->filter(function($value, $attribute) { |
|
65 | + return ! is_null($value) && $this->$attribute !== $value; |
|
66 | 66 | } ); |
67 | 67 | |
68 | - if ( $changes->isEmpty() ) { |
|
68 | + if ($changes->isEmpty()) { |
|
69 | 69 | $adjustment->delete(); |
70 | 70 | } else { |
71 | - $adjustment->fill( $attributes ); |
|
72 | - $adjustment->$changesColumn = $this->castChanges( $changes, $adjustment ); |
|
73 | - $this->adjustment()->save( $adjustment ); |
|
71 | + $adjustment->fill($attributes); |
|
72 | + $adjustment->$changesColumn = $this->castChanges($changes, $adjustment); |
|
73 | + $this->adjustment()->save($adjustment); |
|
74 | 74 | |
75 | 75 | return $adjustment; |
76 | 76 | } |
@@ -83,10 +83,10 @@ discard block |
||
83 | 83 | */ |
84 | 84 | public function adjustment():Relation |
85 | 85 | { |
86 | - if ( config( 'adjuster.polymorphic' ) ) { |
|
87 | - return $this->morphOne( config( 'adjuster.adjustment_model' ), config( 'adjuster.adjustable_column' ) ); |
|
86 | + if (config('adjuster.polymorphic')) { |
|
87 | + return $this->morphOne(config('adjuster.adjustment_model'), config('adjuster.adjustable_column')); |
|
88 | 88 | } else { |
89 | - return $this->hasOne( config( 'adjuster.adjustment_model' ), config( 'adjuster.adjustable_column' ) ); |
|
89 | + return $this->hasOne(config('adjuster.adjustment_model'), config('adjuster.adjustable_column')); |
|
90 | 90 | } |
91 | 91 | } |
92 | 92 | |
@@ -97,15 +97,15 @@ discard block |
||
97 | 97 | */ |
98 | 98 | public function applyAdjustments():Model |
99 | 99 | { |
100 | - $changes = $this->adjustment->{config( 'adjuster.changes_column' )} ?? null; |
|
100 | + $changes = $this->adjustment->{config('adjuster.changes_column')} ?? null; |
|
101 | 101 | |
102 | - if ( is_string( $changes ) ) { |
|
103 | - $changes = json_decode( $changes, true ); |
|
104 | - } elseif ( $changes instanceof Collection ) { |
|
102 | + if (is_string($changes)) { |
|
103 | + $changes = json_decode($changes, true); |
|
104 | + } elseif ($changes instanceof Collection) { |
|
105 | 105 | $changes = $changes->toArray(); |
106 | 106 | } |
107 | 107 | |
108 | - $this->fill( $changes ); |
|
108 | + $this->fill($changes); |
|
109 | 109 | $this->adjusted = true; |
110 | 110 | |
111 | 111 | return $this; |
@@ -129,7 +129,7 @@ discard block |
||
129 | 129 | */ |
130 | 130 | public function hasSaveProtection():bool |
131 | 131 | { |
132 | - return $this->saveProtection ?? config( 'adjuster.save_protection' ); |
|
132 | + return $this->saveProtection ?? config('adjuster.save_protection'); |
|
133 | 133 | } |
134 | 134 | |
135 | 135 | /** |
@@ -140,9 +140,9 @@ discard block |
||
140 | 140 | * @param Model $adjustment |
141 | 141 | * @return mixed |
142 | 142 | */ |
143 | - protected function castChanges( Collection $changes, Model $adjustment ) |
|
143 | + protected function castChanges(Collection $changes, Model $adjustment) |
|
144 | 144 | { |
145 | - switch ( $adjustment->hasCast( config( 'adjuster.changes_column' ) ) ) { |
|
145 | + switch ($adjustment->hasCast(config('adjuster.changes_column'))) { |
|
146 | 146 | case 'collection': |
147 | 147 | return $changes; |
148 | 148 | case 'array': |
@@ -27,14 +27,14 @@ discard block |
||
27 | 27 | */ |
28 | 28 | public function boot() |
29 | 29 | { |
30 | - $this->publishes( [ |
|
31 | - __DIR__ . '/../resources/config/adjuster.php' => config_path( 'adjuster.php' ) |
|
32 | - ], 'config' ); |
|
30 | + $this->publishes([ |
|
31 | + __DIR__ . '/../resources/config/adjuster.php' => config_path('adjuster.php') |
|
32 | + ], 'config'); |
|
33 | 33 | |
34 | - $timestamp = date( 'Y_m_d_His', time() ); |
|
35 | - $this->publishes( [ |
|
36 | - __DIR__ . '/../resources/migrations/create_adjustments_table.php.stub' => database_path( 'migrations' ) . '/' . $timestamp . '_create_adjustments_table.php', |
|
37 | - ], 'migrations' ); |
|
34 | + $timestamp = date('Y_m_d_His', time()); |
|
35 | + $this->publishes([ |
|
36 | + __DIR__ . '/../resources/migrations/create_adjustments_table.php.stub' => database_path('migrations') . '/' . $timestamp . '_create_adjustments_table.php', |
|
37 | + ], 'migrations'); |
|
38 | 38 | } |
39 | 39 | |
40 | 40 | /** |
@@ -44,9 +44,9 @@ discard block |
||
44 | 44 | */ |
45 | 45 | public function register() |
46 | 46 | { |
47 | - $this->mergeConfigFrom( __DIR__ . '/../resources/config/adjuster.php', 'adjuster' ); |
|
47 | + $this->mergeConfigFrom(__DIR__ . '/../resources/config/adjuster.php', 'adjuster'); |
|
48 | 48 | |
49 | - $this->app->bind( 'adjuster.model', function ( $app ) { |
|
49 | + $this->app->bind('adjuster.model', function($app) { |
|
50 | 50 | return new $app->config[ 'adjuster.adjustment_model' ]; |
51 | 51 | } ); |
52 | 52 | } |