-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Valentin SALMERON
committed
Jun 24, 2024
1 parent
ca3319c
commit d38e933
Showing
8 changed files
with
341 additions
and
60 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 |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\ORM\Persisters\Traits; | ||
|
||
use BackedEnum; | ||
use Doctrine\ORM\EntityManagerInterface; | ||
use Doctrine\ORM\Proxy\DefaultProxyClassNameResolver; | ||
|
||
use function array_merge; | ||
use function is_array; | ||
use function is_object; | ||
|
||
trait ResolveValuesHelper | ||
{ | ||
protected EntityManagerInterface $em; | ||
|
||
/** | ||
* Retrieves the parameters that identifies a value. | ||
* | ||
* @return mixed[] | ||
*/ | ||
private function getValues(mixed $value): array | ||
{ | ||
if (is_array($value)) { | ||
$newValue = []; | ||
|
||
foreach ($value as $itemValue) { | ||
$newValue = array_merge($newValue, $this->getValues($itemValue)); | ||
} | ||
|
||
return [$newValue]; | ||
} | ||
|
||
return $this->getIndividualValue($value); | ||
} | ||
|
||
/** | ||
* Retrieves an individual parameter value. | ||
* | ||
* @psalm-return list<mixed> | ||
Check failure on line 42 in src/Persisters/Traits/ResolveValuesHelper.php GitHub Actions / Static Analysis with Psalm (3.8.2)MoreSpecificReturnType
Check failure on line 42 in src/Persisters/Traits/ResolveValuesHelper.php GitHub Actions / Static Analysis with Psalm (default)MoreSpecificReturnType
|
||
*/ | ||
private function getIndividualValue(mixed $value): array | ||
{ | ||
if (! is_object($value)) { | ||
return [$value]; | ||
} | ||
|
||
if ($value instanceof BackedEnum) { | ||
return [$value->value]; | ||
} | ||
|
||
$valueClass = DefaultProxyClassNameResolver::getClass($value); | ||
|
||
if ($this->em->getMetadataFactory()->isTransient($valueClass)) { | ||
return [$value]; | ||
} | ||
|
||
$class = $this->em->getClassMetadata($valueClass); | ||
|
||
if ($class->isIdentifierComposite) { | ||
$newValue = []; | ||
|
||
foreach ($class->getIdentifierValues($value) as $innerValue) { | ||
$newValue = array_merge($newValue, $this->getValues($innerValue)); | ||
} | ||
|
||
return $newValue; | ||
} | ||
|
||
return [$this->em->getUnitOfWork()->getSingleIdentifierValue($value)]; | ||
} | ||
} |
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,52 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\Tests\Models\Enums; | ||
|
||
use Doctrine\Common\Collections\ArrayCollection; | ||
use Doctrine\Common\Collections\Collection; | ||
use Doctrine\ORM\Mapping\Column; | ||
use Doctrine\ORM\Mapping\Entity; | ||
use Doctrine\ORM\Mapping\GeneratedValue; | ||
use Doctrine\ORM\Mapping\Id; | ||
use Doctrine\ORM\Mapping\JoinColumn; | ||
use Doctrine\ORM\Mapping\ManyToMany; | ||
use Doctrine\ORM\Mapping\ManyToOne; | ||
use Doctrine\ORM\Mapping\Table; | ||
|
||
#[Entity] | ||
#[Table(name: 'books')] | ||
class Book | ||
{ | ||
#[Id] | ||
#[GeneratedValue] | ||
#[Column] | ||
public int $id; | ||
|
||
#[ManyToOne(targetEntity: Library::class, inversedBy: 'books')] | ||
#[JoinColumn(name: 'library_id', referencedColumnName: 'id')] | ||
public Library $library; | ||
|
||
#[Column(enumType: BookColor::class)] | ||
public BookColor $bookColor; | ||
|
||
#[ManyToMany(targetEntity: BookCategory::class, mappedBy: 'books')] | ||
public Collection $categories; | ||
|
||
public function __construct() | ||
{ | ||
$this->categories = new ArrayCollection(); | ||
} | ||
|
||
public function setLibrary(Library $library): void | ||
{ | ||
$this->library = $library; | ||
} | ||
|
||
public function addCategory(BookCategory $bookCategory): void | ||
{ | ||
$this->categories->add($bookCategory); | ||
$bookCategory->addBook($this); | ||
} | ||
} |
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,52 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\Tests\Models\Enums; | ||
|
||
use Doctrine\Common\Collections\ArrayCollection; | ||
use Doctrine\Common\Collections\Collection; | ||
use Doctrine\Common\Collections\Criteria; | ||
use Doctrine\ORM\Mapping\Column; | ||
use Doctrine\ORM\Mapping\Entity; | ||
use Doctrine\ORM\Mapping\GeneratedValue; | ||
use Doctrine\ORM\Mapping\Id; | ||
use Doctrine\ORM\Mapping\ManyToMany; | ||
|
||
#[Entity] | ||
class BookCategory | ||
{ | ||
#[Id] | ||
#[Column] | ||
#[GeneratedValue] | ||
public int $id; | ||
|
||
#[Column] | ||
public string $name; | ||
|
||
#[ManyToMany(targetEntity: Book::class, inversedBy: 'categories')] | ||
public Collection $books; | ||
|
||
public function __construct() | ||
{ | ||
$this->books = new ArrayCollection(); | ||
} | ||
|
||
public function addBook(Book $book): void | ||
{ | ||
$this->books->add($book); | ||
} | ||
|
||
public function getBooks(): Collection | ||
{ | ||
return $this->books; | ||
} | ||
|
||
public function getBooksWithColor(BookColor $bookColor): Collection | ||
{ | ||
$criteria = Criteria::create() | ||
->andWhere(Criteria::expr()->eq('bookColor', $bookColor)); | ||
|
||
return $this->books->matching($criteria); | ||
} | ||
} |
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,11 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\Tests\Models\Enums; | ||
|
||
enum BookColor: string | ||
{ | ||
case RED = 'red'; | ||
case BLUE = 'blue'; | ||
} |
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,50 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\Tests\Models\Enums; | ||
|
||
use Doctrine\Common\Collections\ArrayCollection; | ||
use Doctrine\Common\Collections\Collection; | ||
use Doctrine\Common\Collections\Criteria; | ||
use Doctrine\ORM\Mapping\Column; | ||
use Doctrine\ORM\Mapping\Entity; | ||
use Doctrine\ORM\Mapping\GeneratedValue; | ||
use Doctrine\ORM\Mapping\Id; | ||
use Doctrine\ORM\Mapping\OneToMany; | ||
|
||
#[Entity] | ||
class Library | ||
{ | ||
#[Id] | ||
#[GeneratedValue] | ||
#[Column] | ||
public int $id; | ||
|
||
#[OneToMany(targetEntity: Book::class, mappedBy: 'library')] | ||
public Collection $books; | ||
|
||
public function __construct() | ||
{ | ||
$this->books = new ArrayCollection(); | ||
} | ||
|
||
public function getBooksWithColor(BookColor $bookColor): Collection | ||
{ | ||
$criteria = Criteria::create() | ||
->andWhere(Criteria::expr()->eq('bookColor', $bookColor)); | ||
|
||
return $this->books->matching($criteria); | ||
} | ||
|
||
public function getBooks(): Collection | ||
{ | ||
return $this->books; | ||
} | ||
|
||
public function addBook(Book $book): void | ||
{ | ||
$this->books->add($book); | ||
$book->setLibrary($this); | ||
} | ||
} |
Oops, something went wrong.