File tree Expand file tree Collapse file tree 4 files changed +74
-2
lines changed
Expand file tree Collapse file tree 4 files changed +74
-2
lines changed Original file line number Diff line number Diff line change @@ -18,6 +18,21 @@ class Setting extends Entity
1818 'protected ' => 'bool ' ,
1919 ];
2020
21+ /**
22+ * Sets the cast datatype for
23+ * the content field.
24+ *
25+ * @param string $datatype
26+ *
27+ * @return $this
28+ */
29+ public function setContentCast (string $ datatype = 'string ' )
30+ {
31+ $ this ->casts ['content ' ] = $ datatype ;
32+
33+ return $ this ;
34+ }
35+
2136 /**
2237 * Forces the content to cast
2338 * to its predefined datatype.
@@ -26,7 +41,7 @@ class Setting extends Entity
2641 */
2742 public function __construct (array $ data = null )
2843 {
29- $ this ->casts [ ' content ' ] = $ data ['datatype ' ] ?? 'string ' ;
44+ $ this ->setContentCast ( $ data ['datatype ' ] ?? 'string ' ) ;
3045
3146 parent ::__construct ($ data );
3247 }
Original file line number Diff line number Diff line change @@ -96,7 +96,7 @@ public function getTemplates(): array
9696 self ::$ templates = [];
9797 foreach ($ templates as $ template )
9898 {
99- self ::$ templates [$ template ['name ' ]] = new Setting ($ template );
99+ self ::$ templates [$ template ['name ' ]] = ( new Setting ())-> setContentCast ( $ template [ ' datatype ' ])-> setAttributes ($ template );
100100 }
101101
102102 return self ::$ templates ;
Original file line number Diff line number Diff line change @@ -23,6 +23,24 @@ public function testContentCastsToDatatype()
2323 $ this ->assertSame (12 , $ setting ->content );
2424 }
2525
26+ public function testContentJsonCastsToDatatype ()
27+ {
28+ $ array = [
29+ 'a ' => 'Bananas ' ,
30+ 'b ' => 'Oranges ' ,
31+ ];
32+
33+ $ setting = new Setting ([
34+ 'datatype ' => 'json-array ' ,
35+ 'content ' => $ array ,
36+ ]);
37+
38+ $ check = $ this ->getPrivateProperty ($ setting , 'attributes ' )['content ' ];
39+ $ this ->assertSame ('{"a":"Bananas","b":"Oranges"} ' , $ check );
40+
41+ $ this ->assertSame ($ array , $ setting ->content );
42+ }
43+
2644 public function testFaked ()
2745 {
2846 $ setting = fake (SettingModel::class, null , false );
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ use Tatter \Settings \Entities \Setting ;
4+ use Tatter \Settings \Models \SettingModel ;
5+ use Tests \Support \SettingsTestCase ;
6+
7+ final class ModelTest extends SettingsTestCase
8+ {
9+ public function testGetTemplatesDoesNotCastDatabaseValues ()
10+ {
11+ // Insert using the model (e.g., admin dashboard)
12+ model (SettingModel::class)->insert ([
13+ 'name ' => 'fruits ' ,
14+ 'datatype ' => 'json-array ' ,
15+ 'summary ' => 'Yummy fruits ' ,
16+ 'content ' => '{"a":"Bananas","b":"Oranges"} ' ,
17+ 'protected ' => 1 ,
18+ ]);
19+
20+ // Load templates from the database into the model
21+ $ model = model (SettingModel::class);
22+ $ model ->getTemplates ();
23+
24+ // The attibute of the entity should be encoded
25+ $ setting = $ this ->getPrivateProperty ($ model , 'templates ' )['fruits ' ];
26+ $ attribute = $ this ->getPrivateProperty ($ setting , 'attributes ' )['content ' ];
27+
28+ $ this ->assertSame ('{"a":"Bananas","b":"Oranges"} ' , $ attribute );
29+
30+ // The value returned from the getter should be decoded
31+ $ array = [
32+ 'a ' => 'Bananas ' ,
33+ 'b ' => 'Oranges ' ,
34+ ];
35+
36+ $ this ->assertSame ($ array , config ('Settings ' )->fruits );
37+ }
38+
39+ }
You can’t perform that action at this time.
0 commit comments