Skip to content

Commit

Permalink
Add IS_REPEATABLE attr, nullable param and updating Psalm (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
msmakouz authored May 19, 2023
1 parent 95b63ba commit 3772826
Show file tree
Hide file tree
Showing 36 changed files with 1,024 additions and 73 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/ci-mssql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ jobs:
extensions: pdo, pdo_sqlsrv
mssql: 'server:2019-latest'
- php: '8.1'
extensions: pdo, pdo_sqlsrv-5.10.0beta2
extensions: pdo, pdo_sqlsrv-5.11.0
mssql: 'server:2019-latest'
- php: '8.2'
extensions: pdo, pdo_sqlsrv-5.11.0
mssql: 'server:2019-latest'

services:
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/ci-mysql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ jobs:
php-version:
- "8.0"
- "8.1"
- "8.2"

mysql-version:
- "5.7"
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/ci-pgsql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ jobs:
php-version:
- "8.0"
- "8.1"
- "8.2"

pgsql-version:
- "10"
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ jobs:
php-version:
- "8.0"
- "8.1"
- "8.2"
steps:
- name: Checkout
uses: actions/checkout@v2
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/static.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
- ubuntu-latest

php:
- "8.0"
- "8.2"

steps:
- name: Checkout
Expand Down Expand Up @@ -44,4 +44,4 @@ jobs:
run: composer update --prefer-dist --no-interaction --no-progress --optimize-autoloader --ansi

- name: Static analysis
run: vendor/bin/psalm --shepherd --stats --output-format=checkstyle
run: vendor/bin/psalm --shepherd --stats --output-format=checkstyle
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"cycle/annotated": "^3.0",
"phpunit/phpunit": "^9.5",
"spiral/tokenizer": "^2.8",
"vimeo/psalm": "^4.13"
"vimeo/psalm": "^5.11"
},
"license": "MIT",
"autoload": {
Expand Down
2 changes: 2 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
findUnusedCode="false"
findUnusedBaselineEntry="true"
>
<projectFiles>
<directory name="src" />
Expand Down
5 changes: 3 additions & 2 deletions src/Listener/Uuid1.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@ final class Uuid1
public function __construct(
private string $field = 'uuid',
private Hexadecimal|int|string|null $node = null,
private ?int $clockSeq = null
private ?int $clockSeq = null,
private bool $nullable = false
) {
}

#[Listen(OnCreate::class)]
public function __invoke(OnCreate $event): void
{
if (!isset($event->state->getData()[$this->field])) {
if (!$this->nullable && !isset($event->state->getData()[$this->field])) {
$event->state->register($this->field, Uuid::uuid1($this->node, $this->clockSeq));
}
}
Expand Down
17 changes: 10 additions & 7 deletions src/Listener/Uuid2.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,28 @@ public function __construct(
private string $field = 'uuid',
private IntegerObject|string|null $localIdentifier = null,
private Hexadecimal|string|null $node = null,
private ?int $clockSeq = null
private ?int $clockSeq = null,
private bool $nullable = false
) {
}

#[Listen(OnCreate::class)]
public function __invoke(OnCreate $event): void
{
if ($this->nullable || isset($event->state->getData()[$this->field])) {
return;
}

if (\is_string($this->localIdentifier)) {
$this->localIdentifier = new IntegerObject($this->localIdentifier);
}
if (\is_string($this->node)) {
$this->node = new Hexadecimal($this->node);
}

if (!isset($event->state->getData()[$this->field])) {
$event->state->register(
$this->field,
Uuid::uuid2($this->localDomain, $this->localIdentifier, $this->node, $this->clockSeq)
);
}
$event->state->register(
$this->field,
Uuid::uuid2($this->localDomain, $this->localIdentifier, $this->node, $this->clockSeq)
);
}
}
5 changes: 3 additions & 2 deletions src/Listener/Uuid3.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@ final class Uuid3
public function __construct(
private string|UuidInterface $namespace,
private string $name,
private string $field = 'uuid'
private string $field = 'uuid',
private bool $nullable = false
) {
}

#[Listen(OnCreate::class)]
public function __invoke(OnCreate $event): void
{
if (!isset($event->state->getData()[$this->field])) {
if (!$this->nullable && !isset($event->state->getData()[$this->field])) {
$event->state->register($this->field, Uuid::uuid3($this->namespace, $this->name));
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/Listener/Uuid4.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@
final class Uuid4
{
public function __construct(
private string $field = 'uuid'
private string $field = 'uuid',
private bool $nullable = false
) {
}

#[Listen(OnCreate::class)]
public function __invoke(OnCreate $event): void
{
if (!isset($event->state->getData()[$this->field])) {
if (!$this->nullable && !isset($event->state->getData()[$this->field])) {
$event->state->register($this->field, Uuid::uuid4());
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/Listener/Uuid5.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@ final class Uuid5
public function __construct(
private string|UuidInterface $namespace,
private string $name,
private string $field = 'uuid'
private string $field = 'uuid',
private bool $nullable = false
) {
}

#[Listen(OnCreate::class)]
public function __invoke(OnCreate $event): void
{
if (!isset($event->state->getData()[$this->field])) {
if (!$this->nullable && !isset($event->state->getData()[$this->field])) {
$event->state->register($this->field, Uuid::uuid5($this->namespace, $this->name));
}
}
Expand Down
11 changes: 7 additions & 4 deletions src/Listener/Uuid6.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,22 @@ final class Uuid6
public function __construct(
private string $field = 'uuid',
private Hexadecimal|string|null $node = null,
private ?int $clockSeq = null
private ?int $clockSeq = null,
private bool $nullable = false
) {
}

#[Listen(OnCreate::class)]
public function __invoke(OnCreate $event): void
{
if ($this->nullable || isset($event->state->getData()[$this->field])) {
return;
}

if (\is_string($this->node)) {
$this->node = new Hexadecimal($this->node);
}

if (!isset($event->state->getData()[$this->field])) {
$event->state->register($this->field, Uuid::uuid6($this->node, $this->clockSeq));
}
$event->state->register($this->field, Uuid::uuid6($this->node, $this->clockSeq));
}
}
5 changes: 3 additions & 2 deletions src/Listener/Uuid7.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@
final class Uuid7
{
public function __construct(
private string $field = 'uuid'
private string $field = 'uuid',
private bool $nullable = false
) {
}

#[Listen(OnCreate::class)]
public function __invoke(OnCreate $event): void
{
if (!isset($event->state->getData()[$this->field])) {
if (!$this->nullable && !isset($event->state->getData()[$this->field])) {
$event->state->register($this->field, Uuid::uuid7());
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/Uuid.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ abstract class Uuid extends BaseModifier
{
protected ?string $column = null;
protected string $field;
protected bool $nullable = false;

public function compute(Registry $registry): void
{
$modifier = new RegistryModifier($registry, $this->role);
$this->column = $modifier->findColumnName($this->field, $this->column);

if ($this->column !== null) {
$modifier->addUuidColumn($this->column, $this->field);
$modifier->addUuidColumn($this->column, $this->field)->nullable($this->nullable);
$modifier->setTypecast(
$registry->getEntity($this->role)->getFields()->get($this->field),
[RamseyUuid::class, 'fromString']
Expand All @@ -33,7 +33,7 @@ public function render(Registry $registry): void
$modifier = new RegistryModifier($registry, $this->role);
$this->column = $modifier->findColumnName($this->field, $this->column) ?? $this->field;

$modifier->addUuidColumn($this->column, $this->field);
$modifier->addUuidColumn($this->column, $this->field)->nullable($this->nullable);
$modifier->setTypecast(
$registry->getEntity($this->role)->getFields()->get($this->field),
[RamseyUuid::class, 'fromString']
Expand Down
12 changes: 8 additions & 4 deletions src/Uuid1.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
* @NamedArgumentConstructor()
* @Target({"CLASS"})
*/
#[\Attribute(\Attribute::TARGET_CLASS), NamedArgumentConstructor]
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::IS_REPEATABLE), NamedArgumentConstructor]
final class Uuid1 extends Uuid
{
/**
Expand All @@ -30,31 +30,35 @@ final class Uuid1 extends Uuid
* @param int|null $clockSeq A 14-bit number used to help avoid duplicates
* that could arise when the clock is set backwards in time or if the
* node ID changes
* @param bool $nullable Indicates whether to generate a new UUID or not
*
* @see \Ramsey\Uuid\UuidFactoryInterface::uuid1()
*/
public function __construct(
string $field = 'uuid',
?string $column = null,
private Hexadecimal|int|string|null $node = null,
private ?int $clockSeq = null
private ?int $clockSeq = null,
bool $nullable = false
) {
$this->field = $field;
$this->column = $column;
$this->nullable = $nullable;
}

protected function getListenerClass(): string
{
return Listener::class;
}

#[ArrayShape(['field' => 'string', 'node' => 'int|string|null', 'clockSeq' => 'int|null'])]
#[ArrayShape(['field' => 'string', 'node' => 'int|string|null', 'clockSeq' => 'int|null', 'nullable' => 'bool'])]
protected function getListenerArgs(): array
{
return [
'field' => $this->field,
'node' => $this->node instanceof Hexadecimal ? (string) $this->node : $this->node,
'clockSeq' => $this->clockSeq
'clockSeq' => $this->clockSeq,
'nullable' => $this->nullable
];
}
}
13 changes: 9 additions & 4 deletions src/Uuid2.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
* @NamedArgumentConstructor()
* @Target({"CLASS"})
*/
#[\Attribute(\Attribute::TARGET_CLASS), NamedArgumentConstructor]
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::IS_REPEATABLE), NamedArgumentConstructor]
final class Uuid2 extends Uuid
{
/**
Expand All @@ -36,6 +36,7 @@ final class Uuid2 extends Uuid
* @param int|null $clockSeq A 14-bit number used to help avoid duplicates
* that could arise when the clock is set backwards in time or if the
* node ID changes
* @param bool $nullable Indicates whether to generate a new UUID or not
*
* @see \Ramsey\Uuid\UuidFactoryInterface::uuid2()
*/
Expand All @@ -45,10 +46,12 @@ public function __construct(
?string $column = null,
private IntegerObject|string|null $localIdentifier = null,
private Hexadecimal|string|null $node = null,
private ?int $clockSeq = null
private ?int $clockSeq = null,
bool $nullable = false
) {
$this->field = $field;
$this->column = $column;
$this->nullable = $nullable;
}

protected function getListenerClass(): string
Expand All @@ -61,7 +64,8 @@ protected function getListenerClass(): string
'localDomain' => 'int',
'localIdentifier' => 'string|null',
'node' => 'string|null',
'clockSeq' => 'int|null'
'clockSeq' => 'int|null',
'nullable' => 'bool'
])]
protected function getListenerArgs(): array
{
Expand All @@ -72,7 +76,8 @@ protected function getListenerArgs(): array
? (string) $this->localIdentifier
: $this->localIdentifier,
'node' => $this->node instanceof Hexadecimal ? (string) $this->node : $this->node,
'clockSeq' => $this->clockSeq
'clockSeq' => $this->clockSeq,
'nullable' => $this->nullable
];
}
}
12 changes: 8 additions & 4 deletions src/Uuid3.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,39 +18,43 @@
* @NamedArgumentConstructor()
* @Target({"CLASS"})
*/
#[\Attribute(\Attribute::TARGET_CLASS), NamedArgumentConstructor]
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::IS_REPEATABLE), NamedArgumentConstructor]
final class Uuid3 extends Uuid
{
/**
* @param string|UuidInterface $namespace The namespace (must be a valid UUID)
* @param non-empty-string $name The name to use for creating a UUID
* @param non-empty-string $field Uuid property name
* @param non-empty-string|null $column Uuid column name
* @param bool $nullable Indicates whether to generate a new UUID or not
*
* @see \Ramsey\Uuid\UuidFactoryInterface::uuid3()
*/
public function __construct(
private string|UuidInterface $namespace,
private string $name,
string $field = 'uuid',
?string $column = null
?string $column = null,
bool $nullable = false
) {
$this->field = $field;
$this->column = $column;
$this->nullable = $nullable;
}

protected function getListenerClass(): string
{
return Listener::class;
}

#[ArrayShape(['field' => 'string', 'namespace' => 'string', 'name' => 'string'])]
#[ArrayShape(['field' => 'string', 'namespace' => 'string', 'name' => 'string', 'nullable' => 'bool'])]
protected function getListenerArgs(): array
{
return [
'field' => $this->field,
'namespace' => $this->namespace instanceof UuidInterface ? (string) $this->namespace : $this->namespace,
'name' => $this->name
'name' => $this->name,
'nullable' => $this->nullable
];
}
}
Loading

0 comments on commit 3772826

Please sign in to comment.