Passed
Pull Request — master (#2)
by David
03:49
created

getUpperCamelCaseName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace TheCodingMachine\TDBM\Utils;
4
5
use Doctrine\DBAL\Schema\Column;
6
use Doctrine\DBAL\Schema\Table;
7
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
8
9
/**
10
 * This class represent a property in a bean (a property has a getter, a setter, etc...).
11
 */
12
class ScalarBeanPropertyDescriptor extends AbstractBeanPropertyDescriptor
13
{
14
    /**
15
     * @var Column
16
     */
17
    private $column;
18
19
    /**
20
     * ScalarBeanPropertyDescriptor constructor.
21
     * @param Table $table
22
     * @param Column $column
23
     * @param NamingStrategyInterface $namingStrategy
24
     */
25
    public function __construct(Table $table, Column $column, NamingStrategyInterface $namingStrategy)
26
    {
27
        parent::__construct($table, $namingStrategy);
28
        $this->table = $table;
29
        $this->column = $column;
30
    }
31
32
    /**
33
     * Returns the foreign-key the column is part of, if any. null otherwise.
34
     *
35
     * @return ForeignKeyConstraint|null
36
     */
37
    public function getForeignKey()
38
    {
39
        return false;
40
    }
41
42
    /**
43
     * Returns the param annotation for this property (useful for constructor).
44
     *
45
     * @return string
46
     */
47
    public function getParamAnnotation()
48
    {
49
        $className = $this->getClassName();
50
        $paramType = $className ?: TDBMDaoGenerator::dbalTypeToPhpType($this->column->getType());
51
52
        $str = '     * @param %s %s';
53
54
        return sprintf($str, $paramType, $this->getVariableName());
55
    }
56
57
    /**
58
     * Returns the name of the class linked to this property or null if this is not a foreign key.
59
     *
60
     * @return null|string
61
     */
62
    public function getClassName()
63
    {
64
        return;
65
    }
66
67
    /**
68
     * Returns true if the property is compulsory (and therefore should be fetched in the constructor).
69
     *
70
     * @return bool
71
     */
72
    public function isCompulsory()
73
    {
74
        return $this->column->getNotnull() && !$this->column->getAutoincrement() && $this->column->getDefault() === null;
75
    }
76
77
    /**
78
     * Returns true if the property has a default value.
79
     *
80
     * @return bool
81
     */
82
    public function hasDefault()
83
    {
84
        return $this->column->getDefault() !== null;
85
    }
86
87
    /**
88
     * Returns the code that assigns a value to its default value.
89
     *
90
     * @return string
91
     */
92
    public function assignToDefaultCode()
93
    {
94
        $str = '        $this->%s(%s);';
95
96
        $default = $this->column->getDefault();
97
98
        if (strtoupper($default) === 'CURRENT_TIMESTAMP') {
99
            $defaultCode = 'new \DateTimeImmutable()';
100
        } else {
101
            $defaultCode = var_export($this->column->getDefault(), true);
102
        }
103
104
        return sprintf($str, $this->getSetterName(), $defaultCode);
105
    }
106
107
    /**
108
     * Returns true if the property is the primary key.
109
     *
110
     * @return bool
111
     */
112
    public function isPrimaryKey()
113
    {
114
        return in_array($this->column->getName(), $this->table->getPrimaryKeyColumns());
115
    }
116
117
    /**
118
     * Returns the PHP code for getters and setters.
119
     *
120
     * @return string
121
     */
122
    public function getGetterSetterCode()
123
    {
124
        $type = $this->column->getType();
125
        $normalizedType = TDBMDaoGenerator::dbalTypeToPhpType($type);
126
127
        $columnGetterName = $this->getGetterName();
128
        $columnSetterName = $this->getSetterName();
129
130
        // A column type can be forced if it is not nullable and not auto-incrementable (for auto-increment columns, we can get "null" as long as the bean is not saved).
131
        $isNullable = !$this->column->getNotnull() || $this->column->getAutoincrement();
132
133
        $getterAndSetterCode = '    /**
134
     * The getter for the "%s" column.
135
     *
136
     * @return %s
137
     */
138
    public function %s() : %s%s
139
    {
140
        return $this->get(%s, %s);
141
    }
142
143
    /**
144
     * The setter for the "%s" column.
145
     *
146
     * @param %s $%s
147
     */
148
    public function %s(%s%s $%s) : void
149
    {
150
        $this->set(%s, $%s, %s);
151
    }
152
153
';
154
155
        return sprintf($getterAndSetterCode,
156
            // Getter
157
            $this->column->getName(),
158
            $normalizedType.($isNullable ? '|null' : ''),
159
            $columnGetterName,
160
            ($isNullable ? '?' : ''),
161
            $normalizedType,
162
            var_export($this->column->getName(), true),
163
            var_export($this->table->getName(), true),
164
            // Setter
165
            $this->column->getName(),
166
            $normalizedType,
167
            $this->column->getName(),
168
            $columnSetterName,
169
            $this->column->getNotnull() ? '' : '?',
170
            $normalizedType,
171
                //$castTo,
172
            $this->column->getName(),
173
            var_export($this->column->getName(), true),
174
            $this->column->getName(),
175
            var_export($this->table->getName(), true)
176
        );
177
    }
178
179
    /**
180
     * Returns the part of code useful when doing json serialization.
181
     *
182
     * @return string
183
     */
184
    public function getJsonSerializeCode()
185
    {
186
        $type = $this->column->getType();
187
        $normalizedType = TDBMDaoGenerator::dbalTypeToPhpType($type);
188
189
        if ($normalizedType == '\\DateTimeInterface') {
190
            return '        $array['.var_export($this->namingStrategy->getJsonProperty($this), true).'] = ($this->'.$this->getGetterName().'() === null) ? null : $this->'.$this->getGetterName()."()->format('c');\n";
191
        } else {
192
            return '        $array['.var_export($this->namingStrategy->getJsonProperty($this), true).'] = $this->'.$this->getGetterName()."();\n";
193
        }
194
    }
195
196
    /**
197
     * Returns the column name.
198
     *
199
     * @return string
200
     */
201
    public function getColumnName()
202
    {
203
        return $this->column->getName();
204
    }
205
}
206