From 8bb9feb469176a19e20191c49f2f36d2d23cb88a Mon Sep 17 00:00:00 2001 From: Bader Date: Mon, 30 Mar 2020 17:50:42 +0300 Subject: [PATCH] addd row dynamically --- composer.json | 5 +++-- src/Relationships/HasManyCached.php | 8 ++++++++ src/Traits/Kabsa.php | 30 ++++++++++++++++++++++++----- tests/KabsaTest.php | 3 ++- 4 files changed, 38 insertions(+), 8 deletions(-) diff --git a/composer.json b/composer.json index 73bce6d..0b39d7f 100755 --- a/composer.json +++ b/composer.json @@ -23,10 +23,11 @@ }, "require-dev": { "doctrine/dbal": "^2.5", - "phpunit/phpunit": "^8.3", "fzaninotto/faker": "^1.6", + "guzzlehttp/guzzle": "^6.5", "mockery/mockery": "^1.2", - "orchestra/testbench": "^3.5 || ^3.6 || ^3.7 || ^3.8 || ^4.0 || ^5.0" + "orchestra/testbench": "^3.5 || ^3.6 || ^3.7 || ^3.8 || ^4.0 || ^5.0", + "phpunit/phpunit": "^8.3" }, "autoload": { "psr-4": { diff --git a/src/Relationships/HasManyCached.php b/src/Relationships/HasManyCached.php index 82873e1..4082394 100644 --- a/src/Relationships/HasManyCached.php +++ b/src/Relationships/HasManyCached.php @@ -26,4 +26,12 @@ public function getResults() return $this->related::where($this->foreignKey, $this->parent->{$this->ownerKey}); } + + public function initRelation(array $models, $relation) + { + } + + public function match(array $models, Collection $results, $relation) + { + } } diff --git a/src/Traits/Kabsa.php b/src/Traits/Kabsa.php index a413506..2187888 100755 --- a/src/Traits/Kabsa.php +++ b/src/Traits/Kabsa.php @@ -7,8 +7,16 @@ trait Kabsa { + /** + * @var Collection + */ protected static $kabsaCollection; - + + public static function bootKabsa() + { + self::unguard(); + } + public function getRows() { return $this->rows; @@ -19,11 +27,23 @@ public static function all($columns = []) if(!empty(static::$kabsaCollection)) { return static::$kabsaCollection; } - - self::unguard(); - $self = new self(); - return static::$kabsaCollection = Collection::make($self->getRows() ?? [])->map(function ($row) { return new self($row); }); + return static::$kabsaCollection = Collection::make( + (new static)->getRows() ?? []) + ->map(function ($row) { + return new static($row); + }); + } + + public static function addRow($row) + { + if(static::$kabsaCollection instanceof Collection) { + static::$kabsaCollection->push(new static($row)); + } else { + static::$kabsaCollection = Collection::make([new static($row)]); + } + + return true; } public function __call($method, $parameters) diff --git a/tests/KabsaTest.php b/tests/KabsaTest.php index e62d832..4dff3ab 100755 --- a/tests/KabsaTest.php +++ b/tests/KabsaTest.php @@ -7,6 +7,7 @@ use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\DB; +use Illuminate\Support\Facades\Http; use Illuminate\Support\Facades\Redis; use Orchestra\Testbench\TestCase; @@ -165,4 +166,4 @@ public function role() { return $this->belongsToKabsaRow(Role::class, 'label', 'role_label'); } -} \ No newline at end of file +}