Skip to content

Commit 932af5a

Browse files
authored
Merge pull request #3 from moufmouf/doctrine_annotation_writer
Use new class to generate more easily doctrine annotations
2 parents c1ea428 + d6693c0 commit 932af5a

11 files changed

+161
-28
lines changed

src/Comment.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,23 @@ public function hasAnnotation(string $annotation): bool
5757
return false;
5858
}
5959

60-
public function addAnnotation(string $annotation, string $content = '', bool $replaceExisting = true): self
60+
/**
61+
* @param string $annotation
62+
* @param mixed $content
63+
* @param bool $replaceExisting
64+
* @return Comment
65+
*/
66+
public function addAnnotation(string $annotation, $content = null, bool $replaceExisting = true, bool $explicitNull = false): self
6167
{
6268
if ($replaceExisting === true) {
6369
$this->removeAnnotation($annotation);
6470
}
6571
$annotation = ltrim($annotation, '@');
72+
if ($explicitNull === true && $content === null) {
73+
$content = '(null)';
74+
} else {
75+
$content = DoctrineAnnotationDumper::exportValues($content);
76+
}
6677
$this->comment .= "\n@".$annotation.$content;
6778
return $this;
6879
}

src/DoctrineAnnotationDumper.php

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
3+
4+
namespace TheCodingMachine\FluidSchema;
5+
6+
7+
use function addslashes;
8+
use function array_map;
9+
use function implode;
10+
use function is_array;
11+
use function is_numeric;
12+
use function is_string;
13+
use function var_export;
14+
15+
class DoctrineAnnotationDumper
16+
{
17+
/**
18+
* Write a string representing the parameter passed in.
19+
* @param mixed $item
20+
*/
21+
public static function exportValues($item): string
22+
{
23+
if ($item === null) {
24+
return '';
25+
}
26+
if ($item === []) {
27+
return '({})';
28+
}
29+
return '('.self::innerExportValues($item, true).')';
30+
}
31+
32+
private static function innerExportValues($item, bool $first): string
33+
{
34+
if ($item === null) {
35+
return 'null';
36+
}
37+
if (is_string($item)) {
38+
return '"'.addslashes($item).'"';
39+
}
40+
if (is_numeric($item)) {
41+
return $item;
42+
}
43+
if (is_bool($item)) {
44+
return $item ? 'true' : 'false';
45+
}
46+
if (is_array($item)) {
47+
if (self::isAssoc($item)) {
48+
if ($first) {
49+
array_walk($item, function(&$value, $key) {
50+
$value = $key.' = '.self::innerExportValues($value, false);
51+
});
52+
} else {
53+
array_walk($item, function(&$value, $key) {
54+
$value = '"'.addslashes($key).'":'.self::innerExportValues($value, false);
55+
});
56+
}
57+
$result = implode(', ', $item);
58+
if (!$first) {
59+
$result = '{'.$result.'}';
60+
}
61+
return $result;
62+
} else {
63+
array_walk($item, function(&$value, $key) {
64+
$value = self::innerExportValues($value, false);
65+
});
66+
$result = implode(', ', $item);
67+
if (!$first) {
68+
$result = '{'.$result.'}';
69+
}
70+
return $result;
71+
}
72+
}
73+
throw new \RuntimeException('Cannot serialize value in Doctrine annotation.');
74+
}
75+
76+
private static function isAssoc(array $arr): bool
77+
{
78+
return array_keys($arr) !== range(0, count($arr) - 1);
79+
}
80+
}

src/TdbmFluidColumnGraphqlOptions.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public function __construct(TdbmFluidColumnOptions $tdbmFluidColumnOptions)
2121

2222
public function fieldName(string $name): self
2323
{
24-
$this->addAnnotation('TheCodingMachine\\GraphQLite\\Annotations\\Field', '(name="'.addslashes($name).'")');
24+
$this->addAnnotation('TheCodingMachine\\GraphQLite\\Annotations\\Field', ['name'=>$name]);
2525
return $this;
2626
}
2727

@@ -37,19 +37,25 @@ public function logged(bool $mustBeLogged = true): self
3737

3838
public function right(string $rightName): self
3939
{
40-
$this->addAnnotation('TheCodingMachine\\GraphQLite\\Annotations\\Right', '(name="'.addslashes($rightName).'")');
40+
$this->addAnnotation('TheCodingMachine\\GraphQLite\\Annotations\\Right', ['name'=>$rightName]);
4141
return $this;
4242
}
4343

4444
public function failWith($value): self
4545
{
46-
$this->addAnnotation('TheCodingMachine\\GraphQLite\\Annotations\\FailWith', '('.var_export($value, true).')');
46+
$this->addAnnotation('TheCodingMachine\\GraphQLite\\Annotations\\FailWith', $value, true, true);
4747
return $this;
4848
}
4949

50-
public function addAnnotation(string $annotation, string $content = '', bool $replaceExisting = true): self
50+
/**
51+
* @param string $annotation
52+
* @param mixed $content
53+
* @param bool $replaceExisting
54+
* @return TdbmFluidColumnGraphqlOptions
55+
*/
56+
public function addAnnotation(string $annotation, $content = null, bool $replaceExisting = true, bool $explicitNull = false): self
5157
{
52-
$this->tdbmFluidColumnOptions->addAnnotation($annotation, $content, $replaceExisting);
58+
$this->tdbmFluidColumnOptions->addAnnotation($annotation, $content, $replaceExisting, $explicitNull);
5359
return $this;
5460
}
5561

src/TdbmFluidColumnOptions.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,15 @@ private function saveComment(Comment $comment): self
123123
return $this;
124124
}
125125

126-
public function addAnnotation(string $annotation, string $content = '', bool $replaceExisting = true): self
126+
/**
127+
* @param string $annotation
128+
* @param mixed $content
129+
* @param bool $replaceExisting
130+
* @return TdbmFluidColumnOptions
131+
*/
132+
public function addAnnotation(string $annotation, $content = null, bool $replaceExisting = true, bool $explicitNull = false): self
127133
{
128-
$comment = $this->getComment()->addAnnotation($annotation, $content, $replaceExisting);
134+
$comment = $this->getComment()->addAnnotation($annotation, $content, $replaceExisting, $explicitNull);
129135
$this->saveComment($comment);
130136
return $this;
131137
}

src/TdbmFluidJunctionTableGraphqlOptions.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,25 @@ public function logged(bool $mustBeLogged = true): self
3636

3737
public function right(string $rightName): self
3838
{
39-
$this->addAnnotation('TheCodingMachine\\GraphQLite\\Annotations\\Right', '(name="'.addslashes($rightName).'")');
39+
$this->addAnnotation('TheCodingMachine\\GraphQLite\\Annotations\\Right', ['name'=>$rightName]);
4040
return $this;
4141
}
4242

4343
public function failWith($value): self
4444
{
45-
$this->addAnnotation('TheCodingMachine\\GraphQLite\\Annotations\\FailWith', '('.var_export($value, true).')');
45+
$this->addAnnotation('TheCodingMachine\\GraphQLite\\Annotations\\FailWith', $value, true, true);
4646
return $this;
4747
}
4848

49-
public function addAnnotation(string $annotation, string $content = '', bool $replaceExisting = true): self
49+
/**
50+
* @param string $annotation
51+
* @param mixed $content
52+
* @param bool $replaceExisting
53+
* @return TdbmFluidJunctionTableGraphqlOptions
54+
*/
55+
public function addAnnotation(string $annotation, $content = null, bool $replaceExisting = true, bool $explicitNull = false): self
5056
{
51-
$this->tdbmFluidTable->addAnnotation($annotation, $content, $replaceExisting);
57+
$this->tdbmFluidTable->addAnnotation($annotation, $content, $replaceExisting, $explicitNull);
5258
return $this;
5359
}
5460

src/TdbmFluidTable.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public function uuid(string $version = 'v4'): TdbmFluidTable
7373
}
7474
$this->fluidTable->uuid();
7575

76-
$this->column('uuid')->guid()->addAnnotation('UUID', '("'.$version.'")');
76+
$this->column('uuid')->guid()->addAnnotation('UUID', $version);
7777
return $this;
7878
}
7979

@@ -97,7 +97,7 @@ public function extends(string $tableName): TdbmFluidTable
9797
*/
9898
public function customBeanName(string $beanName): TdbmFluidTable
9999
{
100-
$this->addAnnotation('Bean', '(name="'.addslashes($beanName).'")');
100+
$this->addAnnotation('Bean', ['name'=>$beanName]);
101101
return $this;
102102
}
103103

@@ -124,9 +124,15 @@ private function saveComment(Comment $comment): self
124124
return $this;
125125
}
126126

127-
public function addAnnotation(string $annotation, string $content = '', bool $replaceExisting = true): self
127+
/**
128+
* @param string $annotation
129+
* @param mixed $content
130+
* @param bool $replaceExisting
131+
* @return TdbmFluidTable
132+
*/
133+
public function addAnnotation(string $annotation, $content = null, bool $replaceExisting = true, bool $explicitNull = false): self
128134
{
129-
$comment = $this->getComment()->addAnnotation($annotation, $content, $replaceExisting);
135+
$comment = $this->getComment()->addAnnotation($annotation, $content, $replaceExisting, $explicitNull);
130136
$this->saveComment($comment);
131137
return $this;
132138
}

tests/CommentTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public function testAddAnnotation()
3939
@Yop
4040
EOF
4141
);
42-
$comment->addAnnotation('Yop', '(true)');
42+
$comment->addAnnotation('Yop', true);
4343
$this->assertSame(<<<EOF
4444
Foo
4545
@Yop(true)
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace TheCodingMachine\FluidSchema;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
class DoctrineAnnotationDumperTest extends TestCase
8+
{
9+
10+
public function testExportValues()
11+
{
12+
$this->assertSame('', DoctrineAnnotationDumper::exportValues(null));
13+
$this->assertSame('({})', DoctrineAnnotationDumper::exportValues([]));
14+
$this->assertSame('("foo")', DoctrineAnnotationDumper::exportValues("foo"));
15+
$this->assertSame('(foo = "bar")', DoctrineAnnotationDumper::exportValues(["foo"=>"bar"]));
16+
$this->assertSame('(foo = {"bar":"baz", "baz":"bar"})', DoctrineAnnotationDumper::exportValues(["foo"=>["bar"=>"baz","baz"=>"bar"]]));
17+
}
18+
}

tests/TdbmFluidColumnGraphqlOptionsTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,16 @@ public function testGraphql()
2525
->right('CAN_EDIT')
2626
->failWith(null);
2727

28-
$this->assertSame("\n@TheCodingMachine\GraphQLite\Annotations\Field(name=\"bar\")
28+
$this->assertSame("\n@TheCodingMachine\GraphQLite\Annotations\Field(name = \"bar\")
2929
@TheCodingMachine\GraphQLite\Annotations\Logged
30-
@TheCodingMachine\GraphQLite\Annotations\Right(name=\"CAN_EDIT\")
31-
@TheCodingMachine\GraphQLite\Annotations\FailWith(NULL)", $schema->getTable('posts')->getColumn('foo')->getComment());
30+
@TheCodingMachine\GraphQLite\Annotations\Right(name = \"CAN_EDIT\")
31+
@TheCodingMachine\GraphQLite\Annotations\FailWith(null)", $schema->getTable('posts')->getColumn('foo')->getComment());
3232

3333
$graphqlOptions->logged(false);
3434

35-
$this->assertSame("\n@TheCodingMachine\GraphQLite\Annotations\Field(name=\"bar\")
36-
@TheCodingMachine\GraphQLite\Annotations\Right(name=\"CAN_EDIT\")
37-
@TheCodingMachine\GraphQLite\Annotations\FailWith(NULL)", $schema->getTable('posts')->getColumn('foo')->getComment());
35+
$this->assertSame("\n@TheCodingMachine\GraphQLite\Annotations\Field(name = \"bar\")
36+
@TheCodingMachine\GraphQLite\Annotations\Right(name = \"CAN_EDIT\")
37+
@TheCodingMachine\GraphQLite\Annotations\FailWith(null)", $schema->getTable('posts')->getColumn('foo')->getComment());
3838

3939
$this->assertSame($columnOptions, $graphqlOptions->endGraphql());
4040

tests/TdbmFluidJunctionTableGraphqlOptionsTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@ public function testGraphql()
2525

2626
$this->assertSame("\n@TheCodingMachine\GraphQLite\Annotations\Field
2727
@TheCodingMachine\GraphQLite\Annotations\Logged
28-
@TheCodingMachine\GraphQLite\Annotations\Right(name=\"CAN_EDIT\")
29-
@TheCodingMachine\GraphQLite\Annotations\FailWith(NULL)", $schema->getTable('posts_users')->getOptions()['comment']);
28+
@TheCodingMachine\GraphQLite\Annotations\Right(name = \"CAN_EDIT\")
29+
@TheCodingMachine\GraphQLite\Annotations\FailWith(null)", $schema->getTable('posts_users')->getOptions()['comment']);
3030

3131
$graphqlOptions->logged(false);
3232

3333
$this->assertSame("\n@TheCodingMachine\GraphQLite\Annotations\Field
34-
@TheCodingMachine\GraphQLite\Annotations\Right(name=\"CAN_EDIT\")
35-
@TheCodingMachine\GraphQLite\Annotations\FailWith(NULL)", $schema->getTable('posts_users')->getOptions()['comment']);
34+
@TheCodingMachine\GraphQLite\Annotations\Right(name = \"CAN_EDIT\")
35+
@TheCodingMachine\GraphQLite\Annotations\FailWith(null)", $schema->getTable('posts_users')->getOptions()['comment']);
3636

3737
$this->assertSame($junctionTableOptions, $graphqlOptions->endGraphql());
3838
}

0 commit comments

Comments
 (0)