Skip to content

Commit

Permalink
Adding support for the "@uuid" annotation in column comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
moufmouf committed Jul 12, 2017
1 parent 89c59eb commit d6506f4
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 10 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
"phlib/logger": "^2.1",
"symfony/console": "^2.0 || ^3.0",
"mouf/utils.log.psr.multi-logger": "^1.0",
"symfony/filesystem": "~2.7 || ~3.0"
"symfony/filesystem": "~2.7 || ~3.0",
"ramsey/uuid": "^3.6"
},
"require-dev" : {
"phpunit/phpunit": "^5.5",
Expand Down
1 change: 1 addition & 0 deletions src/Utils/BeanDescriptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,7 @@ public function generatePhpCode(): string
use TheCodingMachine\\TDBM\\ResultIterator;
use TheCodingMachine\\TDBM\\ResultArray;
use TheCodingMachine\\TDBM\\AlterableResultIterator;
use Ramsey\\Uuid\\Uuid;
$use
/*
* This file has been automatically generated by TDBM.
Expand Down
36 changes: 27 additions & 9 deletions src/Utils/ScalarBeanPropertyDescriptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use Doctrine\DBAL\Schema\Column;
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
use Ramsey\Uuid\Uuid;
use TheCodingMachine\TDBM\Utils\Annotation\AnnotationParser;

/**
* This class represent a property in a bean (a property has a getter, a setter, etc...).
Expand Down Expand Up @@ -81,17 +83,29 @@ public function getPhpType(): string
*/
public function isCompulsory()
{
return $this->column->getNotnull() && !$this->column->getAutoincrement() && $this->column->getDefault() === null;
return $this->column->getNotnull() && !$this->column->getAutoincrement() && $this->column->getDefault() === null && !$this->hasUuidAnnotation();
}

private function hasUuidAnnotation(): bool
{
$comment = $this->column->getComment();
if ($comment === null) {
return false;
}
$parser = new AnnotationParser();
$annotations = $parser->parse($comment);
$uuidAnnotation = $annotations->findAnnotation('UUID');
return $uuidAnnotation !== null;
}

/**
* Returns true if the property has a default value.
* Returns true if the property has a default value (or if the @UUID annotation is set for the column)
*
* @return bool
*/
public function hasDefault()
{
return $this->column->getDefault() !== null;
return $this->column->getDefault() !== null || $this->hasUuidAnnotation();
}

/**
Expand All @@ -103,12 +117,17 @@ public function assignToDefaultCode()
{
$str = ' $this->%s(%s);';

$default = $this->column->getDefault();

if (strtoupper($default) === 'CURRENT_TIMESTAMP') {
$defaultCode = 'new \DateTimeImmutable()';
if ($this->hasUuidAnnotation()) {
// We generate a UUID1 because it is always handy to have IDs sorted chronologically (UUID4 on the other had is completely random)
$defaultCode = 'Uuid::uuid1()';
} else {
$defaultCode = var_export($this->column->getDefault(), true);
$default = $this->column->getDefault();

if (strtoupper($default) === 'CURRENT_TIMESTAMP') {
$defaultCode = 'new \DateTimeImmutable()';
} else {
$defaultCode = var_export($this->column->getDefault(), true);
}
}

return sprintf($str, $this->getSetterName(), $defaultCode);
Expand Down Expand Up @@ -210,5 +229,4 @@ public function getColumnName()
{
return $this->column->getName();
}

}
11 changes: 11 additions & 0 deletions tests/TDBMDaoGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
use TheCodingMachine\TDBM\Test\Dao\AllNullableDao;
use TheCodingMachine\TDBM\Test\Dao\AnimalDao;
use TheCodingMachine\TDBM\Test\Dao\Bean\AllNullableBean;
use TheCodingMachine\TDBM\Test\Dao\Bean\ArticleBean;
use TheCodingMachine\TDBM\Test\Dao\Bean\BoatBean;
use TheCodingMachine\TDBM\Test\Dao\Bean\CatBean;
use TheCodingMachine\TDBM\Test\Dao\Bean\CategoryBean;
Expand Down Expand Up @@ -1390,4 +1391,14 @@ public function testFindOneByGeneration()
$this->assertSame('login', $parameters[0]->getName());
$this->assertSame('additionalTablesFetch', $parameters[1]->getName());
}

/**
* @depends testDaoGeneration
*/
public function testUuid()
{
$article = new ArticleBean('content');
$this->assertSame('content', $article->getContent());
$this->assertNotEmpty($article->getId());
}
}

0 comments on commit d6506f4

Please sign in to comment.