Skip to content
This repository was archived by the owner on Sep 22, 2024. It is now read-only.

Commit 05df527

Browse files
committed
Remove dependency for adding casts, also enables usage of accessors with same name as db column name
1 parent 631c52d commit 05df527

File tree

3 files changed

+16
-6
lines changed

3 files changed

+16
-6
lines changed

src/Config.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public function __construct(Model $model, $configKey = null)
4040

4141
$this->configKey = $configKey;
4242

43-
$this->data = $model->{$this->getConfigKey()};
43+
$this->data = $this->getRawData();
4444
}
4545

4646
/**
@@ -100,7 +100,7 @@ public function remove(string $key)
100100
*/
101101
public function all()
102102
{
103-
return $this->model->{$this->getConfigKey()};
103+
return $this->getRawData();
104104
}
105105

106106
/**
@@ -134,6 +134,16 @@ protected function getConfigKey()
134134
return $this->configKey ?? $this->model->getConfigKey();
135135
}
136136

137+
/**
138+
* Get the raw data from the model.
139+
*
140+
* @return array
141+
*/
142+
protected function getRawData()
143+
{
144+
return json_decode($this->model->getAttributes()[$this->getConfigKey()] ?? '{}', true);
145+
}
146+
137147
/**
138148
* Determine if the given attribute exists.
139149
*

tests/ConfigurableTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,10 +130,10 @@ function it_can_use_a_custom_config()
130130

131131
// This method has been added to the User model
132132
// Take a look in the class to see how to do this
133-
$user->extras()->set('test', 'test value');
133+
$user->extras->set('test', 'test value');
134134

135-
$this->assertInstanceOf(Config::class, $user->extras());
136-
$this->assertEquals($user->extras()->test, 'test value');
135+
$this->assertInstanceOf(Config::class, $user->extras);
136+
$this->assertEquals($user->extras->test, 'test value');
137137
}
138138

139139
protected function createUser(array $overwrites = [])

tests/Models/User.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class User extends Model
3434
*
3535
* @return \Signifly\Configurable\Config
3636
*/
37-
public function extras()
37+
public function getExtrasAttribute()
3838
{
3939
return new Config($this, 'extras');
4040
}

0 commit comments

Comments
 (0)