Skip to content

Commit 5643f55

Browse files
committed
Adding support for JSON serialization annotations
1 parent 5a63b4e commit 5643f55

File tree

3 files changed

+199
-0
lines changed

3 files changed

+199
-0
lines changed

src/TdbmFluidColumnJsonOptions.php

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
<?php
2+
3+
4+
namespace TheCodingMachine\FluidSchema;
5+
6+
7+
use function addslashes;
8+
use Doctrine\DBAL\Types\Type;
9+
use function var_export;
10+
11+
class TdbmFluidColumnJsonOptions
12+
{
13+
/**
14+
* @var TdbmFluidColumnOptions
15+
*/
16+
private $tdbmFluidColumnOptions;
17+
/**
18+
* @var string
19+
*/
20+
private $name;
21+
/**
22+
* @var string
23+
*/
24+
private $outputType;
25+
/**
26+
* @var FluidColumn
27+
*/
28+
private $fluidColumn;
29+
30+
public function __construct(TdbmFluidColumnOptions $tdbmFluidColumnOptions, FluidColumn $fluidColumn)
31+
{
32+
$this->tdbmFluidColumnOptions = $tdbmFluidColumnOptions;
33+
$this->fluidColumn = $fluidColumn;
34+
}
35+
36+
private function getComment(): Comment
37+
{
38+
$comment = $this->fluidColumn->getDbalColumn()->getComment();
39+
40+
return new Comment($comment ?? '');
41+
}
42+
43+
public function key(string $name): self
44+
{
45+
$this->addAnnotation('JsonKey', ['key' => $name]);
46+
return $this;
47+
}
48+
49+
public function datetimeFormat(string $format): self
50+
{
51+
$this->addAnnotation('JsonFormat', ['date' => $format]);
52+
return $this;
53+
}
54+
55+
/**
56+
* @param int|null $decimals The number of decimals
57+
* @param string|null $point The decimal point
58+
* @param string|null $separator The thousands separator
59+
* @param string|null $unit The suffix to append after a number
60+
*/
61+
public function numericFormat(?int $decimals = null, ?string $point = null, ?string $separator = null, ?string $unit = null): self
62+
{
63+
$params = [];
64+
if ($decimals !== null) {
65+
$params['decimals'] = $decimals;
66+
}
67+
if ($point !== null) {
68+
$params['point'] = $point;
69+
}
70+
if ($separator !== null) {
71+
$params['separator'] = $separator;
72+
}
73+
if ($unit !== null) {
74+
$params['unit'] = $unit;
75+
}
76+
$this->addAnnotation('JsonFormat', $params);
77+
return $this;
78+
}
79+
80+
/**
81+
* Serialize in JSON by getting the property from an object.
82+
*/
83+
public function formatUsingProperty(string $property): self
84+
{
85+
$this->addAnnotation('JsonFormat', ['property' => $property]);
86+
return $this;
87+
}
88+
89+
/**
90+
* Serialize in JSON by calling a method from an object.
91+
*/
92+
public function formatUsingMethod(string $method): self
93+
{
94+
$this->addAnnotation('JsonFormat', ['method' => $method]);
95+
return $this;
96+
}
97+
98+
public function ignore(): self
99+
{
100+
$this->addAnnotation('JsonIgnore');
101+
return $this;
102+
}
103+
104+
public function include(): self
105+
{
106+
$this->addAnnotation('JsonInclude');
107+
return $this;
108+
}
109+
110+
public function recursive(): self
111+
{
112+
$this->addAnnotation('JsonRecursive');
113+
return $this;
114+
}
115+
116+
public function collection(string $key): self
117+
{
118+
$this->addAnnotation('JsonCollection', ['key' => $key]);
119+
return $this;
120+
}
121+
122+
/**
123+
* @param string $annotation
124+
* @param mixed $content
125+
* @param bool $replaceExisting
126+
* @return TdbmFluidColumnGraphqlOptions
127+
*/
128+
public function addAnnotation(string $annotation, $content = null, bool $replaceExisting = true, bool $explicitNull = false): self
129+
{
130+
$this->tdbmFluidColumnOptions->addAnnotation($annotation, $content, $replaceExisting, $explicitNull);
131+
return $this;
132+
}
133+
134+
public function removeAnnotation(string $annotation): self
135+
{
136+
$this->tdbmFluidColumnOptions->removeAnnotation($annotation);
137+
return $this;
138+
}
139+
140+
public function endJsonSerialize(): TdbmFluidColumnOptions
141+
{
142+
return $this->tdbmFluidColumnOptions;
143+
}
144+
145+
public function then(): TdbmFluidTable
146+
{
147+
return $this->tdbmFluidColumnOptions->then();
148+
}
149+
150+
public function column(string $name): TdbmFluidColumn
151+
{
152+
return $this->tdbmFluidColumnOptions->column($name);
153+
}
154+
}

src/TdbmFluidColumnOptions.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,11 @@ public function graphqlField(): TdbmFluidColumnGraphqlOptions
108108
return new TdbmFluidColumnGraphqlOptions($this, $this->fluidColumn);
109109
}
110110

111+
public function jsonSerialize(): TdbmFluidColumnJsonOptions
112+
{
113+
return new TdbmFluidColumnJsonOptions($this, $this->fluidColumn);
114+
}
115+
111116
public function protectedGetter(): self
112117
{
113118
$this->addAnnotation('TheCodingMachine\\TDBM\\Utils\\Annotation\\ProtectedGetter');
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace TheCodingMachine\FluidSchema;
4+
5+
use Doctrine\DBAL\Schema\Schema;
6+
use Doctrine\DBAL\Types\Type;
7+
use PHPUnit\Framework\TestCase;
8+
9+
class TdbmFluidJunctionTableJsonOptionsTest extends TestCase
10+
{
11+
public function testJson()
12+
{
13+
$schema = new Schema();
14+
$fluid = new TdbmFluidSchema($schema);
15+
16+
$fluid->table('nodes')
17+
->column('id')->integer()->primaryKey()->autoIncrement()->jsonSerialize()->ignore()
18+
->column('alias_id')->references('nodes')->null()->jsonSerialize()->recursive()
19+
->column('parent_id')->references('nodes')->null()->jsonSerialize()->include()
20+
->column('root_id')->references('nodes')->null()->jsonSerialize()->ignore()
21+
->column('owner_id')->references('nodes')->null()->jsonSerialize()->formatUsingProperty('name')->include()
22+
->column('name')->string()->jsonSerialize()->key('basename')
23+
->column('size')->integer()->notNull()->default(0)->jsonSerialize()->numericFormat(null, null, null, ' o')
24+
->column('weight')->float()->null()->jsonSerialize()->numericFormat(2, ',', '.', 'g')
25+
->column('created_at')->date()->null()->jsonSerialize()->datetimeFormat("Y-m-d")
26+
->column('another_parent')->references('nodes')->comment('@JsonCollection("entries") @JsonFormat(property="entry")');
27+
28+
$nodesTable = $schema->getTable('nodes');
29+
$this->assertContains("@JsonIgnore", $nodesTable->getColumn('id')->getComment());
30+
$this->assertContains("@JsonRecursive", $nodesTable->getColumn('alias_id')->getComment());
31+
$this->assertContains("@JsonInclude", $nodesTable->getColumn('parent_id')->getComment());
32+
$this->assertContains("@JsonIgnore", $nodesTable->getColumn('root_id')->getComment());
33+
$this->assertContains('@JsonFormat(property = "name")', $nodesTable->getColumn('owner_id')->getComment());
34+
$this->assertContains("@JsonKey(key = \"basename\")", $nodesTable->getColumn('name')->getComment());
35+
$this->assertContains("@JsonFormat(unit = \" o\")", $nodesTable->getColumn('size')->getComment());
36+
$this->assertContains("@JsonFormat(decimals = 2, point = \",\", separator = \".\", unit = \"g\")", $nodesTable->getColumn('weight')->getComment());
37+
$this->assertContains("@JsonFormat(date = \"Y-m-d\")", $nodesTable->getColumn('created_at')->getComment());
38+
$this->assertContains("@JsonCollection(\"entries\")", $nodesTable->getColumn('another_parent')->getComment());
39+
}
40+
}

0 commit comments

Comments
 (0)