From 8160ecbe62979accc5c14ca1787e6e1dc73cc08b Mon Sep 17 00:00:00 2001 From: Burak Hamza Date: Tue, 25 Jul 2023 18:52:07 +0200 Subject: [PATCH 1/3] feat: upgrade php 8.2 --- .gitignore | 1 + composer.json | 22 ++++++++++---- src/Mappable.php | 67 +++++++++++++++++++++--------------------- tests/MappableTest.php | 4 +++ 4 files changed, 55 insertions(+), 39 deletions(-) diff --git a/.gitignore b/.gitignore index 2c8de41..5c3be21 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ /vendor/ /node_modules/ composer.lock +.phpunit.cache/ \ No newline at end of file diff --git a/composer.json b/composer.json index e647b6e..3a21497 100644 --- a/composer.json +++ b/composer.json @@ -2,6 +2,16 @@ "name": "sofa/eloquence-mappable", "description": "Flexible Searchable, Mappable, Metable, Validation and more extensions for Laravel Eloquent ORM.", "license": "MIT", + "repositories": [ + { + "type": "vcs", + "url": "https://github.com/erasys/eloquence-base" + }, + { + "type": "vcs", + "url": "https://github.com/erasys/hookable" + } + ], "support": { "issues": "https://github.com/jarektkaczyk/eloquence-mappable/issues", "source": "https://github.com/jarektkaczyk/eloquence-mappable" @@ -23,14 +33,14 @@ } ], "require": { - "php": ">=7.1.0", - "sofa/eloquence-base": ">=5.5" + "php": ">=8.2", + "sofa/eloquence-base": "8.0.0" }, "require-dev": { "phpunit/phpunit": "^9.5", - "squizlabs/php_codesniffer": "2.3.3", - "mockery/mockery": "^1.0", - "friendsofphp/php-cs-fixer": "^2.0" + "squizlabs/php_codesniffer": "^3.7", + "mockery/mockery": "^1.5", + "friendsofphp/php-cs-fixer": "^3.18.0" }, "autoload": { "psr-4": { @@ -44,7 +54,7 @@ }, "minimum-stability": "stable", "scripts": { - "test": "phpunit && ./vendor/bin/phpcs src --standard=psr2 --report=diff --colors", + "test": "./vendor/bin/phpunit && ./vendor/bin/phpcs src --standard=psr2 --report=diff --colors", "phpcs": "./vendor/bin/phpcs src --standard=psr2 --report=diff --colors" } } diff --git a/src/Mappable.php b/src/Mappable.php index 47f3611..1b3760a 100644 --- a/src/Mappable.php +++ b/src/Mappable.php @@ -40,7 +40,7 @@ trait Mappable * * @return void */ - public static function bootMappable() + public static function bootMappable() : void { $hooks = new Hooks; @@ -64,7 +64,7 @@ public static function bootMappable() * @param \Sofa\Hookable\Contracts\ArgumentBag $args * @return mixed */ - protected function mappedQuery(Builder $query, $method, ArgumentBag $args) + protected function mappedQuery(Builder $query, $method, ArgumentBag $args) : mixed { $mapping = $this->getMappingForAttribute($args->get('column')); @@ -84,7 +84,7 @@ protected function mappedQuery(Builder $query, $method, ArgumentBag $args) * @param \Sofa\Hookable\Contracts\ArgumentBag $args * @return void */ - protected function mappedSelect(Builder $query, ArgumentBag $args) + protected function mappedSelect(Builder $query, ArgumentBag $args) : void { $columns = $args->get('columns'); @@ -131,7 +131,7 @@ protected function mappedSelect(Builder $query, ArgumentBag $args) * @param string $mapping * @return mixed */ - protected function mappedRelationQuery($query, $method, ArgumentBag $args, $mapping) + protected function mappedRelationQuery($query, $method, ArgumentBag $args, $mapping) : mixed { list($target, $column) = $this->parseMappedColumn($mapping); @@ -152,7 +152,7 @@ protected function mappedRelationQuery($query, $method, ArgumentBag $args, $mapp * @param string $column * @return mixed */ - protected function mappedJoinQuery($query, $method, ArgumentBag $args, $target, $column) + protected function mappedJoinQuery($query, $method, ArgumentBag $args, $target, $column) : mixed { $table = $this->joinMapped($query, $target); @@ -175,7 +175,7 @@ protected function mappedJoinQuery($query, $method, ArgumentBag $args, $target, * @param string $target * @return \Sofa\Eloquence\Builder */ - protected function orderByMapped(Builder $query, ArgumentBag $args, $table, $column, $target) + protected function orderByMapped(Builder $query, ArgumentBag $args, $table, $column, $target) : Builder { $query->with($target)->getQuery()->orderBy("{$table}.{$column}", $args->get('direction')); @@ -190,7 +190,7 @@ protected function orderByMapped(Builder $query, ArgumentBag $args, $table, $col * * @return array */ - protected function listsMapped(Builder $query, ArgumentBag $args, $table, $column) + protected function listsMapped(Builder $query, ArgumentBag $args, $table, $column) : array { return $this->pluckMapped($query, $args, $table, $column); } @@ -202,9 +202,9 @@ protected function listsMapped(Builder $query, ArgumentBag $args, $table, $colum * @param \Sofa\Hookable\Contracts\ArgumentBag $args * @param string $table * @param string $column - * @return array + * @return mixed */ - protected function pluckMapped(Builder $query, ArgumentBag $args, $table, $column) + protected function pluckMapped(Builder $query, ArgumentBag $args, $table, $column) : mixed { $query->select("{$table}.{$column}"); @@ -224,7 +224,7 @@ protected function pluckMapped(Builder $query, ArgumentBag $args, $table, $colum * @param string $key * @return \Sofa\Eloquence\Builder */ - protected function mappedSelectListsKey(Builder $query, $key) + protected function mappedSelectListsKey(Builder $query, $key) : Builder { if ($this->hasColumn($key)) { return $query->addSelect($this->getTable() . '.' . $key); @@ -240,7 +240,7 @@ protected function mappedSelectListsKey(Builder $query, $key) * @param string $target * @return string */ - protected function joinMapped(Builder $query, $target) + protected function joinMapped(Builder $query, $target) : string { $query->prefixColumnsForJoin(); @@ -261,7 +261,7 @@ protected function joinMapped(Builder $query, $target) * @param \Illuminate\Database\Eloquent\Model $parent * @return array */ - protected function joinSegment(Builder $query, $segment, EloquentModel $parent) + protected function joinSegment(Builder $query, $segment, EloquentModel $parent) : array { $relation = $parent->{$segment}(); $related = $relation->getRelated(); @@ -296,7 +296,7 @@ protected function joinSegment(Builder $query, $segment, EloquentModel $parent) * @param string $table * @return bool */ - protected function alreadyJoined(Builder $query, $table) + protected function alreadyJoined(Builder $query, $table) : bool { $joined = Arr::pluck((array) $query->getQuery()->joins, 'table'); @@ -311,7 +311,7 @@ protected function alreadyJoined(Builder $query, $table) * * @throws \LogicException */ - protected function getJoinKeys(Relation $relation) + protected function getJoinKeys(Relation $relation) : array { if ($relation instanceof HasOne || $relation instanceof MorphOne) { return [$relation->getQualifiedForeignKeyName(), $relation->getQualifiedParentKeyName()]; @@ -336,7 +336,7 @@ protected function getJoinKeys(Relation $relation) * @param string $qualifiedColumn * @return mixed */ - protected function mappedSingleResult(Builder $query, $method, $qualifiedColumn) + protected function mappedSingleResult(Builder $query, $method, $qualifiedColumn) : mixed { return $query->getQuery()->select("{$qualifiedColumn}")->{$method}("{$qualifiedColumn}"); } @@ -351,7 +351,7 @@ protected function mappedSingleResult(Builder $query, $method, $qualifiedColumn) * @param string $column * @return \Sofa\Eloquence\Builder */ - protected function mappedHasQuery(Builder $query, $method, ArgumentBag $args, $target, $column) + protected function mappedHasQuery(Builder $query, $method, ArgumentBag $args, $target, $column) : Builder { $boolean = $this->getMappedBoolean($args); @@ -371,7 +371,7 @@ protected function mappedHasQuery(Builder $query, $method, ArgumentBag $args, $t * @param \Sofa\Hookable\Contracts\ArgumentBag $args * @return \Closure */ - protected function getMappedWhereConstraint($method, ArgumentBag $args) + protected function getMappedWhereConstraint($method, ArgumentBag $args) : \Closure { return function ($query) use ($method, $args) { call_user_func_array([$query, $method], $args->all()); @@ -384,7 +384,7 @@ protected function getMappedWhereConstraint($method, ArgumentBag $args) * @param \Sofa\EloquenceArgumentBag $args * @return string */ - protected function getMappedBoolean(ArgumentBag $args) + protected function getMappedBoolean(ArgumentBag $args) : string { $boolean = $args->get('boolean'); @@ -400,7 +400,7 @@ protected function getMappedBoolean(ArgumentBag $args) * @param \Sofa\Hookable\Contracts\ArgumentBag $args * @return string */ - protected function getMappedOperator($method, ArgumentBag $args) + protected function getMappedOperator($method, ArgumentBag $args) : string { if ($not = $args->get('not')) { $args->set('not', false); @@ -419,11 +419,12 @@ protected function getMappedOperator($method, ArgumentBag $args) * @param string $key * @return string|null */ - public function getMappingForAttribute($key) + public function getMappingForAttribute($key) : ?string { if ($this->hasMapping($key)) { return $this->mappedAttributes[$key]; } + return null; } /** @@ -432,7 +433,7 @@ public function getMappingForAttribute($key) * @param string $mapping * @return bool */ - protected function relationMapping($mapping) + protected function relationMapping($mapping) : bool { return strpos($mapping, '.') !== false; } @@ -443,7 +444,7 @@ protected function relationMapping($mapping) * @param string $key * @return bool */ - public function hasMapping($key) + public function hasMapping($key) : bool { if (is_null($this->mappedAttributes)) { $this->parseMappings(); @@ -457,7 +458,7 @@ public function hasMapping($key) * * @return void */ - protected function parseMappings() + protected function parseMappings() : void { $this->mappedAttributes = []; @@ -477,7 +478,7 @@ protected function parseMappings() * @param string $target * @return void */ - protected function parseImplicitMapping($attributes, $target) + protected function parseImplicitMapping($attributes, $target) : void { foreach ($attributes as $attribute) { $this->mappedAttributes[$attribute] = "{$target}.{$attribute}"; @@ -490,7 +491,7 @@ protected function parseImplicitMapping($attributes, $target) * @param string $key * @return mixed */ - protected function mapAttribute($key) + protected function mapAttribute($key) : mixed { $segments = explode('.', $this->getMappingForAttribute($key)); @@ -504,11 +505,11 @@ protected function mapAttribute($key) * @param array $segments * @return mixed */ - protected function getTarget($target, array $segments) + protected function getTarget($target, array $segments) : mixed { foreach ($segments as $segment) { if (!$target) { - return; + return null; } $target = $target->{$segment}; @@ -523,7 +524,7 @@ protected function getTarget($target, array $segments) * @param string $key * @param mixed $value */ - protected function setMappedAttribute($key, $value) + protected function setMappedAttribute($key, $value) : void { $segments = explode('.', $this->getMappingForAttribute($key)); @@ -541,7 +542,7 @@ protected function setMappedAttribute($key, $value) * * @param \Illuminate\Database\Eloquent\Model $target */ - protected function addTargetToSave($target) + protected function addTargetToSave($target) : void { if ($this !== $target) { $this->targetsToSave[] = $target; @@ -553,7 +554,7 @@ protected function addTargetToSave($target) * * @return void */ - protected function saveMapped() + protected function saveMapped() : void { foreach (array_unique($this->targetsToSave) as $target) { $target->save(); @@ -568,7 +569,7 @@ protected function saveMapped() * @param string $key * @return void */ - protected function forget($key) + protected function forget($key) : void { $mapping = $this->getMappingForAttribute($key); @@ -587,7 +588,7 @@ protected function forget($key) * * @inheritdoc */ - protected function mutateAttributeForArray($key, $value) + protected function mutateAttributeForArray($key, $value) : mixed { if ($this->hasMapping($key)) { $value = $this->mapAttribute($key); @@ -603,7 +604,7 @@ protected function mutateAttributeForArray($key, $value) * * @return array */ - public function getMaps() + public function getMaps() : array { return (property_exists($this, 'maps')) ? $this->maps : []; } diff --git a/tests/MappableTest.php b/tests/MappableTest.php index 6294112..1803108 100644 --- a/tests/MappableTest.php +++ b/tests/MappableTest.php @@ -403,6 +403,10 @@ class MappableStub saveMapped as protectedSaveMapped; } + public $bar; + + public $related; + protected $maps = [ // local alias 'alias' => 'original', // $this->original From 9b4ba08bb34b6f3b4af777b27f8e5cbd217f3f7d Mon Sep 17 00:00:00 2001 From: PooyaRaki Date: Mon, 31 Jul 2023 15:45:05 +0200 Subject: [PATCH 2/3] fix: unknown column is fixed --- src/Mappable.php | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/src/Mappable.php b/src/Mappable.php index 99e2f6d..e4f9708 100644 --- a/src/Mappable.php +++ b/src/Mappable.php @@ -162,6 +162,10 @@ protected function mappedRelationQuery($query, $method, ArgumentBag $args, $mapp return $this->mappedJoinQuery($query, $method, $args, $target, $column); } + if (in_array($method, ['whereNull', 'whereNotNull'])) { + return $this->mappedWhereQuery($query, $method, $args, $target, $column); + } + return $this->mappedHasQuery($query, $method, $args, $target, $column); } @@ -386,6 +390,28 @@ protected function mappedHasQuery(Builder $query, $method, ArgumentBag $args, $t ->has($target, $operator, 1, $boolean, $this->getMappedWhereConstraint($method, $args)) ->with($target); } + /** + * Add whereNull/WhereNotNull subquery on the mapped attribute relation. + * + * @param \Sofa\Eloquence\Builder $query + * @param string $method + * @param \Sofa\Hookable\Contracts\ArgumentBag $args + * @param string $target + * @param string $column + * @return \Sofa\Eloquence\Builder + */ + protected function mappedWhereQuery(Builder $query, $method, ArgumentBag $args, $target, $column) : Builder + { + $boolean = $this->getMappedBoolean($args); + $operator = $this->getMappedOperator($method, $args); + + $args->set('columns', $column); + $args->unset('column'); + + return $query + ->has($target, $operator, 1, $boolean, $this->getMappedWhereConstraint($method, $args)) + ->with($target); + } /** * Get the relation constraint closure. @@ -458,7 +484,7 @@ public function getMappingForAttribute($key) : ?string */ protected function relationMapping($mapping) : bool { - return strpos($mapping, '.') !== false; + return strpos((string)$mapping, '.') !== false; } /** From cc84b554352bad50177dd30ce59609c8ce133063 Mon Sep 17 00:00:00 2001 From: PooyaRaki Date: Wed, 2 Aug 2023 12:41:55 +0200 Subject: [PATCH 3/3] chore: updated dependency versions --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index b51f28c..a770f21 100644 --- a/composer.json +++ b/composer.json @@ -44,7 +44,7 @@ ], "require": { "php": ">=8.2", - "sofa/eloquence-base": "8.0.0" + "sofa/eloquence-base": "8.0.1" }, "require-dev": { "phpunit/phpunit": "^9.5",