From 7edafa4822f47eb32750da9a3081200faa5f98dc Mon Sep 17 00:00:00 2001 From: davitbek Date: Fri, 19 Jun 2020 23:05:39 +0400 Subject: [PATCH 01/13] add trait with multi-column support --- src/SchemalessAttributesTrait.php | 45 +++++++++++++++++++++++++++++++ tests/TestModelUsedTrait.php | 18 +++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 src/SchemalessAttributesTrait.php create mode 100644 tests/TestModelUsedTrait.php diff --git a/src/SchemalessAttributesTrait.php b/src/SchemalessAttributesTrait.php new file mode 100644 index 0000000..cd1ed70 --- /dev/null +++ b/src/SchemalessAttributesTrait.php @@ -0,0 +1,45 @@ +getSchemalessAttributes() as $attribute) { + $this->casts[$attribute] = 'array'; + } + } + + /** + * @return array + */ + public function getSchemalessAttributes() + { + return $this->schemalessAttributes ?? []; + } + + /** + * @param $key + * @return mixed|SchemalessAttributes + */ + public function __get($key) + { + if (in_array($key, $this->getSchemalessAttributes())) { + return SchemalessAttributes::createForModel($this, $key); + } + + return parent::__get($key); + } +} diff --git a/tests/TestModelUsedTrait.php b/tests/TestModelUsedTrait.php new file mode 100644 index 0000000..28ae019 --- /dev/null +++ b/tests/TestModelUsedTrait.php @@ -0,0 +1,18 @@ + Date: Fri, 19 Jun 2020 23:11:14 +0400 Subject: [PATCH 02/13] fix space --- src/SchemalessAttributesTrait.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/SchemalessAttributesTrait.php b/src/SchemalessAttributesTrait.php index cd1ed70..5ad6991 100644 --- a/src/SchemalessAttributesTrait.php +++ b/src/SchemalessAttributesTrait.php @@ -2,7 +2,6 @@ namespace Spatie\SchemalessAttributes; - use Illuminate\Support\Collection; /** From b7b4076aa4f747d062e1a1010e2507ab374c74dc Mon Sep 17 00:00:00 2001 From: davitbek Date: Tue, 25 Aug 2020 17:26:42 +0400 Subject: [PATCH 03/13] fill readme add unit tests --- README.md | 37 ++++++++++++++++++++ tests/SchemalessAttributesTraitTest.php | 46 +++++++++++++++++++++++++ 2 files changed, 83 insertions(+) create mode 100644 tests/SchemalessAttributesTraitTest.php diff --git a/README.md b/README.md index 26a2905..b88eedd 100644 --- a/README.md +++ b/README.md @@ -106,6 +106,43 @@ class TestModel extends Model } ``` +Or you can use `SchemalessAttributesTrait` trait with `$schemalessAttributes` property, and add only scope on your model. Here's an example of what you need to add if you've chosen `extra_attributes, other_extra_attributes` as your column name. + +```php +use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\Builder; +use Spatie\SchemalessAttributes\SchemalessAttributes; +use Spatie\SchemalessAttributes\SchemalessAttributesTrait; + +class TestModel extends Model +{ + use SchemalessAttributesTrait; + + // ... + + /** + * @var array + */ + protected $schemalessAttributes = [ + 'extra_attributes', + 'other_extra_attributes', + ]; + + public function scopeWithExtraAttributes(): Builder + { + return SchemalessAttributes::scopeWithSchemalessAttributes('extra_attributes'); + } + + public function scopeWithOtherExtraAttributes(): Builder + { + return SchemalessAttributes::scopeWithSchemalessAttributes('other_extra_attributes'); + } + + // ... +} +``` + + If you want to reuse this behaviour across multiple models you could opt to put the function in a trait of your own. Here's what that trait could look like: ```php diff --git a/tests/SchemalessAttributesTraitTest.php b/tests/SchemalessAttributesTraitTest.php new file mode 100644 index 0000000..8a7b0a7 --- /dev/null +++ b/tests/SchemalessAttributesTraitTest.php @@ -0,0 +1,46 @@ +testModel = new TestModelUsedTrait(); + } + + /** @test */ + public function schemaless_attributes_cast_as_array_initialize_schemaless_attributes_trait() + { + $this->assertTrue($this->testModel->hasCast('schemaless_attributes', 'array')); + } + + /** @test */ + public function other_schemaless_attributes_cast_as_array_initialize_schemaless_attributes_trait() + { + $this->assertTrue($this->testModel->hasCast('other_schemaless_attributes', 'array')); + } + + /** @test */ + public function test_get_schemaless_attributes() + { + $this->assertEquals(['schemaless_attributes', 'other_schemaless_attributes'], $this->testModel->getSchemalessAttributes()); + } + + /** @test */ + public function test_get() + { + $this->assertNull($this->testModel->schemaless_attributes->name); + } + + /** @test */ + public function test_parent_get() + { + $this->assertNull($this->testModel->not_existing_schemaless); + } +} From 307ade5acd409ce6c70527ae8e7f34b4549aed4f Mon Sep 17 00:00:00 2001 From: Freek Van der Herten Date: Tue, 25 Aug 2020 18:52:30 +0200 Subject: [PATCH 04/13] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index b88eedd..a172a2c 100644 --- a/README.md +++ b/README.md @@ -106,7 +106,7 @@ class TestModel extends Model } ``` -Or you can use `SchemalessAttributesTrait` trait with `$schemalessAttributes` property, and add only scope on your model. Here's an example of what you need to add if you've chosen `extra_attributes, other_extra_attributes` as your column name. +Alternatively, you can use `SchemalessAttributesTrait` trait with `$schemalessAttributes` property, and add only scope on your model. Here's an example of what you need to add if you've chosen `extra_attributes, other_extra_attributes` as your column name. ```php use Illuminate\Database\Eloquent\Model; From 572eafbd20eab2fe8f2bd7d61db28e3cc1dae80a Mon Sep 17 00:00:00 2001 From: davitbek Date: Thu, 27 Aug 2020 07:58:48 +0400 Subject: [PATCH 05/13] fix for laravel 5.6 --- src/SchemalessAttributesTrait.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/SchemalessAttributesTrait.php b/src/SchemalessAttributesTrait.php index 5ad6991..cb9f05a 100644 --- a/src/SchemalessAttributesTrait.php +++ b/src/SchemalessAttributesTrait.php @@ -12,7 +12,7 @@ trait SchemalessAttributesTrait { /** - * Register schemalessAttributes attributes as array(cast). + * */ public function initializeSchemalessAttributesTrait() { @@ -26,7 +26,7 @@ public function initializeSchemalessAttributesTrait() */ public function getSchemalessAttributes() { - return $this->schemalessAttributes ?? []; + return $this->schemalessAttributes ? $this->schemalessAttributes : []; } /** From aadc9d9285db8f8d97bad97586830a216c1cf0b6 Mon Sep 17 00:00:00 2001 From: davitbek Date: Thu, 27 Aug 2020 08:01:07 +0400 Subject: [PATCH 06/13] remove comments --- src/SchemalessAttributesTrait.php | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/SchemalessAttributesTrait.php b/src/SchemalessAttributesTrait.php index cb9f05a..745df3c 100644 --- a/src/SchemalessAttributesTrait.php +++ b/src/SchemalessAttributesTrait.php @@ -11,9 +11,6 @@ */ trait SchemalessAttributesTrait { - /** - * - */ public function initializeSchemalessAttributesTrait() { foreach ($this->getSchemalessAttributes() as $attribute) { From 6a052cbfb102290e5720a81716ea8109164ad78f Mon Sep 17 00:00:00 2001 From: davitbek Date: Thu, 27 Aug 2020 09:02:18 +0400 Subject: [PATCH 07/13] initializeSchemalessAttributesTrait for laravel 5.6 --- src/SchemalessAttributesTrait.php | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/SchemalessAttributesTrait.php b/src/SchemalessAttributesTrait.php index 745df3c..b1a7eaf 100644 --- a/src/SchemalessAttributesTrait.php +++ b/src/SchemalessAttributesTrait.php @@ -3,6 +3,7 @@ namespace Spatie\SchemalessAttributes; use Illuminate\Support\Collection; +use Illuminate\Support\Str; /** * @property array $schemalessAttributes @@ -11,6 +12,27 @@ */ trait SchemalessAttributesTrait { + /** + * @var + */ + private $initializeSchemalessAttributesTrait; + + /** + * Get the casts array. + * + * @return array + */ + public function getCasts() + { + if (Str::startsWith(app()->version(), '5.6')) { + if (! $this->initializeSchemalessAttributesTrait) { + $this->initializeSchemalessAttributesTrait(); + $this->initializeSchemalessAttributesTrait = true; + } + } + return parent::getCasts(); + } + public function initializeSchemalessAttributesTrait() { foreach ($this->getSchemalessAttributes() as $attribute) { @@ -23,7 +45,7 @@ public function initializeSchemalessAttributesTrait() */ public function getSchemalessAttributes() { - return $this->schemalessAttributes ? $this->schemalessAttributes : []; + return isset($this->schemalessAttributes) ? $this->schemalessAttributes : []; } /** From ac4f8fa735e08c1241def42aa05095cb22adfeaa Mon Sep 17 00:00:00 2001 From: davitbek Date: Thu, 27 Aug 2020 09:07:44 +0400 Subject: [PATCH 08/13] fix styleci --- src/SchemalessAttributesTrait.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/SchemalessAttributesTrait.php b/src/SchemalessAttributesTrait.php index b1a7eaf..7e51a33 100644 --- a/src/SchemalessAttributesTrait.php +++ b/src/SchemalessAttributesTrait.php @@ -30,6 +30,7 @@ public function getCasts() $this->initializeSchemalessAttributesTrait = true; } } + return parent::getCasts(); } From c6206d835f543899c2e102798fff816ab2cf0410 Mon Sep 17 00:00:00 2001 From: Freek Van der Herten Date: Thu, 27 Aug 2020 19:23:56 +0200 Subject: [PATCH 09/13] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index a172a2c..a8551ea 100644 --- a/README.md +++ b/README.md @@ -77,7 +77,7 @@ Schema::table('your_models', function (Blueprint $table) { ### Preparing the model -In order to work with the schemaless attributes you'll need to add a cast, an accessor and a scope on your model. Here's an example of what you need to add if you've chosen `extra_attributes` as your column name. +In order to work with the schemaless attributes you'll need to add a cast, an accessor and a scopes on your model. Here's an example of what you need to add if you've chosen `extra_attributes` as your column name. ```php use Illuminate\Database\Eloquent\Model; From 92f841cc41a837e5aebd95bfd3abbe4cef98e7cd Mon Sep 17 00:00:00 2001 From: Freek Van der Herten Date: Thu, 27 Aug 2020 19:25:40 +0200 Subject: [PATCH 10/13] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index a8551ea..76c9855 100644 --- a/README.md +++ b/README.md @@ -106,7 +106,7 @@ class TestModel extends Model } ``` -Alternatively, you can use `SchemalessAttributesTrait` trait with `$schemalessAttributes` property, and add only scope on your model. Here's an example of what you need to add if you've chosen `extra_attributes, other_extra_attributes` as your column name. +If you need support for multiple schemaless columns on a single model, you should use `SchemalessAttributesTrait` trait. Here's an example of what you need to add if you've chosen `extra_attributes, other_extra_attributes` as your column names. ```php use Illuminate\Database\Eloquent\Model; From d59a108cbb52ccf3c1bc6e9b67d2203f355e58e5 Mon Sep 17 00:00:00 2001 From: davitbek Date: Fri, 28 Aug 2020 01:56:45 +0400 Subject: [PATCH 11/13] not support laravel 5 --- src/SchemalessAttributesTrait.php | 22 ---------------------- tests/SchemalessAttributesTraitTest.php | 14 ++++++++++++-- 2 files changed, 12 insertions(+), 24 deletions(-) diff --git a/src/SchemalessAttributesTrait.php b/src/SchemalessAttributesTrait.php index 7e51a33..c713ae3 100644 --- a/src/SchemalessAttributesTrait.php +++ b/src/SchemalessAttributesTrait.php @@ -12,28 +12,6 @@ */ trait SchemalessAttributesTrait { - /** - * @var - */ - private $initializeSchemalessAttributesTrait; - - /** - * Get the casts array. - * - * @return array - */ - public function getCasts() - { - if (Str::startsWith(app()->version(), '5.6')) { - if (! $this->initializeSchemalessAttributesTrait) { - $this->initializeSchemalessAttributesTrait(); - $this->initializeSchemalessAttributesTrait = true; - } - } - - return parent::getCasts(); - } - public function initializeSchemalessAttributesTrait() { foreach ($this->getSchemalessAttributes() as $attribute) { diff --git a/tests/SchemalessAttributesTraitTest.php b/tests/SchemalessAttributesTraitTest.php index 8a7b0a7..be26b0d 100644 --- a/tests/SchemalessAttributesTraitTest.php +++ b/tests/SchemalessAttributesTraitTest.php @@ -2,6 +2,8 @@ namespace Spatie\SchemalessAttributes\Tests; +use Illuminate\Support\Str; + class SchemalessAttributesTraitTest extends TestCase { /** @var \Spatie\SchemalessAttributes\Tests\TestModelUsedTrait */ @@ -17,13 +19,21 @@ public function setUp(): void /** @test */ public function schemaless_attributes_cast_as_array_initialize_schemaless_attributes_trait() { - $this->assertTrue($this->testModel->hasCast('schemaless_attributes', 'array')); + if (Str::startsWith(app()->version(), '5.6')) { + $this->assertFalse($this->testModel->hasCast('schemaless_attributes', 'array')); + } else { + $this->assertTrue($this->testModel->hasCast('schemaless_attributes', 'array')); + } } /** @test */ public function other_schemaless_attributes_cast_as_array_initialize_schemaless_attributes_trait() { - $this->assertTrue($this->testModel->hasCast('other_schemaless_attributes', 'array')); + if (Str::startsWith(app()->version(), '5.6')) { + $this->assertFalse($this->testModel->hasCast('other_schemaless_attributes', 'array')); + } else { + $this->assertTrue($this->testModel->hasCast('other_schemaless_attributes', 'array')); + } } /** @test */ From 1d43e37958950020c8d364dc2df1ae3a6fec25ba Mon Sep 17 00:00:00 2001 From: davitbek Date: Fri, 28 Aug 2020 01:58:48 +0400 Subject: [PATCH 12/13] remove unused class namespace --- src/SchemalessAttributesTrait.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/SchemalessAttributesTrait.php b/src/SchemalessAttributesTrait.php index c713ae3..7b581c3 100644 --- a/src/SchemalessAttributesTrait.php +++ b/src/SchemalessAttributesTrait.php @@ -3,7 +3,6 @@ namespace Spatie\SchemalessAttributes; use Illuminate\Support\Collection; -use Illuminate\Support\Str; /** * @property array $schemalessAttributes From 661527c1aeec2fd18a6e7c8bd1a9da2289db3cd5 Mon Sep 17 00:00:00 2001 From: Freek Van der Herten Date: Fri, 28 Aug 2020 10:21:56 +0200 Subject: [PATCH 13/13] Update SchemalessAttributesTrait.php --- src/SchemalessAttributesTrait.php | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/SchemalessAttributesTrait.php b/src/SchemalessAttributesTrait.php index 7b581c3..c935554 100644 --- a/src/SchemalessAttributesTrait.php +++ b/src/SchemalessAttributesTrait.php @@ -18,10 +18,7 @@ public function initializeSchemalessAttributesTrait() } } - /** - * @return array - */ - public function getSchemalessAttributes() + public function getSchemalessAttributes(): array { return isset($this->schemalessAttributes) ? $this->schemalessAttributes : []; }