-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- `Collection::find()`/`filter()` accept mixed `$specification` - `DoctrineBridgeCollection::find()`/`filter()` can accept `Criteria` instance as `$specification` - `DoctrineBridgeCollection::find()`/`filter()` can accept "specification objects" - `EntityResult::find()`/`filter()` can accept `Criteria` instance as `$specification` - `EntityResult::find()`/`filter()` can accept "specification objects" - `Matchable` interface - doctrine/orm specification interpreter
- Loading branch information
Showing
48 changed files
with
2,177 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ | |
namespace Zenstruck\Collection; | ||
|
||
use Zenstruck\Collection; | ||
use Zenstruck\Collection\Exception\InvalidSpecification; | ||
|
||
/** | ||
* @author Kevin Bond <[email protected]> | ||
|
@@ -179,13 +180,17 @@ public function merge(iterable ...$with): self | |
} | ||
|
||
/** | ||
* @param null|callable(V,K):bool $predicate | ||
* @param null|callable(V,K):bool $specification | ||
* | ||
* @return self<K,V> | ||
*/ | ||
public function filter(?callable $predicate = null): self | ||
public function filter(mixed $specification = null): self | ||
{ | ||
return new self(\array_filter($this->source, $predicate, \ARRAY_FILTER_USE_BOTH)); | ||
if (null !== $specification && !\is_callable($specification)) { | ||
throw InvalidSpecification::build($specification, self::class, 'filter', 'Only null|callable(V,K):bool is supported.'); | ||
} | ||
|
||
return new self(\array_filter($this->source, $specification, \ARRAY_FILTER_USE_BOTH)); | ||
} | ||
|
||
/** | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,9 +14,13 @@ | |
use Doctrine\Common\Collections\AbstractLazyCollection; | ||
use Doctrine\Common\Collections\ArrayCollection as DoctrineArrayCollection; | ||
use Doctrine\Common\Collections\Collection as DoctrineCollection; | ||
use Doctrine\Common\Collections\Criteria; | ||
use Doctrine\Common\Collections\Selectable; | ||
use Zenstruck\Collection; | ||
use Zenstruck\Collection\ArrayCollection; | ||
use Zenstruck\Collection\Doctrine\Specification\CriteriaInterpreter; | ||
use Zenstruck\Collection\IterableCollection; | ||
use Zenstruck\Collection\Matchable; | ||
|
||
/** | ||
* @author Kevin Bond <[email protected]> | ||
|
@@ -25,13 +29,15 @@ | |
* @template V | ||
* @implements Collection<K,V> | ||
* @implements DoctrineCollection<K,V> | ||
* @implements Matchable<K,V> | ||
*/ | ||
final class DoctrineBridgeCollection implements Collection, DoctrineCollection | ||
final class DoctrineBridgeCollection implements Collection, DoctrineCollection, Matchable | ||
{ | ||
/** @use IterableCollection<K,V> */ | ||
use IterableCollection { | ||
map as private innerMap; | ||
reduce as private innerReduce; | ||
find as private innerFind; | ||
} | ||
|
||
/** @var DoctrineCollection<K,V> */ | ||
|
@@ -72,11 +78,41 @@ public function findFirst(\Closure $p): mixed | |
} | ||
|
||
/** | ||
* @param Criteria|callable(V,K):bool $specification | ||
*/ | ||
public function find(mixed $specification, mixed $default = null): mixed | ||
{ | ||
if ($specification instanceof Criteria) { | ||
return $this->filter($specification->setMaxResults(1))->first($default); | ||
} | ||
|
||
if (!\is_callable($specification)) { | ||
return $this->find(CriteriaInterpreter::interpret($specification, self::class, 'find'), $default); | ||
} | ||
|
||
return $this->innerFind($specification, $default); | ||
} | ||
|
||
/** | ||
* @param Criteria|callable(V,K):bool $specification | ||
* | ||
* @return self<K,V> | ||
*/ | ||
public function filter(\Closure|callable $p): self | ||
public function filter(mixed $specification): self | ||
{ | ||
return new self($this->inner->filter($p(...))); | ||
if ($this->inner instanceof Selectable && $specification instanceof Criteria) { | ||
return new self($this->inner->matching($specification)); | ||
} | ||
|
||
if ($this->inner instanceof Criteria) { | ||
throw new \LogicException(\sprintf('"%s" is not an instance of "%s". Cannot use Criteria as a specification.', $this->inner::class, Selectable::class)); | ||
} | ||
|
||
if (!\is_callable($specification)) { | ||
return $this->filter(CriteriaInterpreter::interpret($specification, self::class, 'filter')); | ||
} | ||
|
||
return new self($this->inner->filter($specification(...))); | ||
} | ||
|
||
public function reduce(\Closure|callable $function, mixed $initial = null): mixed | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the zenstruck/collection package. | ||
* | ||
* (c) Kevin Bond <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Zenstruck\Collection\Doctrine; | ||
|
||
use Zenstruck\Collection\Doctrine\ORM\Specification\AntiJoin; | ||
use Zenstruck\Collection\Doctrine\ORM\Specification\Join; | ||
use Zenstruck\Collection\Doctrine\Specification\Cache; | ||
use Zenstruck\Collection\Doctrine\Specification\Delete; | ||
use Zenstruck\Collection\Doctrine\Specification\Instance; | ||
use Zenstruck\Collection\Doctrine\Specification\Unwritable; | ||
use Zenstruck\Collection\Spec; | ||
|
||
/** | ||
* @author Kevin Bond <[email protected]> | ||
*/ | ||
final class DoctrineSpec extends Spec | ||
{ | ||
public static function readonly(): Unwritable | ||
{ | ||
return new Unwritable(); | ||
} | ||
|
||
public static function delete(): Delete | ||
{ | ||
return new Delete(); | ||
} | ||
|
||
public static function cache(?int $lifetime = null, ?string $key = null): Cache | ||
{ | ||
return new Cache($lifetime, $key); | ||
} | ||
|
||
/** | ||
* @param class-string $class | ||
*/ | ||
public static function instanceOf(string $class): Instance | ||
{ | ||
return new Instance($class); | ||
} | ||
|
||
public static function innerJoin(string $field, ?string $alias = null): Join | ||
{ | ||
return Join::inner($field, $alias); | ||
} | ||
|
||
public static function leftJoin(string $field, ?string $alias = null): Join | ||
{ | ||
return Join::left($field, $alias); | ||
} | ||
|
||
public static function antiJoin(string $field): AntiJoin | ||
{ | ||
return new AntiJoin($field); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.