forked from doctrine/orm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 2.11.x: Leverage generic ObjectManagerDecorator (doctrine#9312) Fix WhereInWalker description to better describe the behaviour of this class (doctrine#9268) Regenerate Psalm baseline Update Psalm baseline for Persistence 2.3 (doctrine#9349) Support readonly properties for read operations (doctrine#9316)
- Loading branch information
Showing
12 changed files
with
385 additions
and
40 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,51 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\ORM\Mapping; | ||
|
||
use InvalidArgumentException; | ||
use LogicException; | ||
use ReflectionProperty; | ||
|
||
use function assert; | ||
use function func_get_args; | ||
use function func_num_args; | ||
use function is_object; | ||
use function sprintf; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
final class ReflectionReadonlyProperty extends ReflectionProperty | ||
{ | ||
public function __construct( | ||
private ReflectionProperty $wrappedProperty | ||
) { | ||
if (! $wrappedProperty->isReadOnly()) { | ||
throw new InvalidArgumentException('Given property is not readonly.'); | ||
} | ||
|
||
parent::__construct($wrappedProperty->class, $wrappedProperty->name); | ||
} | ||
|
||
public function getValue(?object $object = null): mixed | ||
{ | ||
return $this->wrappedProperty->getValue(...func_get_args()); | ||
} | ||
|
||
public function setValue(mixed $objectOrValue, mixed $value = null): void | ||
{ | ||
if (func_num_args() < 2 || $objectOrValue === null || ! $this->isInitialized($objectOrValue)) { | ||
$this->wrappedProperty->setValue(...func_get_args()); | ||
|
||
return; | ||
} | ||
|
||
assert(is_object($objectOrValue)); | ||
|
||
if (parent::getValue($objectOrValue) !== $value) { | ||
throw new LogicException(sprintf('Attempting to change readonly property %s::$%s.', $this->class, $this->name)); | ||
} | ||
} | ||
} |
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
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,31 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\Tests\Models\ReadonlyProperties; | ||
|
||
use Doctrine\ORM\Mapping\Column; | ||
use Doctrine\ORM\Mapping\Entity; | ||
use Doctrine\ORM\Mapping\GeneratedValue; | ||
use Doctrine\ORM\Mapping\Id; | ||
use Doctrine\ORM\Mapping\Table; | ||
|
||
#[Entity, Table(name: 'author')] | ||
class Author | ||
{ | ||
#[Column, Id, GeneratedValue] | ||
private readonly int $id; | ||
|
||
#[Column] | ||
private readonly string $name; | ||
|
||
public function getId(): int | ||
{ | ||
return $this->id; | ||
} | ||
|
||
public function getName(): string | ||
{ | ||
return $this->name; | ||
} | ||
} |
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,51 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\Tests\Models\ReadonlyProperties; | ||
|
||
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\JoinTable; | ||
use Doctrine\ORM\Mapping\ManyToMany; | ||
use Doctrine\ORM\Mapping\Table; | ||
|
||
#[Entity, Table(name: 'book')] | ||
class Book | ||
{ | ||
#[Column, Id, GeneratedValue] | ||
private readonly int $id; | ||
|
||
#[Column] | ||
private readonly string $title; | ||
|
||
#[ManyToMany(targetEntity: Author::class), JoinTable(name: 'book_author')] | ||
private readonly Collection $authors; | ||
|
||
public function __construct() | ||
{ | ||
$this->authors = new ArrayCollection(); | ||
} | ||
|
||
public function getId(): int | ||
{ | ||
return $this->id; | ||
} | ||
|
||
public function getTitle(): string | ||
{ | ||
return $this->title; | ||
} | ||
|
||
/** | ||
* @return list<Author> | ||
*/ | ||
public function getAuthors(): array | ||
{ | ||
return $this->authors->getValues(); | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
tests/Doctrine/Tests/Models/ReadonlyProperties/SimpleBook.php
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,41 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\Tests\Models\ReadonlyProperties; | ||
|
||
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\ManyToOne; | ||
use Doctrine\ORM\Mapping\Table; | ||
|
||
#[Entity, Table(name: 'simple_book')] | ||
class SimpleBook | ||
{ | ||
#[Column, Id, GeneratedValue] | ||
private readonly int $id; | ||
|
||
#[Column] | ||
private readonly string $title; | ||
|
||
#[ManyToOne, JoinColumn(nullable: false)] | ||
private readonly Author $author; | ||
|
||
public function getId(): int | ||
{ | ||
return $this->id; | ||
} | ||
|
||
public function getTitle(): string | ||
{ | ||
return $this->title; | ||
} | ||
|
||
public function getAuthor(): Author | ||
{ | ||
return $this->author; | ||
} | ||
} |
Oops, something went wrong.