Skip to content

Commit

Permalink
[make:entity] Improve uid support (#1139)
Browse files Browse the repository at this point in the history
  • Loading branch information
HypeMC authored Jul 5, 2022
1 parent 5d517da commit b23178c
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Maker/MakeEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ private function askForNextField(ConsoleStyle $io, array $fields, string $entity
} elseif (str_starts_with($snakeCasedField, 'has_')) {
$defaultType = 'boolean';
} elseif ('uuid' === $snakeCasedField) {
$defaultType = 'uuid';
$defaultType = Type::hasType('uuid') ? 'uuid' : 'guid';
} elseif ('guid' === $snakeCasedField) {
$defaultType = 'guid';
}
Expand Down
6 changes: 6 additions & 0 deletions src/Util/ClassSourceManipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
use Symfony\Bundle\MakerBundle\Doctrine\RelationOneToMany;
use Symfony\Bundle\MakerBundle\Doctrine\RelationOneToOne;
use Symfony\Bundle\MakerBundle\Str;
use Symfony\Component\Uid\Ulid;
use Symfony\Component\Uid\Uuid;

/**
* @internal
Expand Down Expand Up @@ -94,6 +96,8 @@ public function addEntityField(string $propertyName, array $columnOptions, array
$defaultValue = null;
if ('array' === $typeHint) {
$defaultValue = new Node\Expr\Array_([], ['kind' => Node\Expr\Array_::KIND_SHORT]);
} elseif ('\\' === $typeHint[0] && false !== strpos($typeHint, '\\', 1)) {
$typeHint = $this->addUseStatementIfNecessary(substr($typeHint, 1));
}

$this->addProperty(
Expand Down Expand Up @@ -1030,6 +1034,8 @@ private function getEntityTypeHint(string $doctrineType): ?string
'datetime_immutable', 'datetimetz_immutable', 'date_immutable', 'time_immutable' => '\\'.\DateTimeImmutable::class,
'dateinterval' => '\\'.\DateInterval::class,
'object' => 'object',
'uuid' => '\\'.Uuid::class,
'ulid' => '\\'.Ulid::class,
default => null,
};
}
Expand Down
18 changes: 18 additions & 0 deletions tests/Util/ClassSourceManipulatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,24 @@ public function getAddEntityFieldTests(): \Generator
],
'User_simple_object.php',
];

yield 'entity_add_uuid' => [
'User_simple.php',
'uuid',
[
'type' => 'uuid',
],
'User_simple_uuid.php',
];

yield 'entity_add_ulid' => [
'User_simple.php',
'ulid',
[
'type' => 'ulid',
],
'User_simple_ulid.php',
];
}

/**
Expand Down
35 changes: 35 additions & 0 deletions tests/Util/fixtures/add_entity_field/User_simple_ulid.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Uid\Ulid;

#[ORM\Entity]
class User
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer')]
private $id;

#[ORM\Column(type: 'ulid')]
private $ulid;

public function getId(): ?int
{
return $this->id;
}

public function getUlid(): ?Ulid
{
return $this->ulid;
}

public function setUlid(Ulid $ulid): self
{
$this->ulid = $ulid;

return $this;
}
}
35 changes: 35 additions & 0 deletions tests/Util/fixtures/add_entity_field/User_simple_uuid.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Uid\Uuid;

#[ORM\Entity]
class User
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer')]
private $id;

#[ORM\Column(type: 'uuid')]
private $uuid;

public function getId(): ?int
{
return $this->id;
}

public function getUuid(): ?Uuid
{
return $this->uuid;
}

public function setUuid(Uuid $uuid): self
{
$this->uuid = $uuid;

return $this;
}
}

0 comments on commit b23178c

Please sign in to comment.