From 68bbc276294fbc94b3835b55065cbe38561077fc Mon Sep 17 00:00:00 2001 From: pepijn-de-wachter Date: Wed, 27 Mar 2019 19:53:33 +0100 Subject: [PATCH 1/3] enclose return of getRawSchemaLessAttributes with is_array check, Fixes #45 --- src/SchemalessAttributes.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/SchemalessAttributes.php b/src/SchemalessAttributes.php index df2e311..93822c4 100644 --- a/src/SchemalessAttributes.php +++ b/src/SchemalessAttributes.php @@ -138,6 +138,6 @@ public static function scopeWithSchemalessAttributes(string $attributeName): Bui protected function getRawSchemalessAttributes(): array { - return json_decode($this->model->getAttributes()[$this->sourceAttributeName] ?? '{}', true); + return is_array($decoded = json_decode($this->model->getAttributes()[$this->sourceAttributeName] ?? '{}', true)) ? $decoded : []; } } From 47db2ff4e0d7b433b900328811ab94ddc05f1fe0 Mon Sep 17 00:00:00 2001 From: pepijn-de-wachter Date: Sat, 30 Mar 2019 15:56:10 +0100 Subject: [PATCH 2/3] Split up code. Added test. --- src/SchemalessAttributes.php | 8 +++++++- tests/HasSchemalessAttributesTest.php | 10 ++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/SchemalessAttributes.php b/src/SchemalessAttributes.php index 93822c4..7f60dd8 100644 --- a/src/SchemalessAttributes.php +++ b/src/SchemalessAttributes.php @@ -138,6 +138,12 @@ public static function scopeWithSchemalessAttributes(string $attributeName): Bui protected function getRawSchemalessAttributes(): array { - return is_array($decoded = json_decode($this->model->getAttributes()[$this->sourceAttributeName] ?? '{}', true)) ? $decoded : []; + $decoded = json_decode($this->model->getAttributes()[$this->sourceAttributeName] ?? '{}', true); + + if(!is_array($decoded)) { + return []; + } + + return $decoded; } } diff --git a/tests/HasSchemalessAttributesTest.php b/tests/HasSchemalessAttributesTest.php index 89a3080..4886fd8 100644 --- a/tests/HasSchemalessAttributesTest.php +++ b/tests/HasSchemalessAttributesTest.php @@ -328,6 +328,16 @@ public function if_an_iterable_is_passed_to_set_it_will_defer_to_setMany() $this->assertEquals('subVal1', $this->testModel->schemaless_attributes->arr['subKey1']); } + /** @test */ + public function if_database_column_does_not_contain_json_decodable_string_it_returns_empty_array() + { + $model = TestModel::create(['schemaless_attributes' => 'null']); + + $this->assertEquals('"null"', $model->getAttributes()['schemaless_attributes']); + $this->assertIsNotArray(json_decode($model->getAttributes()['schemaless_attributes'] ?? '{}', true)); + $this->assertEquals([], $model->getSchemalessAttributesAttribute()->all()); + } + protected function assertContainsModels(array $expectedModels, Collection $actualModels) { $assertionFailedMessage = 'Expected '.count($expectedModels).' models. Got '.$actualModels->count().' models'; From f96e6402e683d8137d4a890756ac3de1fc4a2d7b Mon Sep 17 00:00:00 2001 From: pepijn-de-wachter Date: Sat, 30 Mar 2019 16:00:43 +0100 Subject: [PATCH 3/3] fixed code style --- src/SchemalessAttributes.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/SchemalessAttributes.php b/src/SchemalessAttributes.php index 7f60dd8..d4517a4 100644 --- a/src/SchemalessAttributes.php +++ b/src/SchemalessAttributes.php @@ -140,7 +140,7 @@ protected function getRawSchemalessAttributes(): array { $decoded = json_decode($this->model->getAttributes()[$this->sourceAttributeName] ?? '{}', true); - if(!is_array($decoded)) { + if (! is_array($decoded)) { return []; }