Completed
Pull Request — 4.0 (#66)
by David
05:19
created

assignToDefaultCode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
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
use Mouf\Database\SchemaAnalyzer\SchemaAnalyzer;
9
10
/**
11
 * This class represent a property in a bean that points to another table.
12
 */
13
class ObjectBeanPropertyDescriptor extends AbstractBeanPropertyDescriptor
14
{
15
    /**
16
     * @var ForeignKeyConstraint
17
     */
18
    private $foreignKey;
19
20
    /**
21
     * @var SchemaAnalyzer
22
     */
23
    private $schemaAnalyzer;
24
25
    public function __construct(Table $table, ForeignKeyConstraint $foreignKey, SchemaAnalyzer $schemaAnalyzer)
26
    {
27
        parent::__construct($table);
28
        $this->foreignKey = $foreignKey;
29
        $this->schemaAnalyzer = $schemaAnalyzer;
30
    }
31
32
    /**
33
     * Returns the foreignkey the column is part of, if any. null otherwise.
34
     *
35
     * @return ForeignKeyConstraint|null
36
     */
37
    public function getForeignKey()
38
    {
39
        return $this->foreignKey;
40
    }
41
42
    /**
43
     * Returns the name of the class linked to this property or null if this is not a foreign key.
44
     *
45
     * @return null|string
46
     */
47
    public function getClassName()
48
    {
49
        return TDBMDaoGenerator::getBeanNameFromTableName($this->foreignKey->getForeignTableName());
50
    }
51
52
    /**
53
     * Returns the param annotation for this property (useful for constructor).
54
     *
55
     * @return string
56
     */
57
    public function getParamAnnotation()
58
    {
59
        $str = '     * @param %s %s';
60
61
        return sprintf($str, $this->getClassName(), $this->getVariableName());
62
    }
63
64
    public function getUpperCamelCaseName()
65
    {
66
        // First, are there many column or only one?
67
        // If one column, we name the setter after it. Otherwise, we name the setter after the table name
68
        if (count($this->foreignKey->getLocalColumns()) > 1) {
69
            $name = TDBMDaoGenerator::toSingular(TDBMDaoGenerator::toCamelCase($this->foreignKey->getForeignTableName()));
70
            if ($this->alternativeName) {
71
                $camelizedColumns = array_map(['Mouf\\Database\\TDBM\\Utils\\TDBMDaoGenerator', 'toCamelCase'], $this->foreignKey->getLocalColumns());
72
73
                $name .= 'By'.implode('And', $camelizedColumns);
74
            }
75
        } else {
76
            $column = $this->foreignKey->getLocalColumns()[0];
77
            // Let's remove any _id or id_.
78
            if (strpos(strtolower($column), 'id_') === 0) {
79
                $column = substr($column, 3);
80
            }
81
            if (strrpos(strtolower($column), '_id') === strlen($column) - 3) {
82
                $column = substr($column, 0, strlen($column) - 3);
83
            }
84
            $name = TDBMDaoGenerator::toCamelCase($column);
85
            if ($this->alternativeName) {
86
                $name .= 'Object';
87
            }
88
        }
89
90
        return $name;
91
    }
92
93
    /**
94
     * Returns true if the property is compulsory (and therefore should be fetched in the constructor).
95
     *
96
     * @return bool
97
     */
98
    public function isCompulsory()
99
    {
100
        // Are all columns nullable?
101
        $localColumnNames = $this->foreignKey->getLocalColumns();
102
103
        foreach ($localColumnNames as $name) {
104
            $column = $this->table->getColumn($name);
105
            if ($column->getNotnull()) {
106
                return true;
107
            }
108
        }
109
110
        return false;
111
    }
112
113
    /**
114
     * Returns true if the property has a default value.
115
     *
116
     * @return bool
117
     */
118
    public function hasDefault()
119
    {
120
        return false;
121
    }
122
123
    /**
124
     * Returns the code that assigns a value to its default value.
125
     *
126
     * @return string
127
     *
128
     * @throws \TDBMException
129
     */
130
    public function assignToDefaultCode()
131
    {
132
        throw new \TDBMException('Foreign key based properties cannot be assigned a default value.');
133
    }
134
135
    /**
136
     * Returns true if the property is the primary key.
137
     *
138
     * @return bool
139
     */
140
    public function isPrimaryKey()
141
    {
142
        $fkColumns = $this->foreignKey->getLocalColumns();
143
        sort($fkColumns);
144
145
        $pkColumns = $this->table->getPrimaryKeyColumns();
146
        sort($pkColumns);
147
148
        return $fkColumns == $pkColumns;
149
    }
150
151
    /**
152
     * Returns the PHP code for getters and setters.
153
     *
154
     * @return string
155
     */
156
    public function getGetterSetterCode()
157
    {
158
        $tableName = $this->table->getName();
159
        $getterName = $this->getGetterName();
160
        $setterName = $this->getSetterName();
161
162
        $referencedBeanName = TDBMDaoGenerator::getBeanNameFromTableName($this->foreignKey->getForeignTableName());
163
164
        $str = '    /**
165
     * Returns the '.$referencedBeanName.' object bound to this object via the '.implode(' and ', $this->foreignKey->getLocalColumns()).' column.
166
     *
167
     * @return '.$referencedBeanName.'
168
     */
169
    public function '.$getterName.'() {
170
        return $this->getRef('.var_export($this->foreignKey->getName(), true).', '.var_export($tableName, true).');
171
    }
172
173
    /**
174
     * The setter for the '.$referencedBeanName.' object bound to this object via the '.implode(' and ', $this->foreignKey->getLocalColumns()).' column.
175
     *
176
     * @param '.$referencedBeanName.' $object
177
     */
178
    public function '.$setterName.'('.$referencedBeanName.' $object = null) {
179
        $this->setRef('.var_export($this->foreignKey->getName(), true).', $object, '.var_export($tableName, true).');
180
    }
181
182
';
183
184
        return $str;
185
    }
186
187
    /**
188
     * Returns the part of code useful when doing json serialization.
189
     *
190
     * @return string
191
     */
192
    public function getJsonSerializeCode()
193
    {
194
        return '        if (!$stopRecursion) {
195
            $array['.var_export($this->getLowerCamelCaseName(), true).'] = $this->'.$this->getGetterName().'()->jsonSerialize(true);
196
        }
197
';
198
    }
199
}
200