-
-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #509: change behavior of EntityFactory
- Loading branch information
Showing
18 changed files
with
1,992 additions
and
1,046 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
Large diffs are not rendered by default.
Oops, something went wrong.
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
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,54 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Cycle\ORM\Service\Implementation; | ||
|
||
use Cycle\ORM\EntityProxyInterface; | ||
use Cycle\ORM\Exception\ORMException; | ||
use Cycle\ORM\Heap\HeapInterface; | ||
use Cycle\ORM\SchemaInterface; | ||
use Cycle\ORM\Service\RoleResolverInterface; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
final class RoleResolver implements RoleResolverInterface | ||
{ | ||
private SchemaInterface $schema; | ||
private HeapInterface $heap; | ||
|
||
public function __construct(SchemaInterface $schema, HeapInterface $heap) | ||
{ | ||
$this->schema = $schema; | ||
$this->heap = $heap; | ||
} | ||
|
||
public function resolveRole(object|string $entity): string | ||
{ | ||
if (\is_object($entity)) { | ||
$node = $this->heap->get($entity); | ||
if ($node !== null) { | ||
return $node->getRole(); | ||
} | ||
|
||
/** @var class-string $class */ | ||
$class = $entity::class; | ||
if (!$this->schema->defines($class)) { | ||
$parentClass = \get_parent_class($entity); | ||
|
||
if ($parentClass === false | ||
|| !$entity instanceof EntityProxyInterface | ||
|| !$this->schema->defines($parentClass) | ||
) { | ||
throw new ORMException("Unable to resolve role of `$class`."); | ||
} | ||
$class = $parentClass; | ||
} | ||
|
||
$entity = $class; | ||
} | ||
|
||
return $this->schema->resolveAlias($entity) ?? throw new ORMException("Unable to resolve role `$entity`."); | ||
} | ||
} |
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,15 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Cycle\ORM\Service; | ||
|
||
interface RoleResolverInterface | ||
{ | ||
/** | ||
* Automatically resolve role based on object name or instance. | ||
* | ||
* @return non-empty-string | ||
*/ | ||
public function resolveRole(string|object $entity): string; | ||
} |
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 |
---|---|---|
|
@@ -259,11 +259,13 @@ public function testRepositoryFindOneWithWhere(): void | |
public function testLoadOverwriteValues(): void | ||
{ | ||
$u = $this->orm->getRepository('user')->findByPK(1); | ||
$this->assertSame('[email protected]', $u->email); | ||
$u->email = '[email protected]'; | ||
$this->assertSame('[email protected]', $u->email); | ||
|
||
$u2 = $this->orm->getRepository('user')->findByPK(1); | ||
$this->assertSame('[email protected]', $u2->email); | ||
self::assertSame($u, $u2); | ||
$this->assertSame('[email protected]', $u2->email); | ||
|
||
$u3 = $this->orm->withHeap(new Heap())->getRepository('user')->findByPK(1); | ||
$this->assertSame('[email protected]', $u3->email); | ||
|
@@ -272,10 +274,10 @@ public function testLoadOverwriteValues(): void | |
$t = new Transaction($this->orm); | ||
$t->persist($u); | ||
$t->run(); | ||
$this->assertNumWrites(0); | ||
$this->assertNumWrites(1); | ||
|
||
$u4 = $this->orm->withHeap(new Heap())->getRepository('user')->findByPK(1); | ||
$this->assertSame('hello@world.com', $u4->email); | ||
$this->assertSame('test@email.com', $u4->email); | ||
} | ||
|
||
public function testNullableValuesInASndOut(): void | ||
|
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 |
---|---|---|
|
@@ -328,19 +328,17 @@ public function testLoadOverwriteValues(): void | |
$this->assertSame('[email protected]', $u->email); | ||
|
||
$u2 = $this->orm->getRepository(User::class)->findByPK(1); | ||
$this->assertSame('hello@world.com', $u2->email); | ||
$this->assertSame('test@email.com', $u2->email); | ||
|
||
$u3 = $this->orm->withHeap(new Heap())->getRepository(User::class)->findByPK(1); | ||
$this->assertSame('[email protected]', $u3->email); | ||
|
||
$this->captureWriteQueries(); | ||
$t = new Transaction($this->orm); | ||
$t->persist($u); | ||
$t->run(); | ||
$this->assertNumWrites(0); | ||
$this->save($u); | ||
$this->assertNumWrites(1); | ||
|
||
$u4 = $this->orm->withHeap(new Heap())->getRepository(User::class)->findByPK(1); | ||
$this->assertSame('hello@world.com', $u4->email); | ||
$this->assertSame('test@email.com', $u4->email); | ||
} | ||
|
||
public function testNullableValuesInASndOut(): void | ||
|
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.