Completed
Push — 4.2 ( 5e62fc...1584c5 )
by David
02:35
created
src/Mouf/Database/TDBM/Utils/ScalarBeanPropertyDescriptor.php 1 patch
Indentation   +167 added lines, -167 removed lines patch added patch discarded remove patch
@@ -11,125 +11,125 @@  discard block
 block discarded – undo
11 11
  */
12 12
 class ScalarBeanPropertyDescriptor extends AbstractBeanPropertyDescriptor
13 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 = '    /**
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 133
      * The getter for the "%s" column.
134 134
      *
135 135
      * @return %s
@@ -151,52 +151,52 @@  discard block
 block discarded – undo
151 151
 
152 152
 ';
153 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
-    }
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 202
 }
Please login to merge, or discard this patch.
src/Mouf/Database/TDBM/QueryFactory/AbstractQueryFactory.php 1 patch
Indentation   +210 added lines, -210 removed lines patch added patch discarded remove patch
@@ -10,214 +10,214 @@
 block discarded – undo
10 10
 
11 11
 abstract class AbstractQueryFactory implements QueryFactory
12 12
 {
13
-    /**
14
-     * @var TDBMService
15
-     */
16
-    protected $tdbmService;
17
-
18
-    /**
19
-     * @var Schema
20
-     */
21
-    protected $schema;
22
-
23
-    /**
24
-     * @var OrderByAnalyzer
25
-     */
26
-    protected $orderByAnalyzer;
27
-
28
-    /**
29
-     * @var string|UncheckedOrderBy|null
30
-     */
31
-    protected $orderBy;
32
-
33
-    protected $magicSql;
34
-    protected $magicSqlCount;
35
-    protected $columnDescList;
36
-
37
-    /**
38
-     * @param TDBMService $tdbmService
39
-     */
40
-    public function __construct(TDBMService $tdbmService, Schema $schema, OrderByAnalyzer $orderByAnalyzer, $orderBy)
41
-    {
42
-        $this->tdbmService = $tdbmService;
43
-        $this->schema = $schema;
44
-        $this->orderByAnalyzer = $orderByAnalyzer;
45
-        $this->orderBy = $orderBy;
46
-    }
47
-
48
-    /**
49
-     * Returns the column list that must be fetched for the SQL request.
50
-     *
51
-     * Note: MySQL dictates that ORDER BYed columns should appear in the SELECT clause.
52
-     *
53
-     * @param string                       $mainTable
54
-     * @param array                        $additionalTablesFetch
55
-     * @param string|UncheckedOrderBy|null $orderBy
56
-     *
57
-     * @return array
58
-     *
59
-     * @throws \Doctrine\DBAL\Schema\SchemaException
60
-     */
61
-    protected function getColumnsList(string $mainTable, array $additionalTablesFetch = array(), $orderBy = null)
62
-    {
63
-        // From the table name and the additional tables we want to fetch, let's build a list of all tables
64
-        // that must be part of the select columns.
65
-
66
-        $connection = $this->tdbmService->getConnection();
67
-
68
-        $tableGroups = [];
69
-        $allFetchedTables = $this->tdbmService->_getRelatedTablesByInheritance($mainTable);
70
-        $tableGroupName = $this->getTableGroupName($allFetchedTables);
71
-        foreach ($allFetchedTables as $table) {
72
-            $tableGroups[$table] = $tableGroupName;
73
-        }
74
-
75
-        $columnsList = [];
76
-        $columnDescList = [];
77
-        $sortColumn = 0;
78
-        $reconstructedOrderBy = null;
79
-
80
-        if (is_string($orderBy)) {
81
-            $orderBy = trim($orderBy);
82
-            if ($orderBy === '') {
83
-                $orderBy = null;
84
-            }
85
-        }
86
-
87
-        // Now, let's deal with "order by columns"
88
-        if ($orderBy !== null) {
89
-            if ($orderBy instanceof UncheckedOrderBy) {
90
-                $securedOrderBy = false;
91
-                $orderBy = $orderBy->getOrderBy();
92
-                $reconstructedOrderBy = $orderBy;
93
-            } else {
94
-                $securedOrderBy = true;
95
-                $reconstructedOrderBys = [];
96
-            }
97
-            $orderByColumns = $this->orderByAnalyzer->analyzeOrderBy($orderBy);
98
-
99
-            // If we sort by a column, there is a high chance we will fetch the bean containing this column.
100
-            // Hence, we should add the table to the $additionalTablesFetch
101
-            foreach ($orderByColumns as $orderByColumn) {
102
-                if ($orderByColumn['type'] === 'colref') {
103
-                    if ($orderByColumn['table'] !== null) {
104
-                        $additionalTablesFetch[] = $orderByColumn['table'];
105
-                    }
106
-                    if ($securedOrderBy) {
107
-                        $reconstructedOrderBys[] = ($orderByColumn['table'] !== null ? $connection->quoteIdentifier($orderByColumn['table']).'.' : '').$connection->quoteIdentifier($orderByColumn['column']).' '.$orderByColumn['direction'];
108
-                    }
109
-                } elseif ($orderByColumn['type'] === 'expr') {
110
-                    $sortColumnName = 'sort_column_'.$sortColumn;
111
-                    $columnsList[] = $orderByColumn['expr'].' as '.$sortColumnName;
112
-                    $columnDescList[] = [
113
-                        'tableGroup' => null,
114
-                    ];
115
-                    ++$sortColumn;
116
-
117
-                    if ($securedOrderBy) {
118
-                        throw new TDBMInvalidArgumentException('Invalid ORDER BY column: "'.$orderByColumn['expr'].'". If you want to use expression in your ORDER BY clause, you must wrap them in a UncheckedOrderBy object. For instance: new UncheckedOrderBy("col1 + col2 DESC")');
119
-                    }
120
-                }
121
-            }
122
-
123
-            if ($reconstructedOrderBy === null) {
124
-                $reconstructedOrderBy = implode(', ', $reconstructedOrderBys);
125
-            }
126
-        }
127
-
128
-        foreach ($additionalTablesFetch as $additionalTable) {
129
-            $relatedTables = $this->tdbmService->_getRelatedTablesByInheritance($additionalTable);
130
-            $tableGroupName = $this->getTableGroupName($relatedTables);
131
-            foreach ($relatedTables as $table) {
132
-                $tableGroups[$table] = $tableGroupName;
133
-            }
134
-            $allFetchedTables = array_merge($allFetchedTables, $relatedTables);
135
-        }
136
-
137
-        // Let's remove any duplicate
138
-        $allFetchedTables = array_flip(array_flip($allFetchedTables));
139
-
140
-        // Now, let's build the column list
141
-        foreach ($allFetchedTables as $table) {
142
-            foreach ($this->schema->getTable($table)->getColumns() as $column) {
143
-                $columnName = $column->getName();
144
-                $columnDescList[] = [
145
-                    'as' => $table.'____'.$columnName,
146
-                    'table' => $table,
147
-                    'column' => $columnName,
148
-                    'type' => $column->getType(),
149
-                    'tableGroup' => $tableGroups[$table],
150
-                ];
151
-                $columnsList[] = $connection->quoteIdentifier($table).'.'.$connection->quoteIdentifier($columnName).' as '.
152
-                    $connection->quoteIdentifier($table.'____'.$columnName);
153
-            }
154
-        }
155
-
156
-        return [$columnDescList, $columnsList, $reconstructedOrderBy];
157
-    }
158
-
159
-    abstract protected function compute();
160
-
161
-    /**
162
-     * Returns an identifier for the group of tables passed in parameter.
163
-     *
164
-     * @param string[] $relatedTables
165
-     *
166
-     * @return string
167
-     */
168
-    protected function getTableGroupName(array $relatedTables)
169
-    {
170
-        sort($relatedTables);
171
-
172
-        return implode('_``_', $relatedTables);
173
-    }
174
-
175
-    public function getMagicSql() : string
176
-    {
177
-        if ($this->magicSql === null) {
178
-            $this->compute();
179
-        }
180
-
181
-        return $this->magicSql;
182
-    }
183
-
184
-    public function getMagicSqlCount() : string
185
-    {
186
-        if ($this->magicSqlCount === null) {
187
-            $this->compute();
188
-        }
189
-
190
-        return $this->magicSqlCount;
191
-    }
192
-
193
-    public function getColumnDescriptors() : array
194
-    {
195
-        if ($this->columnDescList === null) {
196
-            $this->compute();
197
-        }
198
-
199
-        return $this->columnDescList;
200
-    }
201
-
202
-    /**
203
-     * Sets the ORDER BY directive executed in SQL.
204
-     *
205
-     * For instance:
206
-     *
207
-     *  $queryFactory->sort('label ASC, status DESC');
208
-     *
209
-     * **Important:** TDBM does its best to protect you from SQL injection. In particular, it will only allow column names in the "ORDER BY" clause. This means you are safe to pass input from the user directly in the ORDER BY parameter.
210
-     * If you want to pass an expression to the ORDER BY clause, you will need to tell TDBM to stop checking for SQL injections. You do this by passing a `UncheckedOrderBy` object as a parameter:
211
-     *
212
-     *  $queryFactory->sort(new UncheckedOrderBy('RAND()'))
213
-     *
214
-     * @param string|UncheckedOrderBy|null $orderBy
215
-     */
216
-    public function sort($orderBy)
217
-    {
218
-        $this->orderBy = $orderBy;
219
-        $this->magicSql = null;
220
-        $this->magicSqlCount = null;
221
-        $this->columnDescList = null;
222
-    }
13
+	/**
14
+	 * @var TDBMService
15
+	 */
16
+	protected $tdbmService;
17
+
18
+	/**
19
+	 * @var Schema
20
+	 */
21
+	protected $schema;
22
+
23
+	/**
24
+	 * @var OrderByAnalyzer
25
+	 */
26
+	protected $orderByAnalyzer;
27
+
28
+	/**
29
+	 * @var string|UncheckedOrderBy|null
30
+	 */
31
+	protected $orderBy;
32
+
33
+	protected $magicSql;
34
+	protected $magicSqlCount;
35
+	protected $columnDescList;
36
+
37
+	/**
38
+	 * @param TDBMService $tdbmService
39
+	 */
40
+	public function __construct(TDBMService $tdbmService, Schema $schema, OrderByAnalyzer $orderByAnalyzer, $orderBy)
41
+	{
42
+		$this->tdbmService = $tdbmService;
43
+		$this->schema = $schema;
44
+		$this->orderByAnalyzer = $orderByAnalyzer;
45
+		$this->orderBy = $orderBy;
46
+	}
47
+
48
+	/**
49
+	 * Returns the column list that must be fetched for the SQL request.
50
+	 *
51
+	 * Note: MySQL dictates that ORDER BYed columns should appear in the SELECT clause.
52
+	 *
53
+	 * @param string                       $mainTable
54
+	 * @param array                        $additionalTablesFetch
55
+	 * @param string|UncheckedOrderBy|null $orderBy
56
+	 *
57
+	 * @return array
58
+	 *
59
+	 * @throws \Doctrine\DBAL\Schema\SchemaException
60
+	 */
61
+	protected function getColumnsList(string $mainTable, array $additionalTablesFetch = array(), $orderBy = null)
62
+	{
63
+		// From the table name and the additional tables we want to fetch, let's build a list of all tables
64
+		// that must be part of the select columns.
65
+
66
+		$connection = $this->tdbmService->getConnection();
67
+
68
+		$tableGroups = [];
69
+		$allFetchedTables = $this->tdbmService->_getRelatedTablesByInheritance($mainTable);
70
+		$tableGroupName = $this->getTableGroupName($allFetchedTables);
71
+		foreach ($allFetchedTables as $table) {
72
+			$tableGroups[$table] = $tableGroupName;
73
+		}
74
+
75
+		$columnsList = [];
76
+		$columnDescList = [];
77
+		$sortColumn = 0;
78
+		$reconstructedOrderBy = null;
79
+
80
+		if (is_string($orderBy)) {
81
+			$orderBy = trim($orderBy);
82
+			if ($orderBy === '') {
83
+				$orderBy = null;
84
+			}
85
+		}
86
+
87
+		// Now, let's deal with "order by columns"
88
+		if ($orderBy !== null) {
89
+			if ($orderBy instanceof UncheckedOrderBy) {
90
+				$securedOrderBy = false;
91
+				$orderBy = $orderBy->getOrderBy();
92
+				$reconstructedOrderBy = $orderBy;
93
+			} else {
94
+				$securedOrderBy = true;
95
+				$reconstructedOrderBys = [];
96
+			}
97
+			$orderByColumns = $this->orderByAnalyzer->analyzeOrderBy($orderBy);
98
+
99
+			// If we sort by a column, there is a high chance we will fetch the bean containing this column.
100
+			// Hence, we should add the table to the $additionalTablesFetch
101
+			foreach ($orderByColumns as $orderByColumn) {
102
+				if ($orderByColumn['type'] === 'colref') {
103
+					if ($orderByColumn['table'] !== null) {
104
+						$additionalTablesFetch[] = $orderByColumn['table'];
105
+					}
106
+					if ($securedOrderBy) {
107
+						$reconstructedOrderBys[] = ($orderByColumn['table'] !== null ? $connection->quoteIdentifier($orderByColumn['table']).'.' : '').$connection->quoteIdentifier($orderByColumn['column']).' '.$orderByColumn['direction'];
108
+					}
109
+				} elseif ($orderByColumn['type'] === 'expr') {
110
+					$sortColumnName = 'sort_column_'.$sortColumn;
111
+					$columnsList[] = $orderByColumn['expr'].' as '.$sortColumnName;
112
+					$columnDescList[] = [
113
+						'tableGroup' => null,
114
+					];
115
+					++$sortColumn;
116
+
117
+					if ($securedOrderBy) {
118
+						throw new TDBMInvalidArgumentException('Invalid ORDER BY column: "'.$orderByColumn['expr'].'". If you want to use expression in your ORDER BY clause, you must wrap them in a UncheckedOrderBy object. For instance: new UncheckedOrderBy("col1 + col2 DESC")');
119
+					}
120
+				}
121
+			}
122
+
123
+			if ($reconstructedOrderBy === null) {
124
+				$reconstructedOrderBy = implode(', ', $reconstructedOrderBys);
125
+			}
126
+		}
127
+
128
+		foreach ($additionalTablesFetch as $additionalTable) {
129
+			$relatedTables = $this->tdbmService->_getRelatedTablesByInheritance($additionalTable);
130
+			$tableGroupName = $this->getTableGroupName($relatedTables);
131
+			foreach ($relatedTables as $table) {
132
+				$tableGroups[$table] = $tableGroupName;
133
+			}
134
+			$allFetchedTables = array_merge($allFetchedTables, $relatedTables);
135
+		}
136
+
137
+		// Let's remove any duplicate
138
+		$allFetchedTables = array_flip(array_flip($allFetchedTables));
139
+
140
+		// Now, let's build the column list
141
+		foreach ($allFetchedTables as $table) {
142
+			foreach ($this->schema->getTable($table)->getColumns() as $column) {
143
+				$columnName = $column->getName();
144
+				$columnDescList[] = [
145
+					'as' => $table.'____'.$columnName,
146
+					'table' => $table,
147
+					'column' => $columnName,
148
+					'type' => $column->getType(),
149
+					'tableGroup' => $tableGroups[$table],
150
+				];
151
+				$columnsList[] = $connection->quoteIdentifier($table).'.'.$connection->quoteIdentifier($columnName).' as '.
152
+					$connection->quoteIdentifier($table.'____'.$columnName);
153
+			}
154
+		}
155
+
156
+		return [$columnDescList, $columnsList, $reconstructedOrderBy];
157
+	}
158
+
159
+	abstract protected function compute();
160
+
161
+	/**
162
+	 * Returns an identifier for the group of tables passed in parameter.
163
+	 *
164
+	 * @param string[] $relatedTables
165
+	 *
166
+	 * @return string
167
+	 */
168
+	protected function getTableGroupName(array $relatedTables)
169
+	{
170
+		sort($relatedTables);
171
+
172
+		return implode('_``_', $relatedTables);
173
+	}
174
+
175
+	public function getMagicSql() : string
176
+	{
177
+		if ($this->magicSql === null) {
178
+			$this->compute();
179
+		}
180
+
181
+		return $this->magicSql;
182
+	}
183
+
184
+	public function getMagicSqlCount() : string
185
+	{
186
+		if ($this->magicSqlCount === null) {
187
+			$this->compute();
188
+		}
189
+
190
+		return $this->magicSqlCount;
191
+	}
192
+
193
+	public function getColumnDescriptors() : array
194
+	{
195
+		if ($this->columnDescList === null) {
196
+			$this->compute();
197
+		}
198
+
199
+		return $this->columnDescList;
200
+	}
201
+
202
+	/**
203
+	 * Sets the ORDER BY directive executed in SQL.
204
+	 *
205
+	 * For instance:
206
+	 *
207
+	 *  $queryFactory->sort('label ASC, status DESC');
208
+	 *
209
+	 * **Important:** TDBM does its best to protect you from SQL injection. In particular, it will only allow column names in the "ORDER BY" clause. This means you are safe to pass input from the user directly in the ORDER BY parameter.
210
+	 * If you want to pass an expression to the ORDER BY clause, you will need to tell TDBM to stop checking for SQL injections. You do this by passing a `UncheckedOrderBy` object as a parameter:
211
+	 *
212
+	 *  $queryFactory->sort(new UncheckedOrderBy('RAND()'))
213
+	 *
214
+	 * @param string|UncheckedOrderBy|null $orderBy
215
+	 */
216
+	public function sort($orderBy)
217
+	{
218
+		$this->orderBy = $orderBy;
219
+		$this->magicSql = null;
220
+		$this->magicSqlCount = null;
221
+		$this->columnDescList = null;
222
+	}
223 223
 }
Please login to merge, or discard this patch.
src/Mouf/Database/TDBM/QueryFactory/FindObjectsFromSqlQueryFactory.php 1 patch
Indentation   +187 added lines, -187 removed lines patch added patch discarded remove patch
@@ -15,191 +15,191 @@
 block discarded – undo
15 15
  */
16 16
 class FindObjectsFromSqlQueryFactory extends AbstractQueryFactory
17 17
 {
18
-    private $mainTable;
19
-    private $from;
20
-    private $filterString;
21
-    private $cache;
22
-    private $cachePrefix;
23
-
24
-    public function __construct(string $mainTable, string $from, $filterString, $orderBy, TDBMService $tdbmService, Schema $schema, OrderByAnalyzer $orderByAnalyzer, SchemaAnalyzer $schemaAnalyzer, Cache $cache, string $cachePrefix)
25
-    {
26
-        parent::__construct($tdbmService, $schema, $orderByAnalyzer, $orderBy);
27
-        $this->mainTable = $mainTable;
28
-        $this->from = $from;
29
-        $this->filterString = $filterString;
30
-        $this->schemaAnalyzer = $schemaAnalyzer;
31
-        $this->cache = $cache;
32
-        $this->cachePrefix = $cachePrefix;
33
-    }
34
-
35
-    protected function compute()
36
-    {
37
-        $connection = $this->tdbmService->getConnection();
38
-
39
-        $columnsList = null;
40
-
41
-        $allFetchedTables = $this->tdbmService->_getRelatedTablesByInheritance($this->mainTable);
42
-
43
-        $columnDescList = [];
44
-
45
-        $tableGroupName = $this->getTableGroupName($allFetchedTables);
46
-
47
-        foreach ($this->schema->getTable($this->mainTable)->getColumns() as $column) {
48
-            $columnName = $column->getName();
49
-            $columnDescList[] = [
50
-                'as' => $columnName,
51
-                'table' => $this->mainTable,
52
-                'column' => $columnName,
53
-                'type' => $column->getType(),
54
-                'tableGroup' => $tableGroupName,
55
-            ];
56
-        }
57
-
58
-        $sql = 'SELECT DISTINCT '.implode(', ', array_map(function ($columnDesc) {
59
-            return $this->tdbmService->getConnection()->quoteIdentifier($this->mainTable).'.'.$this->tdbmService->getConnection()->quoteIdentifier($columnDesc['column']);
60
-        }, $columnDescList)).' FROM '.$this->from;
61
-
62
-        if (count($allFetchedTables) > 1) {
63
-            list($columnDescList, $columnsList, $orderString) = $this->getColumnsList($this->mainTable, [], $this->orderBy);
64
-        } elseif ($this->orderBy) {
65
-            list(, , $orderString) = $this->getColumnsList($this->mainTable, [], $this->orderBy);
66
-        }
67
-
68
-        // Let's compute the COUNT.
69
-        $pkColumnNames = $this->schema->getTable($this->mainTable)->getPrimaryKeyColumns();
70
-        $pkColumnNames = array_map(function ($pkColumn) {
71
-            return $this->tdbmService->getConnection()->quoteIdentifier($this->mainTable).'.'.$this->tdbmService->getConnection()->quoteIdentifier($pkColumn);
72
-        }, $pkColumnNames);
73
-
74
-        $countSql = 'SELECT COUNT(DISTINCT '.implode(', ', $pkColumnNames).') FROM '.$this->from;
75
-
76
-        if (!empty($this->filterString)) {
77
-            $sql .= ' WHERE '.$this->filterString;
78
-            $countSql .= ' WHERE '.$this->filterString;
79
-        }
80
-
81
-        if (!empty($orderString)) {
82
-            $sql .= ' ORDER BY '.$orderString;
83
-        }
84
-
85
-        if (stripos($countSql, 'GROUP BY') !== false) {
86
-            throw new TDBMException('Unsupported use of GROUP BY in SQL request.');
87
-        }
88
-
89
-        if ($columnsList !== null) {
90
-            $joinSql = '';
91
-            $parentFks = $this->getParentRelationshipForeignKeys($this->mainTable);
92
-            foreach ($parentFks as $fk) {
93
-                $joinSql .= sprintf(' JOIN %s ON (%s.%s = %s.%s)',
94
-                    $connection->quoteIdentifier($fk->getForeignTableName()),
95
-                    $connection->quoteIdentifier($fk->getLocalTableName()),
96
-                    $connection->quoteIdentifier($fk->getLocalColumns()[0]),
97
-                    $connection->quoteIdentifier($fk->getForeignTableName()),
98
-                    $connection->quoteIdentifier($fk->getForeignColumns()[0])
99
-                );
100
-            }
101
-
102
-            $childrenFks = $this->getChildrenRelationshipForeignKeys($this->mainTable);
103
-            foreach ($childrenFks as $fk) {
104
-                $joinSql .= sprintf(' LEFT JOIN %s ON (%s.%s = %s.%s)',
105
-                    $connection->quoteIdentifier($fk->getLocalTableName()),
106
-                    $connection->quoteIdentifier($fk->getForeignTableName()),
107
-                    $connection->quoteIdentifier($fk->getForeignColumns()[0]),
108
-                    $connection->quoteIdentifier($fk->getLocalTableName()),
109
-                    $connection->quoteIdentifier($fk->getLocalColumns()[0])
110
-                );
111
-            }
112
-
113
-            $sql = 'SELECT '.implode(', ', $columnsList).' FROM ('.$sql.') AS '.$this->mainTable.' '.$joinSql;
114
-            if (!empty($orderString)) {
115
-                $sql .= ' ORDER BY '.$orderString;
116
-            }
117
-        }
118
-
119
-        $this->magicSql = $sql;
120
-        $this->magicSqlCount = $countSql;
121
-        $this->columnDescList = $columnDescList;
122
-    }
123
-
124
-    /**
125
-     * @param string $tableName
126
-     *
127
-     * @return ForeignKeyConstraint[]
128
-     */
129
-    private function getParentRelationshipForeignKeys($tableName)
130
-    {
131
-        return $this->fromCache($this->cachePrefix.'_parentrelationshipfks_'.$tableName, function () use ($tableName) {
132
-            return $this->getParentRelationshipForeignKeysWithoutCache($tableName);
133
-        });
134
-    }
135
-
136
-    /**
137
-     * @param string $tableName
138
-     *
139
-     * @return ForeignKeyConstraint[]
140
-     */
141
-    private function getParentRelationshipForeignKeysWithoutCache($tableName)
142
-    {
143
-        $parentFks = [];
144
-        $currentTable = $tableName;
145
-        while ($currentFk = $this->schemaAnalyzer->getParentRelationship($currentTable)) {
146
-            $currentTable = $currentFk->getForeignTableName();
147
-            $parentFks[] = $currentFk;
148
-        }
149
-
150
-        return $parentFks;
151
-    }
152
-
153
-    /**
154
-     * @param string $tableName
155
-     *
156
-     * @return ForeignKeyConstraint[]
157
-     */
158
-    private function getChildrenRelationshipForeignKeys(string $tableName) : array
159
-    {
160
-        return $this->fromCache($this->cachePrefix.'_childrenrelationshipfks_'.$tableName, function () use ($tableName) {
161
-            return $this->getChildrenRelationshipForeignKeysWithoutCache($tableName);
162
-        });
163
-    }
164
-
165
-    /**
166
-     * @param string $tableName
167
-     *
168
-     * @return ForeignKeyConstraint[]
169
-     */
170
-    private function getChildrenRelationshipForeignKeysWithoutCache(string $tableName) : array
171
-    {
172
-        $children = $this->schemaAnalyzer->getChildrenRelationships($tableName);
173
-
174
-        if (!empty($children)) {
175
-            $fksTables = array_map(function (ForeignKeyConstraint $fk) {
176
-                return $this->getChildrenRelationshipForeignKeys($fk->getLocalTableName());
177
-            }, $children);
178
-
179
-            $fks = array_merge($children, call_user_func_array('array_merge', $fksTables));
180
-
181
-            return $fks;
182
-        } else {
183
-            return [];
184
-        }
185
-    }
186
-
187
-    /**
188
-     * Returns an item from cache or computes it using $closure and puts it in cache.
189
-     *
190
-     * @param string   $key
191
-     * @param callable $closure
192
-     *
193
-     * @return mixed
194
-     */
195
-    protected function fromCache(string $key, callable $closure)
196
-    {
197
-        $item = $this->cache->fetch($key);
198
-        if ($item === false) {
199
-            $item = $closure();
200
-            $this->cache->save($key, $item);
201
-        }
202
-
203
-        return $item;
204
-    }
18
+	private $mainTable;
19
+	private $from;
20
+	private $filterString;
21
+	private $cache;
22
+	private $cachePrefix;
23
+
24
+	public function __construct(string $mainTable, string $from, $filterString, $orderBy, TDBMService $tdbmService, Schema $schema, OrderByAnalyzer $orderByAnalyzer, SchemaAnalyzer $schemaAnalyzer, Cache $cache, string $cachePrefix)
25
+	{
26
+		parent::__construct($tdbmService, $schema, $orderByAnalyzer, $orderBy);
27
+		$this->mainTable = $mainTable;
28
+		$this->from = $from;
29
+		$this->filterString = $filterString;
30
+		$this->schemaAnalyzer = $schemaAnalyzer;
31
+		$this->cache = $cache;
32
+		$this->cachePrefix = $cachePrefix;
33
+	}
34
+
35
+	protected function compute()
36
+	{
37
+		$connection = $this->tdbmService->getConnection();
38
+
39
+		$columnsList = null;
40
+
41
+		$allFetchedTables = $this->tdbmService->_getRelatedTablesByInheritance($this->mainTable);
42
+
43
+		$columnDescList = [];
44
+
45
+		$tableGroupName = $this->getTableGroupName($allFetchedTables);
46
+
47
+		foreach ($this->schema->getTable($this->mainTable)->getColumns() as $column) {
48
+			$columnName = $column->getName();
49
+			$columnDescList[] = [
50
+				'as' => $columnName,
51
+				'table' => $this->mainTable,
52
+				'column' => $columnName,
53
+				'type' => $column->getType(),
54
+				'tableGroup' => $tableGroupName,
55
+			];
56
+		}
57
+
58
+		$sql = 'SELECT DISTINCT '.implode(', ', array_map(function ($columnDesc) {
59
+			return $this->tdbmService->getConnection()->quoteIdentifier($this->mainTable).'.'.$this->tdbmService->getConnection()->quoteIdentifier($columnDesc['column']);
60
+		}, $columnDescList)).' FROM '.$this->from;
61
+
62
+		if (count($allFetchedTables) > 1) {
63
+			list($columnDescList, $columnsList, $orderString) = $this->getColumnsList($this->mainTable, [], $this->orderBy);
64
+		} elseif ($this->orderBy) {
65
+			list(, , $orderString) = $this->getColumnsList($this->mainTable, [], $this->orderBy);
66
+		}
67
+
68
+		// Let's compute the COUNT.
69
+		$pkColumnNames = $this->schema->getTable($this->mainTable)->getPrimaryKeyColumns();
70
+		$pkColumnNames = array_map(function ($pkColumn) {
71
+			return $this->tdbmService->getConnection()->quoteIdentifier($this->mainTable).'.'.$this->tdbmService->getConnection()->quoteIdentifier($pkColumn);
72
+		}, $pkColumnNames);
73
+
74
+		$countSql = 'SELECT COUNT(DISTINCT '.implode(', ', $pkColumnNames).') FROM '.$this->from;
75
+
76
+		if (!empty($this->filterString)) {
77
+			$sql .= ' WHERE '.$this->filterString;
78
+			$countSql .= ' WHERE '.$this->filterString;
79
+		}
80
+
81
+		if (!empty($orderString)) {
82
+			$sql .= ' ORDER BY '.$orderString;
83
+		}
84
+
85
+		if (stripos($countSql, 'GROUP BY') !== false) {
86
+			throw new TDBMException('Unsupported use of GROUP BY in SQL request.');
87
+		}
88
+
89
+		if ($columnsList !== null) {
90
+			$joinSql = '';
91
+			$parentFks = $this->getParentRelationshipForeignKeys($this->mainTable);
92
+			foreach ($parentFks as $fk) {
93
+				$joinSql .= sprintf(' JOIN %s ON (%s.%s = %s.%s)',
94
+					$connection->quoteIdentifier($fk->getForeignTableName()),
95
+					$connection->quoteIdentifier($fk->getLocalTableName()),
96
+					$connection->quoteIdentifier($fk->getLocalColumns()[0]),
97
+					$connection->quoteIdentifier($fk->getForeignTableName()),
98
+					$connection->quoteIdentifier($fk->getForeignColumns()[0])
99
+				);
100
+			}
101
+
102
+			$childrenFks = $this->getChildrenRelationshipForeignKeys($this->mainTable);
103
+			foreach ($childrenFks as $fk) {
104
+				$joinSql .= sprintf(' LEFT JOIN %s ON (%s.%s = %s.%s)',
105
+					$connection->quoteIdentifier($fk->getLocalTableName()),
106
+					$connection->quoteIdentifier($fk->getForeignTableName()),
107
+					$connection->quoteIdentifier($fk->getForeignColumns()[0]),
108
+					$connection->quoteIdentifier($fk->getLocalTableName()),
109
+					$connection->quoteIdentifier($fk->getLocalColumns()[0])
110
+				);
111
+			}
112
+
113
+			$sql = 'SELECT '.implode(', ', $columnsList).' FROM ('.$sql.') AS '.$this->mainTable.' '.$joinSql;
114
+			if (!empty($orderString)) {
115
+				$sql .= ' ORDER BY '.$orderString;
116
+			}
117
+		}
118
+
119
+		$this->magicSql = $sql;
120
+		$this->magicSqlCount = $countSql;
121
+		$this->columnDescList = $columnDescList;
122
+	}
123
+
124
+	/**
125
+	 * @param string $tableName
126
+	 *
127
+	 * @return ForeignKeyConstraint[]
128
+	 */
129
+	private function getParentRelationshipForeignKeys($tableName)
130
+	{
131
+		return $this->fromCache($this->cachePrefix.'_parentrelationshipfks_'.$tableName, function () use ($tableName) {
132
+			return $this->getParentRelationshipForeignKeysWithoutCache($tableName);
133
+		});
134
+	}
135
+
136
+	/**
137
+	 * @param string $tableName
138
+	 *
139
+	 * @return ForeignKeyConstraint[]
140
+	 */
141
+	private function getParentRelationshipForeignKeysWithoutCache($tableName)
142
+	{
143
+		$parentFks = [];
144
+		$currentTable = $tableName;
145
+		while ($currentFk = $this->schemaAnalyzer->getParentRelationship($currentTable)) {
146
+			$currentTable = $currentFk->getForeignTableName();
147
+			$parentFks[] = $currentFk;
148
+		}
149
+
150
+		return $parentFks;
151
+	}
152
+
153
+	/**
154
+	 * @param string $tableName
155
+	 *
156
+	 * @return ForeignKeyConstraint[]
157
+	 */
158
+	private function getChildrenRelationshipForeignKeys(string $tableName) : array
159
+	{
160
+		return $this->fromCache($this->cachePrefix.'_childrenrelationshipfks_'.$tableName, function () use ($tableName) {
161
+			return $this->getChildrenRelationshipForeignKeysWithoutCache($tableName);
162
+		});
163
+	}
164
+
165
+	/**
166
+	 * @param string $tableName
167
+	 *
168
+	 * @return ForeignKeyConstraint[]
169
+	 */
170
+	private function getChildrenRelationshipForeignKeysWithoutCache(string $tableName) : array
171
+	{
172
+		$children = $this->schemaAnalyzer->getChildrenRelationships($tableName);
173
+
174
+		if (!empty($children)) {
175
+			$fksTables = array_map(function (ForeignKeyConstraint $fk) {
176
+				return $this->getChildrenRelationshipForeignKeys($fk->getLocalTableName());
177
+			}, $children);
178
+
179
+			$fks = array_merge($children, call_user_func_array('array_merge', $fksTables));
180
+
181
+			return $fks;
182
+		} else {
183
+			return [];
184
+		}
185
+	}
186
+
187
+	/**
188
+	 * Returns an item from cache or computes it using $closure and puts it in cache.
189
+	 *
190
+	 * @param string   $key
191
+	 * @param callable $closure
192
+	 *
193
+	 * @return mixed
194
+	 */
195
+	protected function fromCache(string $key, callable $closure)
196
+	{
197
+		$item = $this->cache->fetch($key);
198
+		if ($item === false) {
199
+			$item = $closure();
200
+			$this->cache->save($key, $item);
201
+		}
202
+
203
+		return $item;
204
+	}
205 205
 }
Please login to merge, or discard this patch.
src/Mouf/Database/TDBM/AbstractTDBMObject.php 1 patch
Indentation   +606 added lines, -606 removed lines patch added patch discarded remove patch
@@ -31,615 +31,615 @@
 block discarded – undo
31 31
  */
32 32
 abstract class AbstractTDBMObject implements JsonSerializable
33 33
 {
34
-    /**
35
-     * The service this object is bound to.
36
-     *
37
-     * @var TDBMService
38
-     */
39
-    protected $tdbmService;
40
-
41
-    /**
42
-     * An array of DbRow, indexed by table name.
43
-     *
44
-     * @var DbRow[]
45
-     */
46
-    protected $dbRows = [];
47
-
48
-    /**
49
-     * One of TDBMObjectStateEnum::STATE_NEW, TDBMObjectStateEnum::STATE_NOT_LOADED, TDBMObjectStateEnum::STATE_LOADED, TDBMObjectStateEnum::STATE_DELETED.
50
-     * $status = TDBMObjectStateEnum::STATE_NEW when a new object is created with DBMObject:getNewObject.
51
-     * $status = TDBMObjectStateEnum::STATE_NOT_LOADED when the object has been retrieved with getObject but when no data has been accessed in it yet.
52
-     * $status = TDBMObjectStateEnum::STATE_LOADED when the object is cached in memory.
53
-     *
54
-     * @var string
55
-     */
56
-    private $status;
57
-
58
-    /**
59
-     * Array storing beans related via many to many relationships (pivot tables).
60
-     *
61
-     * @var \SplObjectStorage[] Key: pivot table name, value: SplObjectStorage
62
-     */
63
-    private $relationships = [];
64
-
65
-    /**
66
-     * @var bool[] Key: pivot table name, value: whether a query was performed to load the data
67
-     */
68
-    private $loadedRelationships = [];
69
-
70
-    /**
71
-     * Array storing beans related via many to one relationships (this bean is pointed by external beans).
72
-     *
73
-     * @var AlterableResultIterator[] Key: [external_table]___[external_column], value: SplObjectStorage
74
-     */
75
-    private $manyToOneRelationships = [];
76
-
77
-    /**
78
-     * Used with $primaryKeys when we want to retrieve an existing object
79
-     * and $primaryKeys=[] if we want a new object.
80
-     *
81
-     * @param string      $tableName
82
-     * @param array       $primaryKeys
83
-     * @param TDBMService $tdbmService
84
-     *
85
-     * @throws TDBMException
86
-     * @throws TDBMInvalidOperationException
87
-     */
88
-    public function __construct($tableName = null, array $primaryKeys = [], TDBMService $tdbmService = null)
89
-    {
90
-        // FIXME: lazy loading should be forbidden on tables with inheritance and dynamic type assignation...
91
-        if (!empty($tableName)) {
92
-            $this->dbRows[$tableName] = new DbRow($this, $tableName, $primaryKeys, $tdbmService);
93
-        }
94
-
95
-        if ($tdbmService === null) {
96
-            $this->_setStatus(TDBMObjectStateEnum::STATE_DETACHED);
97
-        } else {
98
-            $this->_attach($tdbmService);
99
-            if (!empty($primaryKeys)) {
100
-                $this->_setStatus(TDBMObjectStateEnum::STATE_NOT_LOADED);
101
-            } else {
102
-                $this->_setStatus(TDBMObjectStateEnum::STATE_NEW);
103
-            }
104
-        }
105
-    }
106
-
107
-    /**
108
-     * Alternative constructor called when data is fetched from database via a SELECT.
109
-     *
110
-     * @param array       $beanData    array<table, array<column, value>>
111
-     * @param TDBMService $tdbmService
112
-     */
113
-    public function _constructFromData(array $beanData, TDBMService $tdbmService)
114
-    {
115
-        $this->tdbmService = $tdbmService;
116
-
117
-        foreach ($beanData as $table => $columns) {
118
-            $this->dbRows[$table] = new DbRow($this, $table, $tdbmService->_getPrimaryKeysFromObjectData($table, $columns), $tdbmService, $columns);
119
-        }
120
-
121
-        $this->status = TDBMObjectStateEnum::STATE_LOADED;
122
-    }
123
-
124
-    /**
125
-     * Alternative constructor called when bean is lazily loaded.
126
-     *
127
-     * @param string      $tableName
128
-     * @param array       $primaryKeys
129
-     * @param TDBMService $tdbmService
130
-     */
131
-    public function _constructLazy($tableName, array $primaryKeys, TDBMService $tdbmService)
132
-    {
133
-        $this->tdbmService = $tdbmService;
134
-
135
-        $this->dbRows[$tableName] = new DbRow($this, $tableName, $primaryKeys, $tdbmService);
136
-
137
-        $this->status = TDBMObjectStateEnum::STATE_NOT_LOADED;
138
-    }
139
-
140
-    public function _attach(TDBMService $tdbmService)
141
-    {
142
-        if ($this->status !== TDBMObjectStateEnum::STATE_DETACHED) {
143
-            throw new TDBMInvalidOperationException('Cannot attach an object that is already attached to TDBM.');
144
-        }
145
-        $this->tdbmService = $tdbmService;
146
-
147
-        // If we attach this object, we must work to make sure the tables are in ascending order (from low level to top level)
148
-        $tableNames = $this->getUsedTables();
149
-
150
-        $newDbRows = [];
151
-
152
-        foreach ($tableNames as $table) {
153
-            if (!isset($this->dbRows[$table])) {
154
-                $this->registerTable($table);
155
-            }
156
-            $newDbRows[$table] = $this->dbRows[$table];
157
-        }
158
-        $this->dbRows = $newDbRows;
159
-
160
-        $this->status = TDBMObjectStateEnum::STATE_NEW;
161
-        foreach ($this->dbRows as $dbRow) {
162
-            $dbRow->_attach($tdbmService);
163
-        }
164
-    }
165
-
166
-    /**
167
-     * Sets the state of the TDBM Object
168
-     * One of TDBMObjectStateEnum::STATE_NEW, TDBMObjectStateEnum::STATE_NOT_LOADED, TDBMObjectStateEnum::STATE_LOADED, TDBMObjectStateEnum::STATE_DELETED.
169
-     * $status = TDBMObjectStateEnum::STATE_NEW when a new object is created with DBMObject:getNewObject.
170
-     * $status = TDBMObjectStateEnum::STATE_NOT_LOADED when the object has been retrieved with getObject but when no data has been accessed in it yet.
171
-     * $status = TDBMObjectStateEnum::STATE_LOADED when the object is cached in memory.
172
-     *
173
-     * @param string $state
174
-     */
175
-    public function _setStatus($state)
176
-    {
177
-        $this->status = $state;
178
-
179
-        // TODO: we might ignore the loaded => dirty state here! dirty status comes from the db_row itself.
180
-        foreach ($this->dbRows as $dbRow) {
181
-            $dbRow->_setStatus($state);
182
-        }
183
-
184
-        if ($state === TDBMObjectStateEnum::STATE_DELETED) {
185
-            $this->onDelete();
186
-        }
187
-    }
188
-
189
-    /**
190
-     * Checks that $tableName is ok, or returns the only possible table name if "$tableName = null"
191
-     * or throws an error.
192
-     *
193
-     * @param string $tableName
194
-     *
195
-     * @return string
196
-     */
197
-    private function checkTableName($tableName = null)
198
-    {
199
-        if ($tableName === null) {
200
-            if (count($this->dbRows) > 1) {
201
-                throw new TDBMException('This object is based on several tables. You must specify which table you are retrieving data from.');
202
-            } elseif (count($this->dbRows) === 1) {
203
-                $tableName = array_keys($this->dbRows)[0];
204
-            }
205
-        }
206
-
207
-        return $tableName;
208
-    }
209
-
210
-    protected function get($var, $tableName = null)
211
-    {
212
-        $tableName = $this->checkTableName($tableName);
213
-
214
-        if (!isset($this->dbRows[$tableName])) {
215
-            return;
216
-        }
217
-
218
-        return $this->dbRows[$tableName]->get($var);
219
-    }
220
-
221
-    protected function set($var, $value, $tableName = null)
222
-    {
223
-        if ($tableName === null) {
224
-            if (count($this->dbRows) > 1) {
225
-                throw new TDBMException('This object is based on several tables. You must specify which table you are retrieving data from.');
226
-            } elseif (count($this->dbRows) === 1) {
227
-                $tableName = array_keys($this->dbRows)[0];
228
-            } else {
229
-                throw new TDBMException('Please specify a table for this object.');
230
-            }
231
-        }
232
-
233
-        if (!isset($this->dbRows[$tableName])) {
234
-            $this->registerTable($tableName);
235
-        }
236
-
237
-        $this->dbRows[$tableName]->set($var, $value);
238
-        if ($this->dbRows[$tableName]->_getStatus() === TDBMObjectStateEnum::STATE_DIRTY) {
239
-            $this->status = TDBMObjectStateEnum::STATE_DIRTY;
240
-        }
241
-    }
242
-
243
-    /**
244
-     * @param string             $foreignKeyName
245
-     * @param AbstractTDBMObject $bean
246
-     */
247
-    protected function setRef($foreignKeyName, AbstractTDBMObject $bean = null, $tableName = null)
248
-    {
249
-        if ($tableName === null) {
250
-            if (count($this->dbRows) > 1) {
251
-                throw new TDBMException('This object is based on several tables. You must specify which table you are retrieving data from.');
252
-            } elseif (count($this->dbRows) === 1) {
253
-                $tableName = array_keys($this->dbRows)[0];
254
-            } else {
255
-                throw new TDBMException('Please specify a table for this object.');
256
-            }
257
-        }
258
-
259
-        if (!isset($this->dbRows[$tableName])) {
260
-            $this->registerTable($tableName);
261
-        }
262
-
263
-        $oldLinkedBean = $this->dbRows[$tableName]->getRef($foreignKeyName);
264
-        if ($oldLinkedBean !== null) {
265
-            $oldLinkedBean->removeManyToOneRelationship($tableName, $foreignKeyName, $this);
266
-        }
267
-
268
-        $this->dbRows[$tableName]->setRef($foreignKeyName, $bean);
269
-        if ($this->dbRows[$tableName]->_getStatus() === TDBMObjectStateEnum::STATE_DIRTY) {
270
-            $this->status = TDBMObjectStateEnum::STATE_DIRTY;
271
-        }
272
-
273
-        if ($bean !== null) {
274
-            $bean->setManyToOneRelationship($tableName, $foreignKeyName, $this);
275
-        }
276
-    }
277
-
278
-    /**
279
-     * @param string $foreignKeyName A unique name for this reference
280
-     *
281
-     * @return AbstractTDBMObject|null
282
-     */
283
-    protected function getRef($foreignKeyName, $tableName = null)
284
-    {
285
-        $tableName = $this->checkTableName($tableName);
286
-
287
-        if (!isset($this->dbRows[$tableName])) {
288
-            return;
289
-        }
290
-
291
-        return $this->dbRows[$tableName]->getRef($foreignKeyName);
292
-    }
293
-
294
-    /**
295
-     * Adds a many to many relationship to this bean.
296
-     *
297
-     * @param string             $pivotTableName
298
-     * @param AbstractTDBMObject $remoteBean
299
-     */
300
-    protected function addRelationship($pivotTableName, AbstractTDBMObject $remoteBean)
301
-    {
302
-        $this->setRelationship($pivotTableName, $remoteBean, 'new');
303
-    }
304
-
305
-    /**
306
-     * Returns true if there is a relationship to this bean.
307
-     *
308
-     * @param string             $pivotTableName
309
-     * @param AbstractTDBMObject $remoteBean
310
-     *
311
-     * @return bool
312
-     */
313
-    protected function hasRelationship($pivotTableName, AbstractTDBMObject $remoteBean)
314
-    {
315
-        $storage = $this->retrieveRelationshipsStorage($pivotTableName);
316
-
317
-        if ($storage->contains($remoteBean)) {
318
-            if ($storage[$remoteBean]['status'] !== 'delete') {
319
-                return true;
320
-            }
321
-        }
322
-
323
-        return false;
324
-    }
325
-
326
-    /**
327
-     * Internal TDBM method. Removes a many to many relationship from this bean.
328
-     *
329
-     * @param string             $pivotTableName
330
-     * @param AbstractTDBMObject $remoteBean
331
-     */
332
-    public function _removeRelationship($pivotTableName, AbstractTDBMObject $remoteBean)
333
-    {
334
-        if (isset($this->relationships[$pivotTableName][$remoteBean]) && $this->relationships[$pivotTableName][$remoteBean]['status'] === 'new') {
335
-            unset($this->relationships[$pivotTableName][$remoteBean]);
336
-            unset($remoteBean->relationships[$pivotTableName][$this]);
337
-        } else {
338
-            $this->setRelationship($pivotTableName, $remoteBean, 'delete');
339
-        }
340
-    }
341
-
342
-    /**
343
-     * Sets many to many relationships for this bean.
344
-     * Adds new relationships and removes unused ones.
345
-     *
346
-     * @param $pivotTableName
347
-     * @param array $remoteBeans
348
-     */
349
-    protected function setRelationships($pivotTableName, array $remoteBeans)
350
-    {
351
-        $storage = $this->retrieveRelationshipsStorage($pivotTableName);
352
-
353
-        foreach ($storage as $oldRemoteBean) {
354
-            if (!in_array($oldRemoteBean, $remoteBeans, true)) {
355
-                // $oldRemoteBean must be removed
356
-                $this->_removeRelationship($pivotTableName, $oldRemoteBean);
357
-            }
358
-        }
359
-
360
-        foreach ($remoteBeans as $remoteBean) {
361
-            if (!$storage->contains($remoteBean) || $storage[$remoteBean]['status'] === 'delete') {
362
-                // $remoteBean must be added
363
-                $this->addRelationship($pivotTableName, $remoteBean);
364
-            }
365
-        }
366
-    }
367
-
368
-    /**
369
-     * Returns the list of objects linked to this bean via $pivotTableName.
370
-     *
371
-     * @param $pivotTableName
372
-     *
373
-     * @return \SplObjectStorage
374
-     */
375
-    private function retrieveRelationshipsStorage($pivotTableName)
376
-    {
377
-        $storage = $this->getRelationshipStorage($pivotTableName);
378
-        if ($this->status === TDBMObjectStateEnum::STATE_DETACHED || $this->status === TDBMObjectStateEnum::STATE_NEW || (isset($this->loadedRelationships[$pivotTableName]) && $this->loadedRelationships[$pivotTableName])) {
379
-            return $storage;
380
-        }
381
-
382
-        $beans = $this->tdbmService->_getRelatedBeans($pivotTableName, $this);
383
-        $this->loadedRelationships[$pivotTableName] = true;
384
-
385
-        foreach ($beans as $bean) {
386
-            if (isset($storage[$bean])) {
387
-                $oldStatus = $storage[$bean]['status'];
388
-                if ($oldStatus === 'delete') {
389
-                    // Keep deleted things deleted
390
-                    continue;
391
-                }
392
-            }
393
-            $this->setRelationship($pivotTableName, $bean, 'loaded');
394
-        }
395
-
396
-        return $storage;
397
-    }
398
-
399
-    /**
400
-     * Internal TDBM method. Returns the list of objects linked to this bean via $pivotTableName.
401
-     *
402
-     * @param $pivotTableName
403
-     *
404
-     * @return AbstractTDBMObject[]
405
-     */
406
-    public function _getRelationships($pivotTableName)
407
-    {
408
-        return $this->relationshipStorageToArray($this->retrieveRelationshipsStorage($pivotTableName));
409
-    }
410
-
411
-    private function relationshipStorageToArray(\SplObjectStorage $storage)
412
-    {
413
-        $beans = [];
414
-        foreach ($storage as $bean) {
415
-            $statusArr = $storage[$bean];
416
-            if ($statusArr['status'] !== 'delete') {
417
-                $beans[] = $bean;
418
-            }
419
-        }
420
-
421
-        return $beans;
422
-    }
423
-
424
-    /**
425
-     * Declares a relationship between.
426
-     *
427
-     * @param string             $pivotTableName
428
-     * @param AbstractTDBMObject $remoteBean
429
-     * @param string             $status
430
-     */
431
-    private function setRelationship($pivotTableName, AbstractTDBMObject $remoteBean, $status)
432
-    {
433
-        $storage = $this->getRelationshipStorage($pivotTableName);
434
-        $storage->attach($remoteBean, ['status' => $status, 'reverse' => false]);
435
-        if ($this->status === TDBMObjectStateEnum::STATE_LOADED) {
436
-            $this->_setStatus(TDBMObjectStateEnum::STATE_DIRTY);
437
-        }
438
-
439
-        $remoteStorage = $remoteBean->getRelationshipStorage($pivotTableName);
440
-        $remoteStorage->attach($this, ['status' => $status, 'reverse' => true]);
441
-    }
442
-
443
-    /**
444
-     * Returns the SplObjectStorage associated to this relationship (creates it if it does not exists).
445
-     *
446
-     * @param string $pivotTableName
447
-     *
448
-     * @return \SplObjectStorage
449
-     */
450
-    private function getRelationshipStorage(string $pivotTableName) : \SplObjectStorage
451
-    {
452
-        return $this->relationships[$pivotTableName] ?? $this->relationships[$pivotTableName] = new \SplObjectStorage();
453
-    }
454
-
455
-    /**
456
-     * Returns the SplObjectStorage associated to this relationship (creates it if it does not exists).
457
-     *
458
-     * @param string $tableName
459
-     * @param string $foreignKeyName
460
-     *
461
-     * @return AlterableResultIterator
462
-     */
463
-    private function getManyToOneAlterableResultIterator(string $tableName, string $foreignKeyName) : AlterableResultIterator
464
-    {
465
-        $key = $tableName.'___'.$foreignKeyName;
466
-
467
-        return $this->manyToOneRelationships[$key] ?? $this->manyToOneRelationships[$key] = new AlterableResultIterator();
468
-    }
469
-
470
-    /**
471
-     * Declares a relationship between this bean and the bean pointing to it.
472
-     *
473
-     * @param string             $tableName
474
-     * @param string             $foreignKeyName
475
-     * @param AbstractTDBMObject $remoteBean
476
-     */
477
-    private function setManyToOneRelationship(string $tableName, string $foreignKeyName, AbstractTDBMObject $remoteBean)
478
-    {
479
-        $alterableResultIterator = $this->getManyToOneAlterableResultIterator($tableName, $foreignKeyName);
480
-        $alterableResultIterator->add($remoteBean);
481
-    }
482
-
483
-    /**
484
-     * Declares a relationship between this bean and the bean pointing to it.
485
-     *
486
-     * @param string             $tableName
487
-     * @param string             $foreignKeyName
488
-     * @param AbstractTDBMObject $remoteBean
489
-     */
490
-    private function removeManyToOneRelationship(string $tableName, string $foreignKeyName, AbstractTDBMObject $remoteBean)
491
-    {
492
-        $alterableResultIterator = $this->getManyToOneAlterableResultIterator($tableName, $foreignKeyName);
493
-        $alterableResultIterator->remove($remoteBean);
494
-    }
495
-
496
-    /**
497
-     * Returns the list of objects linked to this bean via a given foreign key.
498
-     *
499
-     * @param string $tableName
500
-     * @param string $foreignKeyName
501
-     * @param string $searchTableName
502
-     * @param array  $searchFilter
503
-     * @param string $orderString     The ORDER BY part of the query. All columns must be prefixed by the table name (in the form: table.column). WARNING : This parameter is not kept when there is an additionnal or removal object !
504
-     *
505
-     * @return AlterableResultIterator
506
-     */
507
-    protected function retrieveManyToOneRelationshipsStorage(string $tableName, string $foreignKeyName, string $searchTableName, array $searchFilter, $orderString = null) : AlterableResultIterator
508
-    {
509
-        $key = $tableName.'___'.$foreignKeyName;
510
-        $alterableResultIterator = $this->getManyToOneAlterableResultIterator($tableName, $foreignKeyName);
511
-        if ($this->status === TDBMObjectStateEnum::STATE_DETACHED || $this->status === TDBMObjectStateEnum::STATE_NEW || (isset($this->manyToOneRelationships[$key]) && $this->manyToOneRelationships[$key]->getUnderlyingResultIterator() !== null)) {
512
-            return $alterableResultIterator;
513
-        }
514
-
515
-        $unalteredResultIterator = $this->tdbmService->findObjects($searchTableName, $searchFilter, [], $orderString);
516
-
517
-        $alterableResultIterator->setResultIterator($unalteredResultIterator->getIterator());
518
-
519
-        return $alterableResultIterator;
520
-    }
521
-
522
-    /**
523
-     * Reverts any changes made to the object and resumes it to its DB state.
524
-     * This can only be called on objects that come from database and that have not been deleted.
525
-     * Otherwise, this will throw an exception.
526
-     *
527
-     * @throws TDBMException
528
-     */
529
-    public function discardChanges()
530
-    {
531
-        if ($this->status === TDBMObjectStateEnum::STATE_NEW || $this->status === TDBMObjectStateEnum::STATE_DETACHED) {
532
-            throw new TDBMException("You cannot call discardChanges() on an object that has been created with the 'new' keyword and that has not yet been saved.");
533
-        }
534
-
535
-        if ($this->status === TDBMObjectStateEnum::STATE_DELETED) {
536
-            throw new TDBMException('You cannot call discardChanges() on an object that has been deleted.');
537
-        }
538
-
539
-        $this->_setStatus(TDBMObjectStateEnum::STATE_NOT_LOADED);
540
-    }
541
-
542
-    /**
543
-     * Method used internally by TDBM. You should not use it directly.
544
-     * This method returns the status of the TDBMObject.
545
-     * This is one of TDBMObjectStateEnum::STATE_NEW, TDBMObjectStateEnum::STATE_NOT_LOADED, TDBMObjectStateEnum::STATE_LOADED, TDBMObjectStateEnum::STATE_DELETED.
546
-     * $status = TDBMObjectStateEnum::STATE_NEW when a new object is created with DBMObject:getNewObject.
547
-     * $status = TDBMObjectStateEnum::STATE_NOT_LOADED when the object has been retrieved with getObject but when no data has been accessed in it yet.
548
-     * $status = TDBMObjectStateEnum::STATE_LOADED when the object is cached in memory.
549
-     *
550
-     * @return string
551
-     */
552
-    public function _getStatus()
553
-    {
554
-        return $this->status;
555
-    }
556
-
557
-    /**
558
-     * Override the native php clone function for TDBMObjects.
559
-     */
560
-    public function __clone()
561
-    {
562
-        // Let's clone the many to many relationships
563
-        if ($this->status === TDBMObjectStateEnum::STATE_DETACHED) {
564
-            $pivotTableList = array_keys($this->relationships);
565
-        } else {
566
-            $pivotTableList = $this->tdbmService->_getPivotTablesLinkedToBean($this);
567
-        }
568
-
569
-        foreach ($pivotTableList as $pivotTable) {
570
-            $storage = $this->retrieveRelationshipsStorage($pivotTable);
571
-
572
-            // Let's duplicate the reverse side of the relationship // This is useless: already done by "retrieveRelationshipsStorage"!!!
573
-            /*foreach ($storage as $remoteBean) {
34
+	/**
35
+	 * The service this object is bound to.
36
+	 *
37
+	 * @var TDBMService
38
+	 */
39
+	protected $tdbmService;
40
+
41
+	/**
42
+	 * An array of DbRow, indexed by table name.
43
+	 *
44
+	 * @var DbRow[]
45
+	 */
46
+	protected $dbRows = [];
47
+
48
+	/**
49
+	 * One of TDBMObjectStateEnum::STATE_NEW, TDBMObjectStateEnum::STATE_NOT_LOADED, TDBMObjectStateEnum::STATE_LOADED, TDBMObjectStateEnum::STATE_DELETED.
50
+	 * $status = TDBMObjectStateEnum::STATE_NEW when a new object is created with DBMObject:getNewObject.
51
+	 * $status = TDBMObjectStateEnum::STATE_NOT_LOADED when the object has been retrieved with getObject but when no data has been accessed in it yet.
52
+	 * $status = TDBMObjectStateEnum::STATE_LOADED when the object is cached in memory.
53
+	 *
54
+	 * @var string
55
+	 */
56
+	private $status;
57
+
58
+	/**
59
+	 * Array storing beans related via many to many relationships (pivot tables).
60
+	 *
61
+	 * @var \SplObjectStorage[] Key: pivot table name, value: SplObjectStorage
62
+	 */
63
+	private $relationships = [];
64
+
65
+	/**
66
+	 * @var bool[] Key: pivot table name, value: whether a query was performed to load the data
67
+	 */
68
+	private $loadedRelationships = [];
69
+
70
+	/**
71
+	 * Array storing beans related via many to one relationships (this bean is pointed by external beans).
72
+	 *
73
+	 * @var AlterableResultIterator[] Key: [external_table]___[external_column], value: SplObjectStorage
74
+	 */
75
+	private $manyToOneRelationships = [];
76
+
77
+	/**
78
+	 * Used with $primaryKeys when we want to retrieve an existing object
79
+	 * and $primaryKeys=[] if we want a new object.
80
+	 *
81
+	 * @param string      $tableName
82
+	 * @param array       $primaryKeys
83
+	 * @param TDBMService $tdbmService
84
+	 *
85
+	 * @throws TDBMException
86
+	 * @throws TDBMInvalidOperationException
87
+	 */
88
+	public function __construct($tableName = null, array $primaryKeys = [], TDBMService $tdbmService = null)
89
+	{
90
+		// FIXME: lazy loading should be forbidden on tables with inheritance and dynamic type assignation...
91
+		if (!empty($tableName)) {
92
+			$this->dbRows[$tableName] = new DbRow($this, $tableName, $primaryKeys, $tdbmService);
93
+		}
94
+
95
+		if ($tdbmService === null) {
96
+			$this->_setStatus(TDBMObjectStateEnum::STATE_DETACHED);
97
+		} else {
98
+			$this->_attach($tdbmService);
99
+			if (!empty($primaryKeys)) {
100
+				$this->_setStatus(TDBMObjectStateEnum::STATE_NOT_LOADED);
101
+			} else {
102
+				$this->_setStatus(TDBMObjectStateEnum::STATE_NEW);
103
+			}
104
+		}
105
+	}
106
+
107
+	/**
108
+	 * Alternative constructor called when data is fetched from database via a SELECT.
109
+	 *
110
+	 * @param array       $beanData    array<table, array<column, value>>
111
+	 * @param TDBMService $tdbmService
112
+	 */
113
+	public function _constructFromData(array $beanData, TDBMService $tdbmService)
114
+	{
115
+		$this->tdbmService = $tdbmService;
116
+
117
+		foreach ($beanData as $table => $columns) {
118
+			$this->dbRows[$table] = new DbRow($this, $table, $tdbmService->_getPrimaryKeysFromObjectData($table, $columns), $tdbmService, $columns);
119
+		}
120
+
121
+		$this->status = TDBMObjectStateEnum::STATE_LOADED;
122
+	}
123
+
124
+	/**
125
+	 * Alternative constructor called when bean is lazily loaded.
126
+	 *
127
+	 * @param string      $tableName
128
+	 * @param array       $primaryKeys
129
+	 * @param TDBMService $tdbmService
130
+	 */
131
+	public function _constructLazy($tableName, array $primaryKeys, TDBMService $tdbmService)
132
+	{
133
+		$this->tdbmService = $tdbmService;
134
+
135
+		$this->dbRows[$tableName] = new DbRow($this, $tableName, $primaryKeys, $tdbmService);
136
+
137
+		$this->status = TDBMObjectStateEnum::STATE_NOT_LOADED;
138
+	}
139
+
140
+	public function _attach(TDBMService $tdbmService)
141
+	{
142
+		if ($this->status !== TDBMObjectStateEnum::STATE_DETACHED) {
143
+			throw new TDBMInvalidOperationException('Cannot attach an object that is already attached to TDBM.');
144
+		}
145
+		$this->tdbmService = $tdbmService;
146
+
147
+		// If we attach this object, we must work to make sure the tables are in ascending order (from low level to top level)
148
+		$tableNames = $this->getUsedTables();
149
+
150
+		$newDbRows = [];
151
+
152
+		foreach ($tableNames as $table) {
153
+			if (!isset($this->dbRows[$table])) {
154
+				$this->registerTable($table);
155
+			}
156
+			$newDbRows[$table] = $this->dbRows[$table];
157
+		}
158
+		$this->dbRows = $newDbRows;
159
+
160
+		$this->status = TDBMObjectStateEnum::STATE_NEW;
161
+		foreach ($this->dbRows as $dbRow) {
162
+			$dbRow->_attach($tdbmService);
163
+		}
164
+	}
165
+
166
+	/**
167
+	 * Sets the state of the TDBM Object
168
+	 * One of TDBMObjectStateEnum::STATE_NEW, TDBMObjectStateEnum::STATE_NOT_LOADED, TDBMObjectStateEnum::STATE_LOADED, TDBMObjectStateEnum::STATE_DELETED.
169
+	 * $status = TDBMObjectStateEnum::STATE_NEW when a new object is created with DBMObject:getNewObject.
170
+	 * $status = TDBMObjectStateEnum::STATE_NOT_LOADED when the object has been retrieved with getObject but when no data has been accessed in it yet.
171
+	 * $status = TDBMObjectStateEnum::STATE_LOADED when the object is cached in memory.
172
+	 *
173
+	 * @param string $state
174
+	 */
175
+	public function _setStatus($state)
176
+	{
177
+		$this->status = $state;
178
+
179
+		// TODO: we might ignore the loaded => dirty state here! dirty status comes from the db_row itself.
180
+		foreach ($this->dbRows as $dbRow) {
181
+			$dbRow->_setStatus($state);
182
+		}
183
+
184
+		if ($state === TDBMObjectStateEnum::STATE_DELETED) {
185
+			$this->onDelete();
186
+		}
187
+	}
188
+
189
+	/**
190
+	 * Checks that $tableName is ok, or returns the only possible table name if "$tableName = null"
191
+	 * or throws an error.
192
+	 *
193
+	 * @param string $tableName
194
+	 *
195
+	 * @return string
196
+	 */
197
+	private function checkTableName($tableName = null)
198
+	{
199
+		if ($tableName === null) {
200
+			if (count($this->dbRows) > 1) {
201
+				throw new TDBMException('This object is based on several tables. You must specify which table you are retrieving data from.');
202
+			} elseif (count($this->dbRows) === 1) {
203
+				$tableName = array_keys($this->dbRows)[0];
204
+			}
205
+		}
206
+
207
+		return $tableName;
208
+	}
209
+
210
+	protected function get($var, $tableName = null)
211
+	{
212
+		$tableName = $this->checkTableName($tableName);
213
+
214
+		if (!isset($this->dbRows[$tableName])) {
215
+			return;
216
+		}
217
+
218
+		return $this->dbRows[$tableName]->get($var);
219
+	}
220
+
221
+	protected function set($var, $value, $tableName = null)
222
+	{
223
+		if ($tableName === null) {
224
+			if (count($this->dbRows) > 1) {
225
+				throw new TDBMException('This object is based on several tables. You must specify which table you are retrieving data from.');
226
+			} elseif (count($this->dbRows) === 1) {
227
+				$tableName = array_keys($this->dbRows)[0];
228
+			} else {
229
+				throw new TDBMException('Please specify a table for this object.');
230
+			}
231
+		}
232
+
233
+		if (!isset($this->dbRows[$tableName])) {
234
+			$this->registerTable($tableName);
235
+		}
236
+
237
+		$this->dbRows[$tableName]->set($var, $value);
238
+		if ($this->dbRows[$tableName]->_getStatus() === TDBMObjectStateEnum::STATE_DIRTY) {
239
+			$this->status = TDBMObjectStateEnum::STATE_DIRTY;
240
+		}
241
+	}
242
+
243
+	/**
244
+	 * @param string             $foreignKeyName
245
+	 * @param AbstractTDBMObject $bean
246
+	 */
247
+	protected function setRef($foreignKeyName, AbstractTDBMObject $bean = null, $tableName = null)
248
+	{
249
+		if ($tableName === null) {
250
+			if (count($this->dbRows) > 1) {
251
+				throw new TDBMException('This object is based on several tables. You must specify which table you are retrieving data from.');
252
+			} elseif (count($this->dbRows) === 1) {
253
+				$tableName = array_keys($this->dbRows)[0];
254
+			} else {
255
+				throw new TDBMException('Please specify a table for this object.');
256
+			}
257
+		}
258
+
259
+		if (!isset($this->dbRows[$tableName])) {
260
+			$this->registerTable($tableName);
261
+		}
262
+
263
+		$oldLinkedBean = $this->dbRows[$tableName]->getRef($foreignKeyName);
264
+		if ($oldLinkedBean !== null) {
265
+			$oldLinkedBean->removeManyToOneRelationship($tableName, $foreignKeyName, $this);
266
+		}
267
+
268
+		$this->dbRows[$tableName]->setRef($foreignKeyName, $bean);
269
+		if ($this->dbRows[$tableName]->_getStatus() === TDBMObjectStateEnum::STATE_DIRTY) {
270
+			$this->status = TDBMObjectStateEnum::STATE_DIRTY;
271
+		}
272
+
273
+		if ($bean !== null) {
274
+			$bean->setManyToOneRelationship($tableName, $foreignKeyName, $this);
275
+		}
276
+	}
277
+
278
+	/**
279
+	 * @param string $foreignKeyName A unique name for this reference
280
+	 *
281
+	 * @return AbstractTDBMObject|null
282
+	 */
283
+	protected function getRef($foreignKeyName, $tableName = null)
284
+	{
285
+		$tableName = $this->checkTableName($tableName);
286
+
287
+		if (!isset($this->dbRows[$tableName])) {
288
+			return;
289
+		}
290
+
291
+		return $this->dbRows[$tableName]->getRef($foreignKeyName);
292
+	}
293
+
294
+	/**
295
+	 * Adds a many to many relationship to this bean.
296
+	 *
297
+	 * @param string             $pivotTableName
298
+	 * @param AbstractTDBMObject $remoteBean
299
+	 */
300
+	protected function addRelationship($pivotTableName, AbstractTDBMObject $remoteBean)
301
+	{
302
+		$this->setRelationship($pivotTableName, $remoteBean, 'new');
303
+	}
304
+
305
+	/**
306
+	 * Returns true if there is a relationship to this bean.
307
+	 *
308
+	 * @param string             $pivotTableName
309
+	 * @param AbstractTDBMObject $remoteBean
310
+	 *
311
+	 * @return bool
312
+	 */
313
+	protected function hasRelationship($pivotTableName, AbstractTDBMObject $remoteBean)
314
+	{
315
+		$storage = $this->retrieveRelationshipsStorage($pivotTableName);
316
+
317
+		if ($storage->contains($remoteBean)) {
318
+			if ($storage[$remoteBean]['status'] !== 'delete') {
319
+				return true;
320
+			}
321
+		}
322
+
323
+		return false;
324
+	}
325
+
326
+	/**
327
+	 * Internal TDBM method. Removes a many to many relationship from this bean.
328
+	 *
329
+	 * @param string             $pivotTableName
330
+	 * @param AbstractTDBMObject $remoteBean
331
+	 */
332
+	public function _removeRelationship($pivotTableName, AbstractTDBMObject $remoteBean)
333
+	{
334
+		if (isset($this->relationships[$pivotTableName][$remoteBean]) && $this->relationships[$pivotTableName][$remoteBean]['status'] === 'new') {
335
+			unset($this->relationships[$pivotTableName][$remoteBean]);
336
+			unset($remoteBean->relationships[$pivotTableName][$this]);
337
+		} else {
338
+			$this->setRelationship($pivotTableName, $remoteBean, 'delete');
339
+		}
340
+	}
341
+
342
+	/**
343
+	 * Sets many to many relationships for this bean.
344
+	 * Adds new relationships and removes unused ones.
345
+	 *
346
+	 * @param $pivotTableName
347
+	 * @param array $remoteBeans
348
+	 */
349
+	protected function setRelationships($pivotTableName, array $remoteBeans)
350
+	{
351
+		$storage = $this->retrieveRelationshipsStorage($pivotTableName);
352
+
353
+		foreach ($storage as $oldRemoteBean) {
354
+			if (!in_array($oldRemoteBean, $remoteBeans, true)) {
355
+				// $oldRemoteBean must be removed
356
+				$this->_removeRelationship($pivotTableName, $oldRemoteBean);
357
+			}
358
+		}
359
+
360
+		foreach ($remoteBeans as $remoteBean) {
361
+			if (!$storage->contains($remoteBean) || $storage[$remoteBean]['status'] === 'delete') {
362
+				// $remoteBean must be added
363
+				$this->addRelationship($pivotTableName, $remoteBean);
364
+			}
365
+		}
366
+	}
367
+
368
+	/**
369
+	 * Returns the list of objects linked to this bean via $pivotTableName.
370
+	 *
371
+	 * @param $pivotTableName
372
+	 *
373
+	 * @return \SplObjectStorage
374
+	 */
375
+	private function retrieveRelationshipsStorage($pivotTableName)
376
+	{
377
+		$storage = $this->getRelationshipStorage($pivotTableName);
378
+		if ($this->status === TDBMObjectStateEnum::STATE_DETACHED || $this->status === TDBMObjectStateEnum::STATE_NEW || (isset($this->loadedRelationships[$pivotTableName]) && $this->loadedRelationships[$pivotTableName])) {
379
+			return $storage;
380
+		}
381
+
382
+		$beans = $this->tdbmService->_getRelatedBeans($pivotTableName, $this);
383
+		$this->loadedRelationships[$pivotTableName] = true;
384
+
385
+		foreach ($beans as $bean) {
386
+			if (isset($storage[$bean])) {
387
+				$oldStatus = $storage[$bean]['status'];
388
+				if ($oldStatus === 'delete') {
389
+					// Keep deleted things deleted
390
+					continue;
391
+				}
392
+			}
393
+			$this->setRelationship($pivotTableName, $bean, 'loaded');
394
+		}
395
+
396
+		return $storage;
397
+	}
398
+
399
+	/**
400
+	 * Internal TDBM method. Returns the list of objects linked to this bean via $pivotTableName.
401
+	 *
402
+	 * @param $pivotTableName
403
+	 *
404
+	 * @return AbstractTDBMObject[]
405
+	 */
406
+	public function _getRelationships($pivotTableName)
407
+	{
408
+		return $this->relationshipStorageToArray($this->retrieveRelationshipsStorage($pivotTableName));
409
+	}
410
+
411
+	private function relationshipStorageToArray(\SplObjectStorage $storage)
412
+	{
413
+		$beans = [];
414
+		foreach ($storage as $bean) {
415
+			$statusArr = $storage[$bean];
416
+			if ($statusArr['status'] !== 'delete') {
417
+				$beans[] = $bean;
418
+			}
419
+		}
420
+
421
+		return $beans;
422
+	}
423
+
424
+	/**
425
+	 * Declares a relationship between.
426
+	 *
427
+	 * @param string             $pivotTableName
428
+	 * @param AbstractTDBMObject $remoteBean
429
+	 * @param string             $status
430
+	 */
431
+	private function setRelationship($pivotTableName, AbstractTDBMObject $remoteBean, $status)
432
+	{
433
+		$storage = $this->getRelationshipStorage($pivotTableName);
434
+		$storage->attach($remoteBean, ['status' => $status, 'reverse' => false]);
435
+		if ($this->status === TDBMObjectStateEnum::STATE_LOADED) {
436
+			$this->_setStatus(TDBMObjectStateEnum::STATE_DIRTY);
437
+		}
438
+
439
+		$remoteStorage = $remoteBean->getRelationshipStorage($pivotTableName);
440
+		$remoteStorage->attach($this, ['status' => $status, 'reverse' => true]);
441
+	}
442
+
443
+	/**
444
+	 * Returns the SplObjectStorage associated to this relationship (creates it if it does not exists).
445
+	 *
446
+	 * @param string $pivotTableName
447
+	 *
448
+	 * @return \SplObjectStorage
449
+	 */
450
+	private function getRelationshipStorage(string $pivotTableName) : \SplObjectStorage
451
+	{
452
+		return $this->relationships[$pivotTableName] ?? $this->relationships[$pivotTableName] = new \SplObjectStorage();
453
+	}
454
+
455
+	/**
456
+	 * Returns the SplObjectStorage associated to this relationship (creates it if it does not exists).
457
+	 *
458
+	 * @param string $tableName
459
+	 * @param string $foreignKeyName
460
+	 *
461
+	 * @return AlterableResultIterator
462
+	 */
463
+	private function getManyToOneAlterableResultIterator(string $tableName, string $foreignKeyName) : AlterableResultIterator
464
+	{
465
+		$key = $tableName.'___'.$foreignKeyName;
466
+
467
+		return $this->manyToOneRelationships[$key] ?? $this->manyToOneRelationships[$key] = new AlterableResultIterator();
468
+	}
469
+
470
+	/**
471
+	 * Declares a relationship between this bean and the bean pointing to it.
472
+	 *
473
+	 * @param string             $tableName
474
+	 * @param string             $foreignKeyName
475
+	 * @param AbstractTDBMObject $remoteBean
476
+	 */
477
+	private function setManyToOneRelationship(string $tableName, string $foreignKeyName, AbstractTDBMObject $remoteBean)
478
+	{
479
+		$alterableResultIterator = $this->getManyToOneAlterableResultIterator($tableName, $foreignKeyName);
480
+		$alterableResultIterator->add($remoteBean);
481
+	}
482
+
483
+	/**
484
+	 * Declares a relationship between this bean and the bean pointing to it.
485
+	 *
486
+	 * @param string             $tableName
487
+	 * @param string             $foreignKeyName
488
+	 * @param AbstractTDBMObject $remoteBean
489
+	 */
490
+	private function removeManyToOneRelationship(string $tableName, string $foreignKeyName, AbstractTDBMObject $remoteBean)
491
+	{
492
+		$alterableResultIterator = $this->getManyToOneAlterableResultIterator($tableName, $foreignKeyName);
493
+		$alterableResultIterator->remove($remoteBean);
494
+	}
495
+
496
+	/**
497
+	 * Returns the list of objects linked to this bean via a given foreign key.
498
+	 *
499
+	 * @param string $tableName
500
+	 * @param string $foreignKeyName
501
+	 * @param string $searchTableName
502
+	 * @param array  $searchFilter
503
+	 * @param string $orderString     The ORDER BY part of the query. All columns must be prefixed by the table name (in the form: table.column). WARNING : This parameter is not kept when there is an additionnal or removal object !
504
+	 *
505
+	 * @return AlterableResultIterator
506
+	 */
507
+	protected function retrieveManyToOneRelationshipsStorage(string $tableName, string $foreignKeyName, string $searchTableName, array $searchFilter, $orderString = null) : AlterableResultIterator
508
+	{
509
+		$key = $tableName.'___'.$foreignKeyName;
510
+		$alterableResultIterator = $this->getManyToOneAlterableResultIterator($tableName, $foreignKeyName);
511
+		if ($this->status === TDBMObjectStateEnum::STATE_DETACHED || $this->status === TDBMObjectStateEnum::STATE_NEW || (isset($this->manyToOneRelationships[$key]) && $this->manyToOneRelationships[$key]->getUnderlyingResultIterator() !== null)) {
512
+			return $alterableResultIterator;
513
+		}
514
+
515
+		$unalteredResultIterator = $this->tdbmService->findObjects($searchTableName, $searchFilter, [], $orderString);
516
+
517
+		$alterableResultIterator->setResultIterator($unalteredResultIterator->getIterator());
518
+
519
+		return $alterableResultIterator;
520
+	}
521
+
522
+	/**
523
+	 * Reverts any changes made to the object and resumes it to its DB state.
524
+	 * This can only be called on objects that come from database and that have not been deleted.
525
+	 * Otherwise, this will throw an exception.
526
+	 *
527
+	 * @throws TDBMException
528
+	 */
529
+	public function discardChanges()
530
+	{
531
+		if ($this->status === TDBMObjectStateEnum::STATE_NEW || $this->status === TDBMObjectStateEnum::STATE_DETACHED) {
532
+			throw new TDBMException("You cannot call discardChanges() on an object that has been created with the 'new' keyword and that has not yet been saved.");
533
+		}
534
+
535
+		if ($this->status === TDBMObjectStateEnum::STATE_DELETED) {
536
+			throw new TDBMException('You cannot call discardChanges() on an object that has been deleted.');
537
+		}
538
+
539
+		$this->_setStatus(TDBMObjectStateEnum::STATE_NOT_LOADED);
540
+	}
541
+
542
+	/**
543
+	 * Method used internally by TDBM. You should not use it directly.
544
+	 * This method returns the status of the TDBMObject.
545
+	 * This is one of TDBMObjectStateEnum::STATE_NEW, TDBMObjectStateEnum::STATE_NOT_LOADED, TDBMObjectStateEnum::STATE_LOADED, TDBMObjectStateEnum::STATE_DELETED.
546
+	 * $status = TDBMObjectStateEnum::STATE_NEW when a new object is created with DBMObject:getNewObject.
547
+	 * $status = TDBMObjectStateEnum::STATE_NOT_LOADED when the object has been retrieved with getObject but when no data has been accessed in it yet.
548
+	 * $status = TDBMObjectStateEnum::STATE_LOADED when the object is cached in memory.
549
+	 *
550
+	 * @return string
551
+	 */
552
+	public function _getStatus()
553
+	{
554
+		return $this->status;
555
+	}
556
+
557
+	/**
558
+	 * Override the native php clone function for TDBMObjects.
559
+	 */
560
+	public function __clone()
561
+	{
562
+		// Let's clone the many to many relationships
563
+		if ($this->status === TDBMObjectStateEnum::STATE_DETACHED) {
564
+			$pivotTableList = array_keys($this->relationships);
565
+		} else {
566
+			$pivotTableList = $this->tdbmService->_getPivotTablesLinkedToBean($this);
567
+		}
568
+
569
+		foreach ($pivotTableList as $pivotTable) {
570
+			$storage = $this->retrieveRelationshipsStorage($pivotTable);
571
+
572
+			// Let's duplicate the reverse side of the relationship // This is useless: already done by "retrieveRelationshipsStorage"!!!
573
+			/*foreach ($storage as $remoteBean) {
574 574
                 $metadata = $storage[$remoteBean];
575 575
 
576 576
                 $remoteStorage = $remoteBean->getRelationshipStorage($pivotTable);
577 577
                 $remoteStorage->attach($this, ['status' => $metadata['status'], 'reverse' => !$metadata['reverse']]);
578 578
             }*/
579
-        }
580
-
581
-        // Let's clone each row
582
-        foreach ($this->dbRows as $key => &$dbRow) {
583
-            $dbRow = clone $dbRow;
584
-            $dbRow->setTDBMObject($this);
585
-        }
586
-
587
-        $this->manyToOneRelationships = [];
588
-
589
-        // Let's set the status to new (to enter the save function)
590
-        $this->status = TDBMObjectStateEnum::STATE_DETACHED;
591
-    }
592
-
593
-    /**
594
-     * Returns raw database rows.
595
-     *
596
-     * @return DbRow[] Key: table name, Value: DbRow object
597
-     */
598
-    public function _getDbRows()
599
-    {
600
-        return $this->dbRows;
601
-    }
602
-
603
-    private function registerTable($tableName)
604
-    {
605
-        $dbRow = new DbRow($this, $tableName);
606
-
607
-        if (in_array($this->status, [TDBMObjectStateEnum::STATE_NOT_LOADED, TDBMObjectStateEnum::STATE_LOADED, TDBMObjectStateEnum::STATE_DIRTY])) {
608
-            // Let's get the primary key for the new table
609
-            $anotherDbRow = array_values($this->dbRows)[0];
610
-            /* @var $anotherDbRow DbRow */
611
-            $indexedPrimaryKeys = array_values($anotherDbRow->_getPrimaryKeys());
612
-            $primaryKeys = $this->tdbmService->_getPrimaryKeysFromIndexedPrimaryKeys($tableName, $indexedPrimaryKeys);
613
-            $dbRow->_setPrimaryKeys($primaryKeys);
614
-        }
615
-
616
-        $dbRow->_setStatus($this->status);
617
-
618
-        $this->dbRows[$tableName] = $dbRow;
619
-        // TODO: look at status (if not new)=> get primary key from tdbmservice
620
-    }
621
-
622
-    /**
623
-     * Internal function: return the list of relationships.
624
-     *
625
-     * @return \SplObjectStorage[]
626
-     */
627
-    public function _getCachedRelationships()
628
-    {
629
-        return $this->relationships;
630
-    }
631
-
632
-    /**
633
-     * Returns an array of used tables by this bean (from parent to child relationship).
634
-     *
635
-     * @return string[]
636
-     */
637
-    abstract protected function getUsedTables();
638
-
639
-    /**
640
-     * Method called when the bean is removed from database.
641
-     */
642
-    protected function onDelete()
643
-    {
644
-    }
579
+		}
580
+
581
+		// Let's clone each row
582
+		foreach ($this->dbRows as $key => &$dbRow) {
583
+			$dbRow = clone $dbRow;
584
+			$dbRow->setTDBMObject($this);
585
+		}
586
+
587
+		$this->manyToOneRelationships = [];
588
+
589
+		// Let's set the status to new (to enter the save function)
590
+		$this->status = TDBMObjectStateEnum::STATE_DETACHED;
591
+	}
592
+
593
+	/**
594
+	 * Returns raw database rows.
595
+	 *
596
+	 * @return DbRow[] Key: table name, Value: DbRow object
597
+	 */
598
+	public function _getDbRows()
599
+	{
600
+		return $this->dbRows;
601
+	}
602
+
603
+	private function registerTable($tableName)
604
+	{
605
+		$dbRow = new DbRow($this, $tableName);
606
+
607
+		if (in_array($this->status, [TDBMObjectStateEnum::STATE_NOT_LOADED, TDBMObjectStateEnum::STATE_LOADED, TDBMObjectStateEnum::STATE_DIRTY])) {
608
+			// Let's get the primary key for the new table
609
+			$anotherDbRow = array_values($this->dbRows)[0];
610
+			/* @var $anotherDbRow DbRow */
611
+			$indexedPrimaryKeys = array_values($anotherDbRow->_getPrimaryKeys());
612
+			$primaryKeys = $this->tdbmService->_getPrimaryKeysFromIndexedPrimaryKeys($tableName, $indexedPrimaryKeys);
613
+			$dbRow->_setPrimaryKeys($primaryKeys);
614
+		}
615
+
616
+		$dbRow->_setStatus($this->status);
617
+
618
+		$this->dbRows[$tableName] = $dbRow;
619
+		// TODO: look at status (if not new)=> get primary key from tdbmservice
620
+	}
621
+
622
+	/**
623
+	 * Internal function: return the list of relationships.
624
+	 *
625
+	 * @return \SplObjectStorage[]
626
+	 */
627
+	public function _getCachedRelationships()
628
+	{
629
+		return $this->relationships;
630
+	}
631
+
632
+	/**
633
+	 * Returns an array of used tables by this bean (from parent to child relationship).
634
+	 *
635
+	 * @return string[]
636
+	 */
637
+	abstract protected function getUsedTables();
638
+
639
+	/**
640
+	 * Method called when the bean is removed from database.
641
+	 */
642
+	protected function onDelete()
643
+	{
644
+	}
645 645
 }
Please login to merge, or discard this patch.
src/Mouf/Database/TDBM/Utils/BeanDescriptor.php 1 patch
Indentation   +543 added lines, -543 removed lines patch added patch discarded remove patch
@@ -16,228 +16,228 @@  discard block
 block discarded – undo
16 16
  */
17 17
 class BeanDescriptor
18 18
 {
19
-    /**
20
-     * @var Table
21
-     */
22
-    private $table;
23
-
24
-    /**
25
-     * @var SchemaAnalyzer
26
-     */
27
-    private $schemaAnalyzer;
28
-
29
-    /**
30
-     * @var Schema
31
-     */
32
-    private $schema;
33
-
34
-    /**
35
-     * @var AbstractBeanPropertyDescriptor[]
36
-     */
37
-    private $beanPropertyDescriptors = [];
38
-
39
-    /**
40
-     * @var TDBMSchemaAnalyzer
41
-     */
42
-    private $tdbmSchemaAnalyzer;
43
-
44
-    public function __construct(Table $table, SchemaAnalyzer $schemaAnalyzer, Schema $schema, TDBMSchemaAnalyzer $tdbmSchemaAnalyzer)
45
-    {
46
-        $this->table = $table;
47
-        $this->schemaAnalyzer = $schemaAnalyzer;
48
-        $this->schema = $schema;
49
-        $this->tdbmSchemaAnalyzer = $tdbmSchemaAnalyzer;
50
-        $this->initBeanPropertyDescriptors();
51
-    }
52
-
53
-    private function initBeanPropertyDescriptors()
54
-    {
55
-        $this->beanPropertyDescriptors = $this->getProperties($this->table);
56
-    }
57
-
58
-    /**
59
-     * Returns the foreign-key the column is part of, if any. null otherwise.
60
-     *
61
-     * @param Table  $table
62
-     * @param Column $column
63
-     *
64
-     * @return ForeignKeyConstraint|null
65
-     */
66
-    private function isPartOfForeignKey(Table $table, Column $column)
67
-    {
68
-        $localColumnName = $column->getName();
69
-        foreach ($table->getForeignKeys() as $foreignKey) {
70
-            foreach ($foreignKey->getColumns() as $columnName) {
71
-                if ($columnName === $localColumnName) {
72
-                    return $foreignKey;
73
-                }
74
-            }
75
-        }
76
-
77
-        return;
78
-    }
79
-
80
-    /**
81
-     * @return AbstractBeanPropertyDescriptor[]
82
-     */
83
-    public function getBeanPropertyDescriptors()
84
-    {
85
-        return $this->beanPropertyDescriptors;
86
-    }
87
-
88
-    /**
89
-     * Returns the list of columns that are not nullable and not autogenerated for a given table and its parent.
90
-     *
91
-     * @return AbstractBeanPropertyDescriptor[]
92
-     */
93
-    public function getConstructorProperties()
94
-    {
95
-        $constructorProperties = array_filter($this->beanPropertyDescriptors, function (AbstractBeanPropertyDescriptor $property) {
96
-            return $property->isCompulsory();
97
-        });
98
-
99
-        return $constructorProperties;
100
-    }
101
-
102
-    /**
103
-     * Returns the list of columns that have default values for a given table.
104
-     *
105
-     * @return AbstractBeanPropertyDescriptor[]
106
-     */
107
-    public function getPropertiesWithDefault()
108
-    {
109
-        $properties = $this->getPropertiesForTable($this->table);
110
-        $defaultProperties = array_filter($properties, function (AbstractBeanPropertyDescriptor $property) {
111
-            return $property->hasDefault();
112
-        });
113
-
114
-        return $defaultProperties;
115
-    }
116
-
117
-    /**
118
-     * Returns the list of properties exposed as getters and setters in this class.
119
-     *
120
-     * @return AbstractBeanPropertyDescriptor[]
121
-     */
122
-    public function getExposedProperties()
123
-    {
124
-        $exposedProperties = array_filter($this->beanPropertyDescriptors, function (AbstractBeanPropertyDescriptor $property) {
125
-            return $property->getTable()->getName() == $this->table->getName();
126
-        });
127
-
128
-        return $exposedProperties;
129
-    }
130
-
131
-    /**
132
-     * Returns the list of properties for this table (including parent tables).
133
-     *
134
-     * @param Table $table
135
-     *
136
-     * @return AbstractBeanPropertyDescriptor[]
137
-     */
138
-    private function getProperties(Table $table)
139
-    {
140
-        $parentRelationship = $this->schemaAnalyzer->getParentRelationship($table->getName());
141
-        if ($parentRelationship) {
142
-            $parentTable = $this->schema->getTable($parentRelationship->getForeignTableName());
143
-            $properties = $this->getProperties($parentTable);
144
-            // we merge properties by overriding property names.
145
-            $localProperties = $this->getPropertiesForTable($table);
146
-            foreach ($localProperties as $name => $property) {
147
-                // We do not override properties if this is a primary key!
148
-                if ($property->isPrimaryKey()) {
149
-                    continue;
150
-                }
151
-                $properties[$name] = $property;
152
-            }
153
-        } else {
154
-            $properties = $this->getPropertiesForTable($table);
155
-        }
156
-
157
-        return $properties;
158
-    }
159
-
160
-    /**
161
-     * Returns the list of properties for this table (ignoring parent tables).
162
-     *
163
-     * @param Table $table
164
-     *
165
-     * @return AbstractBeanPropertyDescriptor[]
166
-     */
167
-    private function getPropertiesForTable(Table $table)
168
-    {
169
-        $parentRelationship = $this->schemaAnalyzer->getParentRelationship($table->getName());
170
-        if ($parentRelationship) {
171
-            $ignoreColumns = $parentRelationship->getLocalColumns();
172
-        } else {
173
-            $ignoreColumns = [];
174
-        }
175
-
176
-        $beanPropertyDescriptors = [];
177
-
178
-        foreach ($table->getColumns() as $column) {
179
-            if (array_search($column->getName(), $ignoreColumns) !== false) {
180
-                continue;
181
-            }
182
-
183
-            $fk = $this->isPartOfForeignKey($table, $column);
184
-            if ($fk !== null) {
185
-                // Check that previously added descriptors are not added on same FK (can happen with multi key FK).
186
-                foreach ($beanPropertyDescriptors as $beanDescriptor) {
187
-                    if ($beanDescriptor instanceof ObjectBeanPropertyDescriptor && $beanDescriptor->getForeignKey() === $fk) {
188
-                        continue 2;
189
-                    }
190
-                }
191
-                // Check that this property is not an inheritance relationship
192
-                $parentRelationship = $this->schemaAnalyzer->getParentRelationship($table->getName());
193
-                if ($parentRelationship === $fk) {
194
-                    continue;
195
-                }
196
-
197
-                $beanPropertyDescriptors[] = new ObjectBeanPropertyDescriptor($table, $fk, $this->schemaAnalyzer);
198
-            } else {
199
-                $beanPropertyDescriptors[] = new ScalarBeanPropertyDescriptor($table, $column);
200
-            }
201
-        }
202
-
203
-        // Now, let's get the name of all properties and let's check there is no duplicate.
204
-        /** @var $names AbstractBeanPropertyDescriptor[] */
205
-        $names = [];
206
-        foreach ($beanPropertyDescriptors as $beanDescriptor) {
207
-            $name = $beanDescriptor->getUpperCamelCaseName();
208
-            if (isset($names[$name])) {
209
-                $names[$name]->useAlternativeName();
210
-                $beanDescriptor->useAlternativeName();
211
-            } else {
212
-                $names[$name] = $beanDescriptor;
213
-            }
214
-        }
215
-
216
-        // Final check (throw exceptions if problem arises)
217
-        $names = [];
218
-        foreach ($beanPropertyDescriptors as $beanDescriptor) {
219
-            $name = $beanDescriptor->getUpperCamelCaseName();
220
-            if (isset($names[$name])) {
221
-                throw new TDBMException('Unsolvable name conflict while generating method name');
222
-            } else {
223
-                $names[$name] = $beanDescriptor;
224
-            }
225
-        }
226
-
227
-        // Last step, let's rebuild the list with a map:
228
-        $beanPropertyDescriptorsMap = [];
229
-        foreach ($beanPropertyDescriptors as $beanDescriptor) {
230
-            $beanPropertyDescriptorsMap[$beanDescriptor->getLowerCamelCaseName()] = $beanDescriptor;
231
-        }
232
-
233
-        return $beanPropertyDescriptorsMap;
234
-    }
235
-
236
-    public function generateBeanConstructor()
237
-    {
238
-        $constructorProperties = $this->getConstructorProperties();
239
-
240
-        $constructorCode = '    /**
19
+	/**
20
+	 * @var Table
21
+	 */
22
+	private $table;
23
+
24
+	/**
25
+	 * @var SchemaAnalyzer
26
+	 */
27
+	private $schemaAnalyzer;
28
+
29
+	/**
30
+	 * @var Schema
31
+	 */
32
+	private $schema;
33
+
34
+	/**
35
+	 * @var AbstractBeanPropertyDescriptor[]
36
+	 */
37
+	private $beanPropertyDescriptors = [];
38
+
39
+	/**
40
+	 * @var TDBMSchemaAnalyzer
41
+	 */
42
+	private $tdbmSchemaAnalyzer;
43
+
44
+	public function __construct(Table $table, SchemaAnalyzer $schemaAnalyzer, Schema $schema, TDBMSchemaAnalyzer $tdbmSchemaAnalyzer)
45
+	{
46
+		$this->table = $table;
47
+		$this->schemaAnalyzer = $schemaAnalyzer;
48
+		$this->schema = $schema;
49
+		$this->tdbmSchemaAnalyzer = $tdbmSchemaAnalyzer;
50
+		$this->initBeanPropertyDescriptors();
51
+	}
52
+
53
+	private function initBeanPropertyDescriptors()
54
+	{
55
+		$this->beanPropertyDescriptors = $this->getProperties($this->table);
56
+	}
57
+
58
+	/**
59
+	 * Returns the foreign-key the column is part of, if any. null otherwise.
60
+	 *
61
+	 * @param Table  $table
62
+	 * @param Column $column
63
+	 *
64
+	 * @return ForeignKeyConstraint|null
65
+	 */
66
+	private function isPartOfForeignKey(Table $table, Column $column)
67
+	{
68
+		$localColumnName = $column->getName();
69
+		foreach ($table->getForeignKeys() as $foreignKey) {
70
+			foreach ($foreignKey->getColumns() as $columnName) {
71
+				if ($columnName === $localColumnName) {
72
+					return $foreignKey;
73
+				}
74
+			}
75
+		}
76
+
77
+		return;
78
+	}
79
+
80
+	/**
81
+	 * @return AbstractBeanPropertyDescriptor[]
82
+	 */
83
+	public function getBeanPropertyDescriptors()
84
+	{
85
+		return $this->beanPropertyDescriptors;
86
+	}
87
+
88
+	/**
89
+	 * Returns the list of columns that are not nullable and not autogenerated for a given table and its parent.
90
+	 *
91
+	 * @return AbstractBeanPropertyDescriptor[]
92
+	 */
93
+	public function getConstructorProperties()
94
+	{
95
+		$constructorProperties = array_filter($this->beanPropertyDescriptors, function (AbstractBeanPropertyDescriptor $property) {
96
+			return $property->isCompulsory();
97
+		});
98
+
99
+		return $constructorProperties;
100
+	}
101
+
102
+	/**
103
+	 * Returns the list of columns that have default values for a given table.
104
+	 *
105
+	 * @return AbstractBeanPropertyDescriptor[]
106
+	 */
107
+	public function getPropertiesWithDefault()
108
+	{
109
+		$properties = $this->getPropertiesForTable($this->table);
110
+		$defaultProperties = array_filter($properties, function (AbstractBeanPropertyDescriptor $property) {
111
+			return $property->hasDefault();
112
+		});
113
+
114
+		return $defaultProperties;
115
+	}
116
+
117
+	/**
118
+	 * Returns the list of properties exposed as getters and setters in this class.
119
+	 *
120
+	 * @return AbstractBeanPropertyDescriptor[]
121
+	 */
122
+	public function getExposedProperties()
123
+	{
124
+		$exposedProperties = array_filter($this->beanPropertyDescriptors, function (AbstractBeanPropertyDescriptor $property) {
125
+			return $property->getTable()->getName() == $this->table->getName();
126
+		});
127
+
128
+		return $exposedProperties;
129
+	}
130
+
131
+	/**
132
+	 * Returns the list of properties for this table (including parent tables).
133
+	 *
134
+	 * @param Table $table
135
+	 *
136
+	 * @return AbstractBeanPropertyDescriptor[]
137
+	 */
138
+	private function getProperties(Table $table)
139
+	{
140
+		$parentRelationship = $this->schemaAnalyzer->getParentRelationship($table->getName());
141
+		if ($parentRelationship) {
142
+			$parentTable = $this->schema->getTable($parentRelationship->getForeignTableName());
143
+			$properties = $this->getProperties($parentTable);
144
+			// we merge properties by overriding property names.
145
+			$localProperties = $this->getPropertiesForTable($table);
146
+			foreach ($localProperties as $name => $property) {
147
+				// We do not override properties if this is a primary key!
148
+				if ($property->isPrimaryKey()) {
149
+					continue;
150
+				}
151
+				$properties[$name] = $property;
152
+			}
153
+		} else {
154
+			$properties = $this->getPropertiesForTable($table);
155
+		}
156
+
157
+		return $properties;
158
+	}
159
+
160
+	/**
161
+	 * Returns the list of properties for this table (ignoring parent tables).
162
+	 *
163
+	 * @param Table $table
164
+	 *
165
+	 * @return AbstractBeanPropertyDescriptor[]
166
+	 */
167
+	private function getPropertiesForTable(Table $table)
168
+	{
169
+		$parentRelationship = $this->schemaAnalyzer->getParentRelationship($table->getName());
170
+		if ($parentRelationship) {
171
+			$ignoreColumns = $parentRelationship->getLocalColumns();
172
+		} else {
173
+			$ignoreColumns = [];
174
+		}
175
+
176
+		$beanPropertyDescriptors = [];
177
+
178
+		foreach ($table->getColumns() as $column) {
179
+			if (array_search($column->getName(), $ignoreColumns) !== false) {
180
+				continue;
181
+			}
182
+
183
+			$fk = $this->isPartOfForeignKey($table, $column);
184
+			if ($fk !== null) {
185
+				// Check that previously added descriptors are not added on same FK (can happen with multi key FK).
186
+				foreach ($beanPropertyDescriptors as $beanDescriptor) {
187
+					if ($beanDescriptor instanceof ObjectBeanPropertyDescriptor && $beanDescriptor->getForeignKey() === $fk) {
188
+						continue 2;
189
+					}
190
+				}
191
+				// Check that this property is not an inheritance relationship
192
+				$parentRelationship = $this->schemaAnalyzer->getParentRelationship($table->getName());
193
+				if ($parentRelationship === $fk) {
194
+					continue;
195
+				}
196
+
197
+				$beanPropertyDescriptors[] = new ObjectBeanPropertyDescriptor($table, $fk, $this->schemaAnalyzer);
198
+			} else {
199
+				$beanPropertyDescriptors[] = new ScalarBeanPropertyDescriptor($table, $column);
200
+			}
201
+		}
202
+
203
+		// Now, let's get the name of all properties and let's check there is no duplicate.
204
+		/** @var $names AbstractBeanPropertyDescriptor[] */
205
+		$names = [];
206
+		foreach ($beanPropertyDescriptors as $beanDescriptor) {
207
+			$name = $beanDescriptor->getUpperCamelCaseName();
208
+			if (isset($names[$name])) {
209
+				$names[$name]->useAlternativeName();
210
+				$beanDescriptor->useAlternativeName();
211
+			} else {
212
+				$names[$name] = $beanDescriptor;
213
+			}
214
+		}
215
+
216
+		// Final check (throw exceptions if problem arises)
217
+		$names = [];
218
+		foreach ($beanPropertyDescriptors as $beanDescriptor) {
219
+			$name = $beanDescriptor->getUpperCamelCaseName();
220
+			if (isset($names[$name])) {
221
+				throw new TDBMException('Unsolvable name conflict while generating method name');
222
+			} else {
223
+				$names[$name] = $beanDescriptor;
224
+			}
225
+		}
226
+
227
+		// Last step, let's rebuild the list with a map:
228
+		$beanPropertyDescriptorsMap = [];
229
+		foreach ($beanPropertyDescriptors as $beanDescriptor) {
230
+			$beanPropertyDescriptorsMap[$beanDescriptor->getLowerCamelCaseName()] = $beanDescriptor;
231
+		}
232
+
233
+		return $beanPropertyDescriptorsMap;
234
+	}
235
+
236
+	public function generateBeanConstructor()
237
+	{
238
+		$constructorProperties = $this->getConstructorProperties();
239
+
240
+		$constructorCode = '    /**
241 241
      * The constructor takes all compulsory arguments.
242 242
      *
243 243
 %s
@@ -248,110 +248,110 @@  discard block
 block discarded – undo
248 248
     }
249 249
     ';
250 250
 
251
-        $paramAnnotations = [];
252
-        $arguments = [];
253
-        $assigns = [];
254
-        $parentConstructorArguments = [];
255
-
256
-        foreach ($constructorProperties as $property) {
257
-            $className = $property->getClassName();
258
-            if ($className) {
259
-                $arguments[] = $className.' '.$property->getVariableName();
260
-            } else {
261
-                $arguments[] = $property->getVariableName();
262
-            }
263
-            $paramAnnotations[] = $property->getParamAnnotation();
264
-            if ($property->getTable()->getName() === $this->table->getName()) {
265
-                $assigns[] = $property->getConstructorAssignCode();
266
-            } else {
267
-                $parentConstructorArguments[] = $property->getVariableName();
268
-            }
269
-        }
270
-
271
-        $parentConstructorCode = sprintf("        parent::__construct(%s);\n", implode(', ', $parentConstructorArguments));
272
-
273
-        foreach ($this->getPropertiesWithDefault() as $property) {
274
-            $assigns[] = $property->assignToDefaultCode();
275
-        }
276
-
277
-        return sprintf($constructorCode, implode("\n", $paramAnnotations), implode(', ', $arguments), $parentConstructorCode, implode("\n", $assigns));
278
-    }
279
-
280
-    public function getDirectForeignKeysDescriptors()
281
-    {
282
-        $fks = $this->tdbmSchemaAnalyzer->getIncomingForeignKeys($this->table->getName());
283
-
284
-        $descriptors = [];
285
-
286
-        foreach ($fks as $fk) {
287
-            $descriptors[] = new DirectForeignKeyMethodDescriptor($fk, $this->table);
288
-        }
289
-
290
-        return $descriptors;
291
-    }
292
-
293
-    private function getPivotTableDescriptors()
294
-    {
295
-        $descs = [];
296
-        foreach ($this->schemaAnalyzer->detectJunctionTables(true) as $table) {
297
-            // There are exactly 2 FKs since this is a pivot table.
298
-            $fks = array_values($table->getForeignKeys());
299
-
300
-            if ($fks[0]->getForeignTableName() === $this->table->getName()) {
301
-                list($localFk, $remoteFk) = $fks;
302
-            } elseif ($fks[1]->getForeignTableName() === $this->table->getName()) {
303
-                list($remoteFk, $localFk) = $fks;
304
-            } else {
305
-                continue;
306
-            }
307
-
308
-            $descs[] = new PivotTableMethodsDescriptor($table, $localFk, $remoteFk);
309
-        }
310
-
311
-        return $descs;
312
-    }
313
-
314
-    /**
315
-     * Returns the list of method descriptors (and applies the alternative name if needed).
316
-     *
317
-     * @return MethodDescriptorInterface[]
318
-     */
319
-    private function getMethodDescriptors()
320
-    {
321
-        $directForeignKeyDescriptors = $this->getDirectForeignKeysDescriptors();
322
-        $pivotTableDescriptors = $this->getPivotTableDescriptors();
323
-
324
-        $descriptors = array_merge($directForeignKeyDescriptors, $pivotTableDescriptors);
325
-
326
-        // Descriptors by method names
327
-        $descriptorsByMethodName = [];
328
-
329
-        foreach ($descriptors as $descriptor) {
330
-            $descriptorsByMethodName[$descriptor->getName()][] = $descriptor;
331
-        }
332
-
333
-        foreach ($descriptorsByMethodName as $descriptorsForMethodName) {
334
-            if (count($descriptorsForMethodName) > 1) {
335
-                foreach ($descriptorsForMethodName as $descriptor) {
336
-                    $descriptor->useAlternativeName();
337
-                }
338
-            }
339
-        }
340
-
341
-        return $descriptors;
342
-    }
343
-
344
-    public function generateJsonSerialize()
345
-    {
346
-        $tableName = $this->table->getName();
347
-        $parentFk = $this->schemaAnalyzer->getParentRelationship($tableName);
348
-        if ($parentFk !== null) {
349
-            $initializer = '$array = parent::jsonSerialize($stopRecursion);';
350
-        } else {
351
-            $initializer = '$array = [];';
352
-        }
353
-
354
-        $str = '
251
+		$paramAnnotations = [];
252
+		$arguments = [];
253
+		$assigns = [];
254
+		$parentConstructorArguments = [];
255
+
256
+		foreach ($constructorProperties as $property) {
257
+			$className = $property->getClassName();
258
+			if ($className) {
259
+				$arguments[] = $className.' '.$property->getVariableName();
260
+			} else {
261
+				$arguments[] = $property->getVariableName();
262
+			}
263
+			$paramAnnotations[] = $property->getParamAnnotation();
264
+			if ($property->getTable()->getName() === $this->table->getName()) {
265
+				$assigns[] = $property->getConstructorAssignCode();
266
+			} else {
267
+				$parentConstructorArguments[] = $property->getVariableName();
268
+			}
269
+		}
270
+
271
+		$parentConstructorCode = sprintf("        parent::__construct(%s);\n", implode(', ', $parentConstructorArguments));
272
+
273
+		foreach ($this->getPropertiesWithDefault() as $property) {
274
+			$assigns[] = $property->assignToDefaultCode();
275
+		}
276
+
277
+		return sprintf($constructorCode, implode("\n", $paramAnnotations), implode(', ', $arguments), $parentConstructorCode, implode("\n", $assigns));
278
+	}
279
+
280
+	public function getDirectForeignKeysDescriptors()
281
+	{
282
+		$fks = $this->tdbmSchemaAnalyzer->getIncomingForeignKeys($this->table->getName());
283
+
284
+		$descriptors = [];
285
+
286
+		foreach ($fks as $fk) {
287
+			$descriptors[] = new DirectForeignKeyMethodDescriptor($fk, $this->table);
288
+		}
289
+
290
+		return $descriptors;
291
+	}
292
+
293
+	private function getPivotTableDescriptors()
294
+	{
295
+		$descs = [];
296
+		foreach ($this->schemaAnalyzer->detectJunctionTables(true) as $table) {
297
+			// There are exactly 2 FKs since this is a pivot table.
298
+			$fks = array_values($table->getForeignKeys());
299
+
300
+			if ($fks[0]->getForeignTableName() === $this->table->getName()) {
301
+				list($localFk, $remoteFk) = $fks;
302
+			} elseif ($fks[1]->getForeignTableName() === $this->table->getName()) {
303
+				list($remoteFk, $localFk) = $fks;
304
+			} else {
305
+				continue;
306
+			}
307
+
308
+			$descs[] = new PivotTableMethodsDescriptor($table, $localFk, $remoteFk);
309
+		}
310
+
311
+		return $descs;
312
+	}
313
+
314
+	/**
315
+	 * Returns the list of method descriptors (and applies the alternative name if needed).
316
+	 *
317
+	 * @return MethodDescriptorInterface[]
318
+	 */
319
+	private function getMethodDescriptors()
320
+	{
321
+		$directForeignKeyDescriptors = $this->getDirectForeignKeysDescriptors();
322
+		$pivotTableDescriptors = $this->getPivotTableDescriptors();
323
+
324
+		$descriptors = array_merge($directForeignKeyDescriptors, $pivotTableDescriptors);
325
+
326
+		// Descriptors by method names
327
+		$descriptorsByMethodName = [];
328
+
329
+		foreach ($descriptors as $descriptor) {
330
+			$descriptorsByMethodName[$descriptor->getName()][] = $descriptor;
331
+		}
332
+
333
+		foreach ($descriptorsByMethodName as $descriptorsForMethodName) {
334
+			if (count($descriptorsForMethodName) > 1) {
335
+				foreach ($descriptorsForMethodName as $descriptor) {
336
+					$descriptor->useAlternativeName();
337
+				}
338
+			}
339
+		}
340
+
341
+		return $descriptors;
342
+	}
343
+
344
+	public function generateJsonSerialize()
345
+	{
346
+		$tableName = $this->table->getName();
347
+		$parentFk = $this->schemaAnalyzer->getParentRelationship($tableName);
348
+		if ($parentFk !== null) {
349
+			$initializer = '$array = parent::jsonSerialize($stopRecursion);';
350
+		} else {
351
+			$initializer = '$array = [];';
352
+		}
353
+
354
+		$str = '
355 355
     /**
356 356
      * Serializes the object for JSON encoding.
357 357
      *
@@ -367,76 +367,76 @@  discard block
 block discarded – undo
367 367
     }
368 368
 ';
369 369
 
370
-        $propertiesCode = '';
371
-        foreach ($this->beanPropertyDescriptors as $beanPropertyDescriptor) {
372
-            $propertiesCode .= $beanPropertyDescriptor->getJsonSerializeCode();
373
-        }
374
-
375
-        // Many2many relationships
376
-        $methodsCode = '';
377
-        foreach ($this->getMethodDescriptors() as $methodDescriptor) {
378
-            $methodsCode .= $methodDescriptor->getJsonSerializeCode();
379
-        }
380
-
381
-        return sprintf($str, $initializer, $propertiesCode, $methodsCode);
382
-    }
383
-
384
-    /**
385
-     * Returns as an array the class we need to extend from and the list of use statements.
386
-     *
387
-     * @return array
388
-     */
389
-    private function generateExtendsAndUseStatements(ForeignKeyConstraint $parentFk = null)
390
-    {
391
-        $classes = [];
392
-        if ($parentFk !== null) {
393
-            $extends = TDBMDaoGenerator::getBeanNameFromTableName($parentFk->getForeignTableName());
394
-            $classes[] = $extends;
395
-        }
396
-
397
-        foreach ($this->getBeanPropertyDescriptors() as $beanPropertyDescriptor) {
398
-            $className = $beanPropertyDescriptor->getClassName();
399
-            if (null !== $className) {
400
-                $classes[] = $beanPropertyDescriptor->getClassName();
401
-            }
402
-        }
403
-
404
-        foreach ($this->getMethodDescriptors() as $descriptor) {
405
-            $classes = array_merge($classes, $descriptor->getUsedClasses());
406
-        }
407
-
408
-        $classes = array_unique($classes);
409
-
410
-        return $classes;
411
-    }
412
-
413
-    /**
414
-     * Writes the PHP bean file with all getters and setters from the table passed in parameter.
415
-     *
416
-     * @param string $beannamespace The namespace of the bean
417
-     */
418
-    public function generatePhpCode($beannamespace)
419
-    {
420
-        $tableName = $this->table->getName();
421
-        $baseClassName = TDBMDaoGenerator::getBaseBeanNameFromTableName($tableName);
422
-        $className = TDBMDaoGenerator::getBeanNameFromTableName($tableName);
423
-        $parentFk = $this->schemaAnalyzer->getParentRelationship($tableName);
424
-
425
-        $classes = $this->generateExtendsAndUseStatements($parentFk);
426
-
427
-        $uses = array_map(function ($className) use ($beannamespace) {
428
-            return 'use '.$beannamespace.'\\'.$className.";\n";
429
-        }, $classes);
430
-        $use = implode('', $uses);
431
-
432
-        if ($parentFk !== null) {
433
-            $extends = TDBMDaoGenerator::getBeanNameFromTableName($parentFk->getForeignTableName());
434
-        } else {
435
-            $extends = 'AbstractTDBMObject';
436
-            $use .= "use Mouf\\Database\\TDBM\\AbstractTDBMObject;\n";
437
-        }
438
-
439
-        $str = "<?php
370
+		$propertiesCode = '';
371
+		foreach ($this->beanPropertyDescriptors as $beanPropertyDescriptor) {
372
+			$propertiesCode .= $beanPropertyDescriptor->getJsonSerializeCode();
373
+		}
374
+
375
+		// Many2many relationships
376
+		$methodsCode = '';
377
+		foreach ($this->getMethodDescriptors() as $methodDescriptor) {
378
+			$methodsCode .= $methodDescriptor->getJsonSerializeCode();
379
+		}
380
+
381
+		return sprintf($str, $initializer, $propertiesCode, $methodsCode);
382
+	}
383
+
384
+	/**
385
+	 * Returns as an array the class we need to extend from and the list of use statements.
386
+	 *
387
+	 * @return array
388
+	 */
389
+	private function generateExtendsAndUseStatements(ForeignKeyConstraint $parentFk = null)
390
+	{
391
+		$classes = [];
392
+		if ($parentFk !== null) {
393
+			$extends = TDBMDaoGenerator::getBeanNameFromTableName($parentFk->getForeignTableName());
394
+			$classes[] = $extends;
395
+		}
396
+
397
+		foreach ($this->getBeanPropertyDescriptors() as $beanPropertyDescriptor) {
398
+			$className = $beanPropertyDescriptor->getClassName();
399
+			if (null !== $className) {
400
+				$classes[] = $beanPropertyDescriptor->getClassName();
401
+			}
402
+		}
403
+
404
+		foreach ($this->getMethodDescriptors() as $descriptor) {
405
+			$classes = array_merge($classes, $descriptor->getUsedClasses());
406
+		}
407
+
408
+		$classes = array_unique($classes);
409
+
410
+		return $classes;
411
+	}
412
+
413
+	/**
414
+	 * Writes the PHP bean file with all getters and setters from the table passed in parameter.
415
+	 *
416
+	 * @param string $beannamespace The namespace of the bean
417
+	 */
418
+	public function generatePhpCode($beannamespace)
419
+	{
420
+		$tableName = $this->table->getName();
421
+		$baseClassName = TDBMDaoGenerator::getBaseBeanNameFromTableName($tableName);
422
+		$className = TDBMDaoGenerator::getBeanNameFromTableName($tableName);
423
+		$parentFk = $this->schemaAnalyzer->getParentRelationship($tableName);
424
+
425
+		$classes = $this->generateExtendsAndUseStatements($parentFk);
426
+
427
+		$uses = array_map(function ($className) use ($beannamespace) {
428
+			return 'use '.$beannamespace.'\\'.$className.";\n";
429
+		}, $classes);
430
+		$use = implode('', $uses);
431
+
432
+		if ($parentFk !== null) {
433
+			$extends = TDBMDaoGenerator::getBeanNameFromTableName($parentFk->getForeignTableName());
434
+		} else {
435
+			$extends = 'AbstractTDBMObject';
436
+			$use .= "use Mouf\\Database\\TDBM\\AbstractTDBMObject;\n";
437
+		}
438
+
439
+		$str = "<?php
440 440
 namespace {$beannamespace}\\Generated;
441 441
 
442 442
 use Mouf\\Database\\TDBM\\ResultIterator;
@@ -456,129 +456,129 @@  discard block
 block discarded – undo
456 456
 {
457 457
 ";
458 458
 
459
-        $str .= $this->generateBeanConstructor();
459
+		$str .= $this->generateBeanConstructor();
460 460
 
461
-        foreach ($this->getExposedProperties() as $property) {
462
-            $str .= $property->getGetterSetterCode();
463
-        }
461
+		foreach ($this->getExposedProperties() as $property) {
462
+			$str .= $property->getGetterSetterCode();
463
+		}
464 464
 
465
-        foreach ($this->getMethodDescriptors() as $methodDescriptor) {
466
-            $str .= $methodDescriptor->getCode();
467
-        }
468
-        $str .= $this->generateJsonSerialize();
465
+		foreach ($this->getMethodDescriptors() as $methodDescriptor) {
466
+			$str .= $methodDescriptor->getCode();
467
+		}
468
+		$str .= $this->generateJsonSerialize();
469 469
 
470
-        $str .= $this->generateGetUsedTablesCode();
470
+		$str .= $this->generateGetUsedTablesCode();
471 471
 
472
-        $str .= $this->generateOnDeleteCode();
472
+		$str .= $this->generateOnDeleteCode();
473 473
 
474
-        $str .= '}
474
+		$str .= '}
475 475
 ';
476 476
 
477
-        return $str;
478
-    }
479
-
480
-    /**
481
-     * @param string $beanNamespace
482
-     * @param string $beanClassName
483
-     *
484
-     * @return array first element: list of used beans, second item: PHP code as a string
485
-     */
486
-    public function generateFindByDaoCode($beanNamespace, $beanClassName)
487
-    {
488
-        $code = '';
489
-        $usedBeans = [];
490
-        foreach ($this->table->getIndexes() as $index) {
491
-            if (!$index->isPrimary()) {
492
-                list($usedBeansForIndex, $codeForIndex) = $this->generateFindByDaoCodeForIndex($index, $beanNamespace, $beanClassName);
493
-                $code .= $codeForIndex;
494
-                $usedBeans = array_merge($usedBeans, $usedBeansForIndex);
495
-            }
496
-        }
497
-
498
-        return [$usedBeans, $code];
499
-    }
500
-
501
-    /**
502
-     * @param Index  $index
503
-     * @param string $beanNamespace
504
-     * @param string $beanClassName
505
-     *
506
-     * @return array first element: list of used beans, second item: PHP code as a string
507
-     */
508
-    private function generateFindByDaoCodeForIndex(Index $index, $beanNamespace, $beanClassName)
509
-    {
510
-        $columns = $index->getColumns();
511
-        $usedBeans = [];
512
-
513
-        /*
477
+		return $str;
478
+	}
479
+
480
+	/**
481
+	 * @param string $beanNamespace
482
+	 * @param string $beanClassName
483
+	 *
484
+	 * @return array first element: list of used beans, second item: PHP code as a string
485
+	 */
486
+	public function generateFindByDaoCode($beanNamespace, $beanClassName)
487
+	{
488
+		$code = '';
489
+		$usedBeans = [];
490
+		foreach ($this->table->getIndexes() as $index) {
491
+			if (!$index->isPrimary()) {
492
+				list($usedBeansForIndex, $codeForIndex) = $this->generateFindByDaoCodeForIndex($index, $beanNamespace, $beanClassName);
493
+				$code .= $codeForIndex;
494
+				$usedBeans = array_merge($usedBeans, $usedBeansForIndex);
495
+			}
496
+		}
497
+
498
+		return [$usedBeans, $code];
499
+	}
500
+
501
+	/**
502
+	 * @param Index  $index
503
+	 * @param string $beanNamespace
504
+	 * @param string $beanClassName
505
+	 *
506
+	 * @return array first element: list of used beans, second item: PHP code as a string
507
+	 */
508
+	private function generateFindByDaoCodeForIndex(Index $index, $beanNamespace, $beanClassName)
509
+	{
510
+		$columns = $index->getColumns();
511
+		$usedBeans = [];
512
+
513
+		/*
514 514
          * The list of elements building this index (expressed as columns or foreign keys)
515 515
          * @var AbstractBeanPropertyDescriptor[]
516 516
          */
517
-        $elements = [];
518
-
519
-        foreach ($columns as $column) {
520
-            $fk = $this->isPartOfForeignKey($this->table, $this->table->getColumn($column));
521
-            if ($fk !== null) {
522
-                if (!in_array($fk, $elements)) {
523
-                    $elements[] = new ObjectBeanPropertyDescriptor($this->table, $fk, $this->schemaAnalyzer);
524
-                }
525
-            } else {
526
-                $elements[] = new ScalarBeanPropertyDescriptor($this->table, $this->table->getColumn($column));
527
-            }
528
-        }
529
-
530
-        // If the index is actually only a foreign key, let's bypass it entirely.
531
-        if (count($elements) === 1 && $elements[0] instanceof ObjectBeanPropertyDescriptor) {
532
-            return [[], ''];
533
-        }
534
-
535
-        $methodNameComponent = [];
536
-        $functionParameters = [];
537
-        $first = true;
538
-        foreach ($elements as $element) {
539
-            $methodNameComponent[] = $element->getUpperCamelCaseName();
540
-            $functionParameter = $element->getClassName();
541
-            if ($functionParameter) {
542
-                $usedBeans[] = $beanNamespace.'\\'.$functionParameter;
543
-                $functionParameter .= ' ';
544
-            }
545
-            $functionParameter .= $element->getVariableName();
546
-            if ($first) {
547
-                $first = false;
548
-            } else {
549
-                $functionParameter .= ' = null';
550
-            }
551
-            $functionParameters[] = $functionParameter;
552
-        }
553
-        if ($index->isUnique()) {
554
-            $methodName = 'findOneBy'.implode('And', $methodNameComponent);
555
-            $calledMethod = 'findOne';
556
-            $returnType = "{$beanClassName}";
557
-        } else {
558
-            $methodName = 'findBy'.implode('And', $methodNameComponent);
559
-            $returnType = "{$beanClassName}[]|ResultIterator|ResultArray";
560
-            $calledMethod = 'find';
561
-        }
562
-        $functionParametersString = implode(', ', $functionParameters);
563
-
564
-        $count = 0;
565
-
566
-        $params = [];
567
-        $filterArrayCode = '';
568
-        $commentArguments = [];
569
-        foreach ($elements as $element) {
570
-            $params[] = $element->getParamAnnotation();
571
-            if ($element instanceof ScalarBeanPropertyDescriptor) {
572
-                $filterArrayCode .= '            '.var_export($element->getColumnName(), true).' => '.$element->getVariableName().",\n";
573
-            } else {
574
-                ++$count;
575
-                $filterArrayCode .= '            '.$count.' => '.$element->getVariableName().",\n";
576
-            }
577
-            $commentArguments[] = substr($element->getVariableName(), 1);
578
-        }
579
-        $paramsString = implode("\n", $params);
580
-
581
-        $code = "
517
+		$elements = [];
518
+
519
+		foreach ($columns as $column) {
520
+			$fk = $this->isPartOfForeignKey($this->table, $this->table->getColumn($column));
521
+			if ($fk !== null) {
522
+				if (!in_array($fk, $elements)) {
523
+					$elements[] = new ObjectBeanPropertyDescriptor($this->table, $fk, $this->schemaAnalyzer);
524
+				}
525
+			} else {
526
+				$elements[] = new ScalarBeanPropertyDescriptor($this->table, $this->table->getColumn($column));
527
+			}
528
+		}
529
+
530
+		// If the index is actually only a foreign key, let's bypass it entirely.
531
+		if (count($elements) === 1 && $elements[0] instanceof ObjectBeanPropertyDescriptor) {
532
+			return [[], ''];
533
+		}
534
+
535
+		$methodNameComponent = [];
536
+		$functionParameters = [];
537
+		$first = true;
538
+		foreach ($elements as $element) {
539
+			$methodNameComponent[] = $element->getUpperCamelCaseName();
540
+			$functionParameter = $element->getClassName();
541
+			if ($functionParameter) {
542
+				$usedBeans[] = $beanNamespace.'\\'.$functionParameter;
543
+				$functionParameter .= ' ';
544
+			}
545
+			$functionParameter .= $element->getVariableName();
546
+			if ($first) {
547
+				$first = false;
548
+			} else {
549
+				$functionParameter .= ' = null';
550
+			}
551
+			$functionParameters[] = $functionParameter;
552
+		}
553
+		if ($index->isUnique()) {
554
+			$methodName = 'findOneBy'.implode('And', $methodNameComponent);
555
+			$calledMethod = 'findOne';
556
+			$returnType = "{$beanClassName}";
557
+		} else {
558
+			$methodName = 'findBy'.implode('And', $methodNameComponent);
559
+			$returnType = "{$beanClassName}[]|ResultIterator|ResultArray";
560
+			$calledMethod = 'find';
561
+		}
562
+		$functionParametersString = implode(', ', $functionParameters);
563
+
564
+		$count = 0;
565
+
566
+		$params = [];
567
+		$filterArrayCode = '';
568
+		$commentArguments = [];
569
+		foreach ($elements as $element) {
570
+			$params[] = $element->getParamAnnotation();
571
+			if ($element instanceof ScalarBeanPropertyDescriptor) {
572
+				$filterArrayCode .= '            '.var_export($element->getColumnName(), true).' => '.$element->getVariableName().",\n";
573
+			} else {
574
+				++$count;
575
+				$filterArrayCode .= '            '.$count.' => '.$element->getVariableName().",\n";
576
+			}
577
+			$commentArguments[] = substr($element->getVariableName(), 1);
578
+		}
579
+		$paramsString = implode("\n", $params);
580
+
581
+		$code = "
582 582
     /**
583 583
      * Get a list of $beanClassName filtered by ".implode(', ', $commentArguments).".
584 584
      *
@@ -596,27 +596,27 @@  discard block
 block discarded – undo
596 596
     }
597 597
 ";
598 598
 
599
-        return [$usedBeans, $code];
600
-    }
601
-
602
-    /**
603
-     * Generates the code for the getUsedTable protected method.
604
-     *
605
-     * @return string
606
-     */
607
-    private function generateGetUsedTablesCode()
608
-    {
609
-        $hasParentRelationship = $this->schemaAnalyzer->getParentRelationship($this->table->getName()) !== null;
610
-        if ($hasParentRelationship) {
611
-            $code = sprintf('        $tables = parent::getUsedTables();
599
+		return [$usedBeans, $code];
600
+	}
601
+
602
+	/**
603
+	 * Generates the code for the getUsedTable protected method.
604
+	 *
605
+	 * @return string
606
+	 */
607
+	private function generateGetUsedTablesCode()
608
+	{
609
+		$hasParentRelationship = $this->schemaAnalyzer->getParentRelationship($this->table->getName()) !== null;
610
+		if ($hasParentRelationship) {
611
+			$code = sprintf('        $tables = parent::getUsedTables();
612 612
         $tables[] = %s;
613 613
         
614 614
         return $tables;', var_export($this->table->getName(), true));
615
-        } else {
616
-            $code = sprintf('        return [ %s ];', var_export($this->table->getName(), true));
617
-        }
615
+		} else {
616
+			$code = sprintf('        return [ %s ];', var_export($this->table->getName(), true));
617
+		}
618 618
 
619
-        return sprintf('
619
+		return sprintf('
620 620
     /**
621 621
      * Returns an array of used tables by this bean (from parent to child relationship).
622 622
      *
@@ -627,20 +627,20 @@  discard block
 block discarded – undo
627 627
 %s    
628 628
     }
629 629
 ', $code);
630
-    }
631
-
632
-    private function generateOnDeleteCode()
633
-    {
634
-        $code = '';
635
-        $relationships = $this->getPropertiesForTable($this->table);
636
-        foreach ($relationships as $relationship) {
637
-            if ($relationship instanceof ObjectBeanPropertyDescriptor) {
638
-                $code .= sprintf('        $this->setRef('.var_export($relationship->getForeignKey()->getName(), true).', null, '.var_export($this->table->getName(), true).");\n");
639
-            }
640
-        }
641
-
642
-        if ($code) {
643
-            return sprintf('
630
+	}
631
+
632
+	private function generateOnDeleteCode()
633
+	{
634
+		$code = '';
635
+		$relationships = $this->getPropertiesForTable($this->table);
636
+		foreach ($relationships as $relationship) {
637
+			if ($relationship instanceof ObjectBeanPropertyDescriptor) {
638
+				$code .= sprintf('        $this->setRef('.var_export($relationship->getForeignKey()->getName(), true).', null, '.var_export($this->table->getName(), true).");\n");
639
+			}
640
+		}
641
+
642
+		if ($code) {
643
+			return sprintf('
644 644
     /**
645 645
      * Method called when the bean is removed from database.
646 646
      *
@@ -651,8 +651,8 @@  discard block
 block discarded – undo
651 651
 %s
652 652
     }
653 653
 ', $code);
654
-        }
654
+		}
655 655
 
656
-        return '';
657
-    }
656
+		return '';
657
+	}
658 658
 }
Please login to merge, or discard this patch.
src/Mouf/Database/TDBM/Utils/MethodDescriptorInterface.php 1 patch
Indentation   +28 added lines, -28 removed lines patch added patch discarded remove patch
@@ -4,36 +4,36 @@
 block discarded – undo
4 4
 
5 5
 interface MethodDescriptorInterface
6 6
 {
7
-    /**
8
-     * Returns the name of the method to be generated.
9
-     *
10
-     * @return string
11
-     */
12
-    public function getName() : string;
7
+	/**
8
+	 * Returns the name of the method to be generated.
9
+	 *
10
+	 * @return string
11
+	 */
12
+	public function getName() : string;
13 13
 
14
-    /**
15
-     * Requests the use of an alternative name for this method.
16
-     */
17
-    public function useAlternativeName();
14
+	/**
15
+	 * Requests the use of an alternative name for this method.
16
+	 */
17
+	public function useAlternativeName();
18 18
 
19
-    /**
20
-     * Returns the code of the method.
21
-     *
22
-     * @return string
23
-     */
24
-    public function getCode() : string;
19
+	/**
20
+	 * Returns the code of the method.
21
+	 *
22
+	 * @return string
23
+	 */
24
+	public function getCode() : string;
25 25
 
26
-    /**
27
-     * Returns an array of classes that needs a "use" for this method.
28
-     *
29
-     * @return string[]
30
-     */
31
-    public function getUsedClasses() : array;
26
+	/**
27
+	 * Returns an array of classes that needs a "use" for this method.
28
+	 *
29
+	 * @return string[]
30
+	 */
31
+	public function getUsedClasses() : array;
32 32
 
33
-    /**
34
-     * Returns the code to past in jsonSerialize.
35
-     *
36
-     * @return string
37
-     */
38
-    public function getJsonSerializeCode() : string;
33
+	/**
34
+	 * Returns the code to past in jsonSerialize.
35
+	 *
36
+	 * @return string
37
+	 */
38
+	public function getJsonSerializeCode() : string;
39 39
 }
Please login to merge, or discard this patch.
src/Mouf/Database/TDBM/Utils/DirectForeignKeyMethodDescriptor.php 1 patch
Indentation   +110 added lines, -110 removed lines patch added patch discarded remove patch
@@ -12,65 +12,65 @@  discard block
 block discarded – undo
12 12
  */
13 13
 class DirectForeignKeyMethodDescriptor implements MethodDescriptorInterface
14 14
 {
15
-    /**
16
-     * @var ForeignKeyConstraint
17
-     */
18
-    private $fk;
19
-
20
-    private $useAlternateName = false;
21
-    /**
22
-     * @var Table
23
-     */
24
-    private $mainTable;
25
-
26
-    /**
27
-     * @param ForeignKeyConstraint $fk        The foreign key pointing to our bean
28
-     * @param Table                $mainTable The main table that is pointed to
29
-     */
30
-    public function __construct(ForeignKeyConstraint $fk, Table $mainTable)
31
-    {
32
-        $this->fk = $fk;
33
-        $this->mainTable = $mainTable;
34
-    }
35
-
36
-    /**
37
-     * Returns the name of the method to be generated.
38
-     *
39
-     * @return string
40
-     */
41
-    public function getName() : string
42
-    {
43
-        if (!$this->useAlternateName) {
44
-            return 'get'.TDBMDaoGenerator::toCamelCase($this->fk->getLocalTableName());
45
-        } else {
46
-            $methodName = 'get'.TDBMDaoGenerator::toCamelCase($this->fk->getLocalTableName()).'By';
47
-
48
-            $camelizedColumns = array_map([TDBMDaoGenerator::class, 'toCamelCase'], $this->fk->getLocalColumns());
49
-
50
-            $methodName .= implode('And', $camelizedColumns);
51
-
52
-            return $methodName;
53
-        }
54
-    }
55
-
56
-    /**
57
-     * Requests the use of an alternative name for this method.
58
-     */
59
-    public function useAlternativeName()
60
-    {
61
-        $this->useAlternateName = true;
62
-    }
63
-
64
-    /**
65
-     * Returns the code of the method.
66
-     *
67
-     * @return string
68
-     */
69
-    public function getCode() : string
70
-    {
71
-        $code = '';
72
-
73
-        $getterCode = '    /**
15
+	/**
16
+	 * @var ForeignKeyConstraint
17
+	 */
18
+	private $fk;
19
+
20
+	private $useAlternateName = false;
21
+	/**
22
+	 * @var Table
23
+	 */
24
+	private $mainTable;
25
+
26
+	/**
27
+	 * @param ForeignKeyConstraint $fk        The foreign key pointing to our bean
28
+	 * @param Table                $mainTable The main table that is pointed to
29
+	 */
30
+	public function __construct(ForeignKeyConstraint $fk, Table $mainTable)
31
+	{
32
+		$this->fk = $fk;
33
+		$this->mainTable = $mainTable;
34
+	}
35
+
36
+	/**
37
+	 * Returns the name of the method to be generated.
38
+	 *
39
+	 * @return string
40
+	 */
41
+	public function getName() : string
42
+	{
43
+		if (!$this->useAlternateName) {
44
+			return 'get'.TDBMDaoGenerator::toCamelCase($this->fk->getLocalTableName());
45
+		} else {
46
+			$methodName = 'get'.TDBMDaoGenerator::toCamelCase($this->fk->getLocalTableName()).'By';
47
+
48
+			$camelizedColumns = array_map([TDBMDaoGenerator::class, 'toCamelCase'], $this->fk->getLocalColumns());
49
+
50
+			$methodName .= implode('And', $camelizedColumns);
51
+
52
+			return $methodName;
53
+		}
54
+	}
55
+
56
+	/**
57
+	 * Requests the use of an alternative name for this method.
58
+	 */
59
+	public function useAlternativeName()
60
+	{
61
+		$this->useAlternateName = true;
62
+	}
63
+
64
+	/**
65
+	 * Returns the code of the method.
66
+	 *
67
+	 * @return string
68
+	 */
69
+	public function getCode() : string
70
+	{
71
+		$code = '';
72
+
73
+		$getterCode = '    /**
74 74
      * Returns the list of %s pointing to this bean via the %s column.
75 75
      *
76 76
      * @return %s[]|AlterableResultIterator
@@ -82,55 +82,55 @@  discard block
 block discarded – undo
82 82
 
83 83
 ';
84 84
 
85
-        $beanClass = TDBMDaoGenerator::getBeanNameFromTableName($this->fk->getLocalTableName());
86
-        $code .= sprintf($getterCode,
87
-            $beanClass,
88
-            implode(', ', $this->fk->getColumns()),
89
-            $beanClass,
90
-            $this->getName(),
91
-            var_export($this->fk->getLocalTableName(), true),
92
-            var_export($this->fk->getName(), true),
93
-            var_export($this->fk->getLocalTableName(), true),
94
-            $this->getFilters($this->fk)
95
-        );
96
-
97
-        return $code;
98
-    }
99
-
100
-    private function getFilters(ForeignKeyConstraint $fk) : string
101
-    {
102
-        $counter = 0;
103
-        $parameters = [];
104
-
105
-        $pkColumns = $this->mainTable->getPrimaryKeyColumns();
106
-
107
-        foreach ($fk->getLocalColumns() as $columnName) {
108
-            $pkColumn = $pkColumns[$counter];
109
-            $parameters[] = sprintf('%s => $this->get(%s, %s)', var_export($fk->getLocalTableName().'.'.$columnName, true), var_export($pkColumn, true), var_export($this->fk->getForeignTableName(), true));
110
-            ++$counter;
111
-        }
112
-        $parametersCode = '['.implode(', ', $parameters).']';
113
-
114
-        return $parametersCode;
115
-    }
116
-
117
-    /**
118
-     * Returns an array of classes that needs a "use" for this method.
119
-     *
120
-     * @return string[]
121
-     */
122
-    public function getUsedClasses() : array
123
-    {
124
-        return [TDBMDaoGenerator::getBeanNameFromTableName($this->fk->getForeignTableName())];
125
-    }
126
-
127
-    /**
128
-     * Returns the code to past in jsonSerialize.
129
-     *
130
-     * @return string
131
-     */
132
-    public function getJsonSerializeCode() : string
133
-    {
134
-        return '';
135
-    }
85
+		$beanClass = TDBMDaoGenerator::getBeanNameFromTableName($this->fk->getLocalTableName());
86
+		$code .= sprintf($getterCode,
87
+			$beanClass,
88
+			implode(', ', $this->fk->getColumns()),
89
+			$beanClass,
90
+			$this->getName(),
91
+			var_export($this->fk->getLocalTableName(), true),
92
+			var_export($this->fk->getName(), true),
93
+			var_export($this->fk->getLocalTableName(), true),
94
+			$this->getFilters($this->fk)
95
+		);
96
+
97
+		return $code;
98
+	}
99
+
100
+	private function getFilters(ForeignKeyConstraint $fk) : string
101
+	{
102
+		$counter = 0;
103
+		$parameters = [];
104
+
105
+		$pkColumns = $this->mainTable->getPrimaryKeyColumns();
106
+
107
+		foreach ($fk->getLocalColumns() as $columnName) {
108
+			$pkColumn = $pkColumns[$counter];
109
+			$parameters[] = sprintf('%s => $this->get(%s, %s)', var_export($fk->getLocalTableName().'.'.$columnName, true), var_export($pkColumn, true), var_export($this->fk->getForeignTableName(), true));
110
+			++$counter;
111
+		}
112
+		$parametersCode = '['.implode(', ', $parameters).']';
113
+
114
+		return $parametersCode;
115
+	}
116
+
117
+	/**
118
+	 * Returns an array of classes that needs a "use" for this method.
119
+	 *
120
+	 * @return string[]
121
+	 */
122
+	public function getUsedClasses() : array
123
+	{
124
+		return [TDBMDaoGenerator::getBeanNameFromTableName($this->fk->getForeignTableName())];
125
+	}
126
+
127
+	/**
128
+	 * Returns the code to past in jsonSerialize.
129
+	 *
130
+	 * @return string
131
+	 */
132
+	public function getJsonSerializeCode() : string
133
+	{
134
+		return '';
135
+	}
136 136
 }
Please login to merge, or discard this patch.
src/Mouf/Database/TDBM/Utils/PivotTableMethodsDescriptor.php 1 patch
Indentation   +130 added lines, -130 removed lines patch added patch discarded remove patch
@@ -7,99 +7,99 @@  discard block
 block discarded – undo
7 7
 
8 8
 class PivotTableMethodsDescriptor implements MethodDescriptorInterface
9 9
 {
10
-    /**
11
-     * @var Table
12
-     */
13
-    private $pivotTable;
14
-
15
-    private $useAlternateName = false;
16
-
17
-    /**
18
-     * @var ForeignKeyConstraint
19
-     */
20
-    private $localFk;
21
-
22
-    /**
23
-     * @var ForeignKeyConstraint
24
-     */
25
-    private $remoteFk;
26
-
27
-    /**
28
-     * @param Table                $pivotTable The pivot table
29
-     * @param ForeignKeyConstraint $localFk
30
-     * @param ForeignKeyConstraint $remoteFk
31
-     */
32
-    public function __construct(Table $pivotTable, ForeignKeyConstraint $localFk, ForeignKeyConstraint $remoteFk)
33
-    {
34
-        $this->pivotTable = $pivotTable;
35
-        $this->localFk = $localFk;
36
-        $this->remoteFk = $remoteFk;
37
-    }
38
-
39
-    /**
40
-     * Requests the use of an alternative name for this method.
41
-     */
42
-    public function useAlternativeName()
43
-    {
44
-        $this->useAlternateName = true;
45
-    }
46
-
47
-    /**
48
-     * Returns the name of the method to be generated.
49
-     *
50
-     * @return string
51
-     */
52
-    public function getName() : string
53
-    {
54
-        if (!$this->useAlternateName) {
55
-            return 'get'.TDBMDaoGenerator::toCamelCase($this->remoteFk->getForeignTableName());
56
-        } else {
57
-            return 'get'.TDBMDaoGenerator::toCamelCase($this->remoteFk->getForeignTableName()).'By'.TDBMDaoGenerator::toCamelCase($this->pivotTable->getName());
58
-        }
59
-    }
60
-
61
-    /**
62
-     * Returns the plural name.
63
-     *
64
-     * @return string
65
-     */
66
-    private function getPluralName() : string
67
-    {
68
-        if (!$this->useAlternateName) {
69
-            return TDBMDaoGenerator::toCamelCase($this->remoteFk->getForeignTableName());
70
-        } else {
71
-            return TDBMDaoGenerator::toCamelCase($this->remoteFk->getForeignTableName()).'By'.TDBMDaoGenerator::toCamelCase($this->pivotTable->getName());
72
-        }
73
-    }
74
-
75
-    /**
76
-     * Returns the singular name.
77
-     *
78
-     * @return string
79
-     */
80
-    private function getSingularName() : string
81
-    {
82
-        if (!$this->useAlternateName) {
83
-            return TDBMDaoGenerator::toCamelCase(TDBMDaoGenerator::toSingular($this->remoteFk->getForeignTableName()));
84
-        } else {
85
-            return TDBMDaoGenerator::toCamelCase(TDBMDaoGenerator::toSingular($this->remoteFk->getForeignTableName())).'By'.TDBMDaoGenerator::toCamelCase($this->pivotTable->getName());
86
-        }
87
-    }
88
-
89
-    /**
90
-     * Returns the code of the method.
91
-     *
92
-     * @return string
93
-     */
94
-    public function getCode() : string
95
-    {
96
-        $singularName = $this->getSingularName();
97
-        $pluralName = $this->getPluralName();
98
-        $remoteBeanName = TDBMDaoGenerator::getBeanNameFromTableName($this->remoteFk->getForeignTableName());
99
-        $variableName = '$'.TDBMDaoGenerator::toVariableName($remoteBeanName);
100
-        $pluralVariableName = $variableName.'s';
101
-
102
-        $str = '    /**
10
+	/**
11
+	 * @var Table
12
+	 */
13
+	private $pivotTable;
14
+
15
+	private $useAlternateName = false;
16
+
17
+	/**
18
+	 * @var ForeignKeyConstraint
19
+	 */
20
+	private $localFk;
21
+
22
+	/**
23
+	 * @var ForeignKeyConstraint
24
+	 */
25
+	private $remoteFk;
26
+
27
+	/**
28
+	 * @param Table                $pivotTable The pivot table
29
+	 * @param ForeignKeyConstraint $localFk
30
+	 * @param ForeignKeyConstraint $remoteFk
31
+	 */
32
+	public function __construct(Table $pivotTable, ForeignKeyConstraint $localFk, ForeignKeyConstraint $remoteFk)
33
+	{
34
+		$this->pivotTable = $pivotTable;
35
+		$this->localFk = $localFk;
36
+		$this->remoteFk = $remoteFk;
37
+	}
38
+
39
+	/**
40
+	 * Requests the use of an alternative name for this method.
41
+	 */
42
+	public function useAlternativeName()
43
+	{
44
+		$this->useAlternateName = true;
45
+	}
46
+
47
+	/**
48
+	 * Returns the name of the method to be generated.
49
+	 *
50
+	 * @return string
51
+	 */
52
+	public function getName() : string
53
+	{
54
+		if (!$this->useAlternateName) {
55
+			return 'get'.TDBMDaoGenerator::toCamelCase($this->remoteFk->getForeignTableName());
56
+		} else {
57
+			return 'get'.TDBMDaoGenerator::toCamelCase($this->remoteFk->getForeignTableName()).'By'.TDBMDaoGenerator::toCamelCase($this->pivotTable->getName());
58
+		}
59
+	}
60
+
61
+	/**
62
+	 * Returns the plural name.
63
+	 *
64
+	 * @return string
65
+	 */
66
+	private function getPluralName() : string
67
+	{
68
+		if (!$this->useAlternateName) {
69
+			return TDBMDaoGenerator::toCamelCase($this->remoteFk->getForeignTableName());
70
+		} else {
71
+			return TDBMDaoGenerator::toCamelCase($this->remoteFk->getForeignTableName()).'By'.TDBMDaoGenerator::toCamelCase($this->pivotTable->getName());
72
+		}
73
+	}
74
+
75
+	/**
76
+	 * Returns the singular name.
77
+	 *
78
+	 * @return string
79
+	 */
80
+	private function getSingularName() : string
81
+	{
82
+		if (!$this->useAlternateName) {
83
+			return TDBMDaoGenerator::toCamelCase(TDBMDaoGenerator::toSingular($this->remoteFk->getForeignTableName()));
84
+		} else {
85
+			return TDBMDaoGenerator::toCamelCase(TDBMDaoGenerator::toSingular($this->remoteFk->getForeignTableName())).'By'.TDBMDaoGenerator::toCamelCase($this->pivotTable->getName());
86
+		}
87
+	}
88
+
89
+	/**
90
+	 * Returns the code of the method.
91
+	 *
92
+	 * @return string
93
+	 */
94
+	public function getCode() : string
95
+	{
96
+		$singularName = $this->getSingularName();
97
+		$pluralName = $this->getPluralName();
98
+		$remoteBeanName = TDBMDaoGenerator::getBeanNameFromTableName($this->remoteFk->getForeignTableName());
99
+		$variableName = '$'.TDBMDaoGenerator::toVariableName($remoteBeanName);
100
+		$pluralVariableName = $variableName.'s';
101
+
102
+		$str = '    /**
103 103
      * Returns the list of %s associated to this bean via the %s pivot table.
104 104
      *
105 105
      * @return %s[]
@@ -110,9 +110,9 @@  discard block
 block discarded – undo
110 110
     }
111 111
 ';
112 112
 
113
-        $getterCode = sprintf($str, $remoteBeanName, $this->pivotTable->getName(), $remoteBeanName, $pluralName, var_export($this->remoteFk->getLocalTableName(), true));
113
+		$getterCode = sprintf($str, $remoteBeanName, $this->pivotTable->getName(), $remoteBeanName, $pluralName, var_export($this->remoteFk->getLocalTableName(), true));
114 114
 
115
-        $str = '    /**
115
+		$str = '    /**
116 116
      * Adds a relationship with %s associated to this bean via the %s pivot table.
117 117
      *
118 118
      * @param %s %s
@@ -123,9 +123,9 @@  discard block
 block discarded – undo
123 123
     }
124 124
 ';
125 125
 
126
-        $adderCode = sprintf($str, $remoteBeanName, $this->pivotTable->getName(), $remoteBeanName, $variableName, $singularName, $remoteBeanName, $variableName, var_export($this->remoteFk->getLocalTableName(), true), $variableName);
126
+		$adderCode = sprintf($str, $remoteBeanName, $this->pivotTable->getName(), $remoteBeanName, $variableName, $singularName, $remoteBeanName, $variableName, var_export($this->remoteFk->getLocalTableName(), true), $variableName);
127 127
 
128
-        $str = '    /**
128
+		$str = '    /**
129 129
      * Deletes the relationship with %s associated to this bean via the %s pivot table.
130 130
      *
131 131
      * @param %s %s
@@ -136,9 +136,9 @@  discard block
 block discarded – undo
136 136
     }
137 137
 ';
138 138
 
139
-        $removerCode = sprintf($str, $remoteBeanName, $this->pivotTable->getName(), $remoteBeanName, $variableName, $singularName, $remoteBeanName, $variableName, var_export($this->remoteFk->getLocalTableName(), true), $variableName);
139
+		$removerCode = sprintf($str, $remoteBeanName, $this->pivotTable->getName(), $remoteBeanName, $variableName, $singularName, $remoteBeanName, $variableName, var_export($this->remoteFk->getLocalTableName(), true), $variableName);
140 140
 
141
-        $str = '    /**
141
+		$str = '    /**
142 142
      * Returns whether this bean is associated with %s via the %s pivot table.
143 143
      *
144 144
      * @param %s %s
@@ -150,9 +150,9 @@  discard block
 block discarded – undo
150 150
     }
151 151
 ';
152 152
 
153
-        $hasCode = sprintf($str, $remoteBeanName, $this->pivotTable->getName(), $remoteBeanName, $variableName, $singularName, $remoteBeanName, $variableName, var_export($this->remoteFk->getLocalTableName(), true), $variableName);
153
+		$hasCode = sprintf($str, $remoteBeanName, $this->pivotTable->getName(), $remoteBeanName, $variableName, $singularName, $remoteBeanName, $variableName, var_export($this->remoteFk->getLocalTableName(), true), $variableName);
154 154
 
155
-        $str = '    /**
155
+		$str = '    /**
156 156
      * Sets all relationships with %s associated to this bean via the %s pivot table.
157 157
      * Exiting relationships will be removed and replaced by the provided relationships.
158 158
      *
@@ -164,38 +164,38 @@  discard block
 block discarded – undo
164 164
     }
165 165
 ';
166 166
 
167
-        $setterCode = sprintf($str, $remoteBeanName, $this->pivotTable->getName(), $remoteBeanName, $pluralVariableName, $pluralName, $pluralVariableName, var_export($this->remoteFk->getLocalTableName(), true), $pluralVariableName);
168
-
169
-        $code = $getterCode.$adderCode.$removerCode.$hasCode.$setterCode;
170
-
171
-        return $code;
172
-    }
173
-
174
-    /**
175
-     * Returns an array of classes that needs a "use" for this method.
176
-     *
177
-     * @return string[]
178
-     */
179
-    public function getUsedClasses() : array
180
-    {
181
-        return [TDBMDaoGenerator::getBeanNameFromTableName($this->remoteFk->getForeignTableName())];
182
-    }
183
-
184
-    /**
185
-     * Returns the code to past in jsonSerialize.
186
-     *
187
-     * @return string
188
-     */
189
-    public function getJsonSerializeCode() : string
190
-    {
191
-        $remoteBeanName = TDBMDaoGenerator::getBeanNameFromTableName($this->remoteFk->getForeignTableName());
192
-        $variableName = '$'.TDBMDaoGenerator::toVariableName($remoteBeanName);
193
-
194
-        return '        if (!$stopRecursion) {
167
+		$setterCode = sprintf($str, $remoteBeanName, $this->pivotTable->getName(), $remoteBeanName, $pluralVariableName, $pluralName, $pluralVariableName, var_export($this->remoteFk->getLocalTableName(), true), $pluralVariableName);
168
+
169
+		$code = $getterCode.$adderCode.$removerCode.$hasCode.$setterCode;
170
+
171
+		return $code;
172
+	}
173
+
174
+	/**
175
+	 * Returns an array of classes that needs a "use" for this method.
176
+	 *
177
+	 * @return string[]
178
+	 */
179
+	public function getUsedClasses() : array
180
+	{
181
+		return [TDBMDaoGenerator::getBeanNameFromTableName($this->remoteFk->getForeignTableName())];
182
+	}
183
+
184
+	/**
185
+	 * Returns the code to past in jsonSerialize.
186
+	 *
187
+	 * @return string
188
+	 */
189
+	public function getJsonSerializeCode() : string
190
+	{
191
+		$remoteBeanName = TDBMDaoGenerator::getBeanNameFromTableName($this->remoteFk->getForeignTableName());
192
+		$variableName = '$'.TDBMDaoGenerator::toVariableName($remoteBeanName);
193
+
194
+		return '        if (!$stopRecursion) {
195 195
             $array[\''.lcfirst($this->getPluralName()).'\'] = array_map(function ('.$remoteBeanName.' '.$variableName.') {
196 196
                 return '.$variableName.'->jsonSerialize(true);
197 197
             }, $this->'.$this->getName().'());
198 198
         }
199 199
 ';
200
-    }
200
+	}
201 201
 }
Please login to merge, or discard this patch.
src/Mouf/Database/TDBM/AlterableResultIterator.php 1 patch
Indentation   +245 added lines, -245 removed lines patch added patch discarded remove patch
@@ -14,274 +14,274 @@
 block discarded – undo
14 14
  */
15 15
 class AlterableResultIterator implements Result, \ArrayAccess, \JsonSerializable
16 16
 {
17
-    /**
18
-     * @var \Iterator|null
19
-     */
20
-    private $resultIterator;
17
+	/**
18
+	 * @var \Iterator|null
19
+	 */
20
+	private $resultIterator;
21 21
 
22
-    /**
23
-     * Key: the object to alter in the result set.
24
-     * Value: "add" => the object will be added to the resultset (if it is not found in it)
25
-     *        "delete" => the object will be removed from the resultset (if found).
26
-     *
27
-     * @var \SplObjectStorage
28
-     */
29
-    private $alterations;
22
+	/**
23
+	 * Key: the object to alter in the result set.
24
+	 * Value: "add" => the object will be added to the resultset (if it is not found in it)
25
+	 *        "delete" => the object will be removed from the resultset (if found).
26
+	 *
27
+	 * @var \SplObjectStorage
28
+	 */
29
+	private $alterations;
30 30
 
31
-    /**
32
-     * The result array from the result set.
33
-     *
34
-     * @var array|null
35
-     */
36
-    private $resultArray;
31
+	/**
32
+	 * The result array from the result set.
33
+	 *
34
+	 * @var array|null
35
+	 */
36
+	private $resultArray;
37 37
 
38
-    /**
39
-     * @param \Iterator|null $resultIterator
40
-     */
41
-    public function __construct(\Iterator $resultIterator = null)
42
-    {
43
-        $this->resultIterator = $resultIterator;
44
-        $this->alterations = new \SplObjectStorage();
45
-    }
38
+	/**
39
+	 * @param \Iterator|null $resultIterator
40
+	 */
41
+	public function __construct(\Iterator $resultIterator = null)
42
+	{
43
+		$this->resultIterator = $resultIterator;
44
+		$this->alterations = new \SplObjectStorage();
45
+	}
46 46
 
47
-    /**
48
-     * Sets a new iterator as the base iterator to be altered.
49
-     *
50
-     * @param \Iterator $resultIterator
51
-     */
52
-    public function setResultIterator(\Iterator $resultIterator)
53
-    {
54
-        $this->resultIterator = $resultIterator;
55
-        $this->resultArray = null;
56
-    }
47
+	/**
48
+	 * Sets a new iterator as the base iterator to be altered.
49
+	 *
50
+	 * @param \Iterator $resultIterator
51
+	 */
52
+	public function setResultIterator(\Iterator $resultIterator)
53
+	{
54
+		$this->resultIterator = $resultIterator;
55
+		$this->resultArray = null;
56
+	}
57 57
 
58
-    /**
59
-     * Returns the non altered result iterator (or null if none exist).
60
-     *
61
-     * @return \Iterator|null
62
-     */
63
-    public function getUnderlyingResultIterator()
64
-    {
65
-        return $this->resultIterator;
66
-    }
58
+	/**
59
+	 * Returns the non altered result iterator (or null if none exist).
60
+	 *
61
+	 * @return \Iterator|null
62
+	 */
63
+	public function getUnderlyingResultIterator()
64
+	{
65
+		return $this->resultIterator;
66
+	}
67 67
 
68
-    /**
69
-     * Adds an additional object to the result set (if not already available).
70
-     *
71
-     * @param $object
72
-     */
73
-    public function add($object)
74
-    {
75
-        $this->alterations->attach($object, 'add');
68
+	/**
69
+	 * Adds an additional object to the result set (if not already available).
70
+	 *
71
+	 * @param $object
72
+	 */
73
+	public function add($object)
74
+	{
75
+		$this->alterations->attach($object, 'add');
76 76
 
77
-        if ($this->resultArray !== null) {
78
-            $foundKey = array_search($object, $this->resultArray, true);
79
-            if ($foundKey === false) {
80
-                $this->resultArray[] = $object;
81
-            }
82
-        }
83
-    }
77
+		if ($this->resultArray !== null) {
78
+			$foundKey = array_search($object, $this->resultArray, true);
79
+			if ($foundKey === false) {
80
+				$this->resultArray[] = $object;
81
+			}
82
+		}
83
+	}
84 84
 
85
-    /**
86
-     * Removes an object from the result set.
87
-     *
88
-     * @param $object
89
-     */
90
-    public function remove($object)
91
-    {
92
-        $this->alterations->attach($object, 'delete');
85
+	/**
86
+	 * Removes an object from the result set.
87
+	 *
88
+	 * @param $object
89
+	 */
90
+	public function remove($object)
91
+	{
92
+		$this->alterations->attach($object, 'delete');
93 93
 
94
-        if ($this->resultArray !== null) {
95
-            $foundKey = array_search($object, $this->resultArray, true);
96
-            if ($foundKey !== false) {
97
-                unset($this->resultArray[$foundKey]);
98
-            }
99
-        }
100
-    }
94
+		if ($this->resultArray !== null) {
95
+			$foundKey = array_search($object, $this->resultArray, true);
96
+			if ($foundKey !== false) {
97
+				unset($this->resultArray[$foundKey]);
98
+			}
99
+		}
100
+	}
101 101
 
102
-    /**
103
-     * Casts the result set to a PHP array.
104
-     *
105
-     * @return array
106
-     */
107
-    public function toArray()
108
-    {
109
-        if ($this->resultArray === null) {
110
-            if ($this->resultIterator !== null) {
111
-                $this->resultArray = iterator_to_array($this->resultIterator);
112
-            } else {
113
-                $this->resultArray = [];
114
-            }
102
+	/**
103
+	 * Casts the result set to a PHP array.
104
+	 *
105
+	 * @return array
106
+	 */
107
+	public function toArray()
108
+	{
109
+		if ($this->resultArray === null) {
110
+			if ($this->resultIterator !== null) {
111
+				$this->resultArray = iterator_to_array($this->resultIterator);
112
+			} else {
113
+				$this->resultArray = [];
114
+			}
115 115
 
116
-            foreach ($this->alterations as $obj) {
117
-                $action = $this->alterations->getInfo(); // return, if exists, associated with cur. obj. data; else NULL
116
+			foreach ($this->alterations as $obj) {
117
+				$action = $this->alterations->getInfo(); // return, if exists, associated with cur. obj. data; else NULL
118 118
 
119
-                $foundKey = array_search($obj, $this->resultArray, true);
119
+				$foundKey = array_search($obj, $this->resultArray, true);
120 120
 
121
-                if ($action === 'add' && $foundKey === false) {
122
-                    $this->resultArray[] = $obj;
123
-                } elseif ($action === 'delete' && $foundKey !== false) {
124
-                    unset($this->resultArray[$foundKey]);
125
-                }
126
-            }
127
-        }
121
+				if ($action === 'add' && $foundKey === false) {
122
+					$this->resultArray[] = $obj;
123
+				} elseif ($action === 'delete' && $foundKey !== false) {
124
+					unset($this->resultArray[$foundKey]);
125
+				}
126
+			}
127
+		}
128 128
 
129
-        return array_values($this->resultArray);
130
-    }
129
+		return array_values($this->resultArray);
130
+	}
131 131
 
132
-    /**
133
-     * Whether a offset exists.
134
-     *
135
-     * @link http://php.net/manual/en/arrayaccess.offsetexists.php
136
-     *
137
-     * @param mixed $offset <p>
138
-     *                      An offset to check for.
139
-     *                      </p>
140
-     *
141
-     * @return bool true on success or false on failure.
142
-     *              </p>
143
-     *              <p>
144
-     *              The return value will be casted to boolean if non-boolean was returned
145
-     *
146
-     * @since 5.0.0
147
-     */
148
-    public function offsetExists($offset)
149
-    {
150
-        return isset($this->toArray()[$offset]);
151
-    }
132
+	/**
133
+	 * Whether a offset exists.
134
+	 *
135
+	 * @link http://php.net/manual/en/arrayaccess.offsetexists.php
136
+	 *
137
+	 * @param mixed $offset <p>
138
+	 *                      An offset to check for.
139
+	 *                      </p>
140
+	 *
141
+	 * @return bool true on success or false on failure.
142
+	 *              </p>
143
+	 *              <p>
144
+	 *              The return value will be casted to boolean if non-boolean was returned
145
+	 *
146
+	 * @since 5.0.0
147
+	 */
148
+	public function offsetExists($offset)
149
+	{
150
+		return isset($this->toArray()[$offset]);
151
+	}
152 152
 
153
-    /**
154
-     * Offset to retrieve.
155
-     *
156
-     * @link http://php.net/manual/en/arrayaccess.offsetget.php
157
-     *
158
-     * @param mixed $offset <p>
159
-     *                      The offset to retrieve.
160
-     *                      </p>
161
-     *
162
-     * @return mixed Can return all value types
163
-     *
164
-     * @since 5.0.0
165
-     */
166
-    public function offsetGet($offset)
167
-    {
168
-        return $this->toArray()[$offset];
169
-    }
153
+	/**
154
+	 * Offset to retrieve.
155
+	 *
156
+	 * @link http://php.net/manual/en/arrayaccess.offsetget.php
157
+	 *
158
+	 * @param mixed $offset <p>
159
+	 *                      The offset to retrieve.
160
+	 *                      </p>
161
+	 *
162
+	 * @return mixed Can return all value types
163
+	 *
164
+	 * @since 5.0.0
165
+	 */
166
+	public function offsetGet($offset)
167
+	{
168
+		return $this->toArray()[$offset];
169
+	}
170 170
 
171
-    /**
172
-     * Offset to set.
173
-     *
174
-     * @link http://php.net/manual/en/arrayaccess.offsetset.php
175
-     *
176
-     * @param mixed $offset <p>
177
-     *                      The offset to assign the value to.
178
-     *                      </p>
179
-     * @param mixed $value  <p>
180
-     *                      The value to set.
181
-     *                      </p>
182
-     *
183
-     * @since 5.0.0
184
-     */
185
-    public function offsetSet($offset, $value)
186
-    {
187
-        throw new TDBMInvalidOperationException('You can set values in a TDBM result set, even in an alterable one. Use the add method instead.');
188
-    }
171
+	/**
172
+	 * Offset to set.
173
+	 *
174
+	 * @link http://php.net/manual/en/arrayaccess.offsetset.php
175
+	 *
176
+	 * @param mixed $offset <p>
177
+	 *                      The offset to assign the value to.
178
+	 *                      </p>
179
+	 * @param mixed $value  <p>
180
+	 *                      The value to set.
181
+	 *                      </p>
182
+	 *
183
+	 * @since 5.0.0
184
+	 */
185
+	public function offsetSet($offset, $value)
186
+	{
187
+		throw new TDBMInvalidOperationException('You can set values in a TDBM result set, even in an alterable one. Use the add method instead.');
188
+	}
189 189
 
190
-    /**
191
-     * Offset to unset.
192
-     *
193
-     * @link http://php.net/manual/en/arrayaccess.offsetunset.php
194
-     *
195
-     * @param mixed $offset <p>
196
-     *                      The offset to unset.
197
-     *                      </p>
198
-     *
199
-     * @since 5.0.0
200
-     */
201
-    public function offsetUnset($offset)
202
-    {
203
-        throw new TDBMInvalidOperationException('You can unset values in a TDBM result set, even in an alterable one. Use the delete method instead.');
204
-    }
190
+	/**
191
+	 * Offset to unset.
192
+	 *
193
+	 * @link http://php.net/manual/en/arrayaccess.offsetunset.php
194
+	 *
195
+	 * @param mixed $offset <p>
196
+	 *                      The offset to unset.
197
+	 *                      </p>
198
+	 *
199
+	 * @since 5.0.0
200
+	 */
201
+	public function offsetUnset($offset)
202
+	{
203
+		throw new TDBMInvalidOperationException('You can unset values in a TDBM result set, even in an alterable one. Use the delete method instead.');
204
+	}
205 205
 
206
-    /**
207
-     * @param int $offset
208
-     *
209
-     * @return \Porpaginas\Page
210
-     */
211
-    public function take($offset, $limit)
212
-    {
213
-        // TODO: replace this with a class implementing the map method.
214
-        return new ArrayPage(array_slice($this->toArray(), $offset, $limit), $offset, $limit, count($this->toArray()));
215
-    }
206
+	/**
207
+	 * @param int $offset
208
+	 *
209
+	 * @return \Porpaginas\Page
210
+	 */
211
+	public function take($offset, $limit)
212
+	{
213
+		// TODO: replace this with a class implementing the map method.
214
+		return new ArrayPage(array_slice($this->toArray(), $offset, $limit), $offset, $limit, count($this->toArray()));
215
+	}
216 216
 
217
-    /**
218
-     * Return the number of all results in the paginatable.
219
-     *
220
-     * @return int
221
-     */
222
-    public function count()
223
-    {
224
-        return count($this->toArray());
225
-    }
217
+	/**
218
+	 * Return the number of all results in the paginatable.
219
+	 *
220
+	 * @return int
221
+	 */
222
+	public function count()
223
+	{
224
+		return count($this->toArray());
225
+	}
226 226
 
227
-    /**
228
-     * Return an iterator over all results of the paginatable.
229
-     *
230
-     * @return Iterator
231
-     */
232
-    public function getIterator()
233
-    {
234
-        if ($this->alterations->count() === 0) {
235
-            if ($this->resultIterator !== null) {
236
-                return $this->resultIterator;
237
-            } else {
238
-                return new \ArrayIterator([]);
239
-            }
240
-        } else {
241
-            return new \ArrayIterator($this->toArray());
242
-        }
243
-    }
227
+	/**
228
+	 * Return an iterator over all results of the paginatable.
229
+	 *
230
+	 * @return Iterator
231
+	 */
232
+	public function getIterator()
233
+	{
234
+		if ($this->alterations->count() === 0) {
235
+			if ($this->resultIterator !== null) {
236
+				return $this->resultIterator;
237
+			} else {
238
+				return new \ArrayIterator([]);
239
+			}
240
+		} else {
241
+			return new \ArrayIterator($this->toArray());
242
+		}
243
+	}
244 244
 
245
-    /**
246
-     * Specify data which should be serialized to JSON.
247
-     *
248
-     * @link http://php.net/manual/en/jsonserializable.jsonserialize.php
249
-     *
250
-     * @return mixed data which can be serialized by <b>json_encode</b>,
251
-     *               which is a value of any type other than a resource
252
-     *
253
-     * @since 5.4.0
254
-     */
255
-    public function jsonSerialize()
256
-    {
257
-        return $this->toArray();
258
-    }
245
+	/**
246
+	 * Specify data which should be serialized to JSON.
247
+	 *
248
+	 * @link http://php.net/manual/en/jsonserializable.jsonserialize.php
249
+	 *
250
+	 * @return mixed data which can be serialized by <b>json_encode</b>,
251
+	 *               which is a value of any type other than a resource
252
+	 *
253
+	 * @since 5.4.0
254
+	 */
255
+	public function jsonSerialize()
256
+	{
257
+		return $this->toArray();
258
+	}
259 259
 
260
-    /**
261
-     * Returns only one value (the first) of the result set.
262
-     * Returns null if no value exists.
263
-     *
264
-     * @return mixed|null
265
-     */
266
-    public function first()
267
-    {
268
-        $page = $this->take(0, 1);
269
-        foreach ($page as $bean) {
270
-            return $bean;
271
-        }
260
+	/**
261
+	 * Returns only one value (the first) of the result set.
262
+	 * Returns null if no value exists.
263
+	 *
264
+	 * @return mixed|null
265
+	 */
266
+	public function first()
267
+	{
268
+		$page = $this->take(0, 1);
269
+		foreach ($page as $bean) {
270
+			return $bean;
271
+		}
272 272
 
273
-        return;
274
-    }
273
+		return;
274
+	}
275 275
 
276
-    /**
277
-     * Returns a new iterator mapping any call using the $callable function.
278
-     *
279
-     * @param callable $callable
280
-     *
281
-     * @return MapIterator
282
-     */
283
-    public function map(callable $callable)
284
-    {
285
-        return new MapIterator($this->getIterator(), $callable);
286
-    }
276
+	/**
277
+	 * Returns a new iterator mapping any call using the $callable function.
278
+	 *
279
+	 * @param callable $callable
280
+	 *
281
+	 * @return MapIterator
282
+	 */
283
+	public function map(callable $callable)
284
+	{
285
+		return new MapIterator($this->getIterator(), $callable);
286
+	}
287 287
 }
Please login to merge, or discard this patch.