Completed
Pull Request — master (#16)
by David
13:19
created

TdbmFluidColumnJsonOptions::addAnnotation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 4
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
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
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
}
155