Skip to content

Commit 48300ea

Browse files
committed
Add GeneratedField class; cleanup
1 parent ec0df10 commit 48300ea

File tree

10 files changed

+66
-32
lines changed

10 files changed

+66
-32
lines changed

src/Command/Database/Insert.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Cycle\ORM\Command\Traits\MapperTrait;
1212
use Cycle\ORM\Heap\State;
1313
use Cycle\ORM\MapperInterface;
14+
use Cycle\ORM\Schema\GeneratedField;
1415
use Cycle\ORM\SchemaInterface;
1516

1617
/**
@@ -87,7 +88,7 @@ public function execute(): void
8788
}
8889
// unset db-generated fields if they are null
8990
foreach ($this->generated as $column => $mode) {
90-
if (($mode & SchemaInterface::GENERATED_DB) === 0x0) {
91+
if (($mode & GeneratedField::DB_INSERT) === 0x0) {
9192
continue;
9293
}
9394

@@ -164,7 +165,7 @@ private function hasGeneratedFields(): bool
164165
$data = $this->state->getData();
165166

166167
foreach ($this->generated as $field => $mode) {
167-
if (($mode & (SchemaInterface::GENERATED_DB | SchemaInterface::GENERATED_PHP_INSERT)) === 0x0) {
168+
if (($mode & (GeneratedField::DB_INSERT | GeneratedField::PHP_INSERT)) === 0x0) {
168169
continue;
169170
}
170171

src/Heap/Traits/WaitFieldTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public function waitField(string $key, bool $required = true): void
1818

1919
public function getWaitingFields(bool $requiredOnly = false): array
2020
{
21-
return array_keys($requiredOnly ? array_filter($this->waitingFields) : $this->waitingFields);
21+
return \array_keys($requiredOnly ? \array_filter($this->waitingFields) : $this->waitingFields);
2222
}
2323

2424
/**

src/Relation/BelongsTo.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ private function checkNullValuePossibility(Tuple $tuple): bool
179179
}
180180

181181
if ($tuple->status < Tuple::STATUS_PREPROCESSED
182-
&& array_intersect($this->innerKeys, $tuple->state->getWaitingFields(false)) !== []
182+
&& \array_intersect($this->innerKeys, $tuple->state->getWaitingFields(false)) !== []
183183
) {
184184
return true;
185185
}

src/Relation/RefersTo.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public function queue(Pool $pool, Tuple $tuple): void
9898
if ($rTuple->status === Tuple::STATUS_PROCESSED
9999
|| ($rTuple->status > Tuple::STATUS_PREPARING
100100
&& $rTuple->state->getStatus() !== node::NEW
101-
&& array_intersect($this->outerKeys, $rTuple->state->getWaitingFields()) === [])
101+
&& \array_intersect($this->outerKeys, $rTuple->state->getWaitingFields()) === [])
102102
) {
103103
$this->pullValues($tuple->state, $rTuple->state);
104104
$node->setRelation($this->getName(), $related);

src/Schema/GeneratedField.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Cycle\ORM\Schema;
6+
7+
/**
8+
* @see SchemaInterface::GENERATED_FIELDS
9+
*/
10+
final class GeneratedField
11+
{
12+
/**
13+
* Field value is generated in the user space before transaction running.
14+
*/
15+
public const DEFAULT = 0;
16+
17+
/**
18+
* Field value is generated by the Database.
19+
* It is: autoincrement fields, sequences, timestamps, etc.
20+
*/
21+
public const DB_INSERT = 1;
22+
23+
/**
24+
* Field value is generated by PHP code before Insert command running
25+
* like with `CreatedAt` or `Uuid` Entity Behaviors
26+
*
27+
* @link https://github.com/cycle/entity-behavior
28+
* @link https://github.com/cycle/entity-behavior-uuid
29+
*/
30+
public const PHP_INSERT = 2;
31+
32+
/**
33+
* Field value is generated by PHP code before Update command running like with `UpdatedAt` Entity Behavior.
34+
*
35+
* @link https://github.com/cycle/entity-behavior
36+
*/
37+
public const PHP_UPDATE = 4;
38+
}

src/SchemaInterface.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,6 @@ interface SchemaInterface
3333
public const TYPECAST_HANDLER = 19; // Typecast handler definition that implements TypecastInterface
3434
public const GENERATED_FIELDS = 20; // List of generated fields [field => generating type]
3535

36-
37-
public const GENERATED_DB = 1; // sequences and others
38-
public const GENERATED_PHP_INSERT = 2; // generated by PHP code on insert like uuid
39-
public const GENERATED_PHP_UPDATE = 4; // generated by PHP code on update like time
40-
4136
/**
4237
* Return all roles defined within the schema.
4338
*/

src/Transaction/Runner.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public function complete(): void
7575
{
7676
if ($this->mode === self::MODE_OPEN_TRANSACTION) {
7777
// Commit all of the open and normalized database transactions
78-
foreach (array_reverse($this->drivers) as $driver) {
78+
foreach (\array_reverse($this->drivers) as $driver) {
7979
/** @var DriverInterface $driver */
8080
$driver->commitTransaction();
8181
}
@@ -94,14 +94,14 @@ public function rollback(): void
9494
{
9595
if ($this->mode === self::MODE_OPEN_TRANSACTION) {
9696
// Close all open and normalized database transactions
97-
foreach (array_reverse($this->drivers) as $driver) {
97+
foreach (\array_reverse($this->drivers) as $driver) {
9898
/** @var DriverInterface $driver */
9999
$driver->rollbackTransaction();
100100
}
101101
}
102102

103103
// Close all of external types of transactions (revert changes)
104-
foreach (array_reverse($this->executed) as $command) {
104+
foreach (\array_reverse($this->executed) as $command) {
105105
if ($command instanceof RollbackMethodInterface) {
106106
$command->rollBack();
107107
}
@@ -145,7 +145,7 @@ private function prepareTransaction(DriverInterface $driver): void
145145

146146
if ($this->mode === self::MODE_CONTINUE_TRANSACTION) {
147147
if ($driver->getTransactionLevel() === 0) {
148-
throw new RunnerException(sprintf(
148+
throw new RunnerException(\sprintf(
149149
'The `%s` driver connection has no opened transaction.',
150150
$driver->getType()
151151
));

src/Transaction/UnitOfWork.php

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ private function resolveSlaveRelations(Tuple $tuple, RelationMap $map): int
265265
if (!$map->hasSlaves()) {
266266
return self::RELATIONS_RESOLVED;
267267
}
268-
$changedFields = array_keys($tuple->state->getChanges());
268+
$changedFields = \array_keys($tuple->state->getChanges());
269269

270270
// Attach children to pool
271271
$transactData = $tuple->state->getTransactionData();
@@ -281,15 +281,15 @@ private function resolveSlaveRelations(Tuple $tuple, RelationMap $map): int
281281
}
282282

283283
$innerKeys = $relation->getInnerKeys();
284-
$isWaitingKeys = array_intersect($innerKeys, $tuple->state->getWaitingFields(true)) !== [];
285-
$hasChangedKeys = array_intersect($innerKeys, $changedFields) !== [];
284+
$isWaitingKeys = \array_intersect($innerKeys, $tuple->state->getWaitingFields(true)) !== [];
285+
$hasChangedKeys = \array_intersect($innerKeys, $changedFields) !== [];
286286
if ($relationStatus === RelationInterface::STATUS_PREPARE) {
287287
$relData ??= $tuple->mapper->fetchRelations($tuple->entity);
288288
$relation->prepare(
289289
$this->pool,
290290
$tuple,
291291
$relData[$name] ?? null,
292-
$isWaitingKeys || $hasChangedKeys
292+
$isWaitingKeys || $hasChangedKeys,
293293
);
294294
$relationStatus = $tuple->state->getRelationStatus($relation->getName());
295295
}
@@ -298,9 +298,9 @@ private function resolveSlaveRelations(Tuple $tuple, RelationMap $map): int
298298
&& $relationStatus !== RelationInterface::STATUS_RESOLVED
299299
&& !$isWaitingKeys
300300
&& !$hasChangedKeys
301-
&& \count(array_intersect($innerKeys, array_keys($transactData))) === \count($innerKeys)
301+
&& \count(\array_intersect($innerKeys, \array_keys($transactData))) === \count($innerKeys)
302302
) {
303-
$child ??= $tuple->state->getRelation($name);
303+
// $child ??= $tuple->state->getRelation($name);
304304
$relation->queue($this->pool, $tuple);
305305
$relationStatus = $tuple->state->getRelationStatus($relation->getName());
306306
}
@@ -316,7 +316,7 @@ private function resolveSelfWithEmbedded(Tuple $tuple, RelationMap $map, bool $h
316316
if (!$map->hasEmbedded() && !$tuple->state->hasChanges()) {
317317
$tuple->status = !$hasDeferredRelations
318318
? Tuple::STATUS_PROCESSED
319-
: max(Tuple::STATUS_DEFERRED, $tuple->status);
319+
: \max(Tuple::STATUS_DEFERRED, $tuple->status);
320320

321321
return;
322322
}
@@ -358,7 +358,6 @@ private function resolveRelations(Tuple $tuple): void
358358
{
359359
$map = $this->orm->getRelationMap($tuple->node->getRole());
360360

361-
// Dependency relations
362361
$result = $tuple->task === Tuple::TASK_STORE
363362
? $this->resolveMasterRelations($tuple, $map)
364363
: $this->resolveSlaveRelations($tuple, $map);
@@ -368,8 +367,8 @@ private function resolveRelations(Tuple $tuple): void
368367
// Self
369368
if ($deferred && $tuple->status < Tuple::STATUS_PROPOSED) {
370369
$tuple->status = Tuple::STATUS_DEFERRED;
371-
// $this->pool->attachTuple($tuple);
372370
}
371+
373372
if ($isDependenciesResolved) {
374373
if ($tuple->task === Tuple::TASK_STORE) {
375374
$this->resolveSelfWithEmbedded($tuple, $map, $deferred);
@@ -383,7 +382,6 @@ private function resolveRelations(Tuple $tuple): void
383382
}
384383

385384
if ($tuple->cascade) {
386-
// Slave relations relations
387385
$tuple->task === Tuple::TASK_STORE
388386
? $this->resolveSlaveRelations($tuple, $map)
389387
: $this->resolveMasterRelations($tuple, $map);

tests/ORM/Functional/Driver/Common/Integration/Case321/schema.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
declare(strict_types=1);
44

55
use Cycle\ORM\Mapper\Mapper;
6+
use Cycle\ORM\Schema\GeneratedField;
67
use Cycle\ORM\SchemaInterface as Schema;
78
use Cycle\ORM\Select\Source;
89
use Cycle\ORM\Tests\Functional\Driver\Common\Integration\Case321\Entity\User1;
@@ -27,7 +28,7 @@
2728
],
2829
Schema::SCHEMA => [],
2930
Schema::GENERATED_FIELDS => [
30-
'id' => Schema::GENERATED_DB, // autoincrement
31+
'id' => GeneratedField::DB_INSERT, // autoincrement
3132
],
3233
],
3334
'user2' => [
@@ -48,7 +49,7 @@
4849
],
4950
Schema::SCHEMA => [],
5051
Schema::GENERATED_FIELDS => [
51-
'id' => Schema::GENERATED_DB, // autoincrement
52+
'id' => GeneratedField::DB_INSERT, // autoincrement
5253
],
5354
],
5455
];

tests/ORM/Functional/Driver/Postgres/SerialColumnTest.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Cycle\ORM\Mapper\Mapper;
1010
use Cycle\ORM\ORMInterface;
1111
use Cycle\ORM\Schema;
12+
use Cycle\ORM\Schema\GeneratedField;
1213
use Cycle\ORM\SchemaInterface;
1314
use Cycle\ORM\Select;
1415
use Cycle\ORM\Tests\Fixtures\CyclicRef2\Document;
@@ -64,7 +65,7 @@ public function setUp(): void
6465
SchemaInterface::SCHEMA => [],
6566
SchemaInterface::RELATIONS => [],
6667
SchemaInterface::GENERATED_FIELDS => [
67-
'balance' => SchemaInterface::GENERATED_DB, // sequence
68+
'balance' => GeneratedField::DB_INSERT, // sequence
6869
],
6970
],
7071
Document::class => [
@@ -81,10 +82,10 @@ public function setUp(): void
8182
'updated_at' => 'datetime',
8283
],
8384
SchemaInterface::GENERATED_FIELDS => [
84-
'id' => SchemaInterface::GENERATED_DB,
85-
'body' => SchemaInterface::GENERATED_DB,
86-
'created_at' => SchemaInterface::GENERATED_DB,
87-
'updated_at' => SchemaInterface::GENERATED_PHP_INSERT | SchemaInterface::GENERATED_PHP_UPDATE,
85+
'id' => GeneratedField::DB_INSERT,
86+
'body' => GeneratedField::DB_INSERT,
87+
'created_at' => GeneratedField::DB_INSERT,
88+
'updated_at' => GeneratedField::PHP_INSERT | GeneratedField::PHP_UPDATE,
8889
],
8990
],
9091
]));
@@ -134,7 +135,7 @@ public function testPersistMultipleSerial(): void
134135

135136
protected function getCommandGenerator(): ?Transaction\CommandGeneratorInterface
136137
{
137-
return new class extends Transaction\CommandGenerator {
138+
return new class () extends Transaction\CommandGenerator {
138139
protected function storeEntity(ORMInterface $orm, Transaction\Tuple $tuple, bool $isNew): ?CommandInterface
139140
{
140141
/** @var CommandInterface|null $command */

0 commit comments

Comments
 (0)