ScalarBeanPropertyDescriptor::isCompulsory()   A
last analyzed

Complexity

Conditions 3
Paths 3

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