/
AbstractObjectBuilder.php
203 lines (177 loc) · 7.2 KB
/
AbstractObjectBuilder.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
<?php
/**
* This file is part of the Propel package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT License
*/
namespace Propel\Generator\Builder\Om;
use Propel\Generator\Model\PropelTypes;
use Propel\Generator\Model\Table;
/**
* Base class for Peer-building classes.
*
* This class is designed so that it can be extended the "standard" PeerBuilder
* and ComplexOMPeerBuilder. Hence, this class should not have any actual
* template code in it -- simply basic logic & utility methods.
*
* @author Hans Lellelid <hans@xmpl.org>
*/
abstract class AbstractObjectBuilder extends AbstractOMBuilder
{
/**
* Constructs a new PeerBuilder subclass.
*/
public function __construct(Table $table) {
parent::__construct($table);
}
/**
* This method adds the contents of the generated class to the script.
*
* This method is abstract and should be overridden by the subclasses.
*
* Hint: Override this method in your subclass if you want to reorganize or
* drastically change the contents of the generated peer class.
*
* @param string &$script The script will be modified in this method.
*/
abstract protected function addClassBody(&$script);
/**
* Adds the getter methods for the column values.
* This is here because it is probably generic enough to apply to templates being generated
* in different langauges (e.g. PHP4 and PHP5).
* @param string &$script The script will be modified in this method.
*/
protected function addColumnAccessorMethods(&$script)
{
$table = $this->getTable();
foreach ($table->getColumns() as $col) {
// if they're not using the DateTime class than we will generate "compatibility" accessor method
if ($col->getType() === PropelTypes::DATE || $col->getType() === PropelTypes::TIME || $col->getType() === PropelTypes::TIMESTAMP) {
$this->addTemporalAccessor($script, $col);
} elseif ($col->getType() === PropelTypes::OBJECT) {
$this->addObjectAccessor($script, $col);
} elseif ($col->getType() === PropelTypes::PHP_ARRAY) {
$this->addArrayAccessor($script, $col);
if ($col->isNamePlural()) {
$this->addHasArrayElement($script, $col);
}
} elseif ($col->isEnumType()) {
$this->addEnumAccessor($script, $col);
} else {
$this->addDefaultAccessor($script, $col);
}
if ($col->isLazyLoad()) {
$this->addLazyLoader($script, $col);
}
}
}
/**
* Adds the mutator (setter) methods for setting column values.
* This is here because it is probably generic enough to apply to templates being generated
* in different langauges (e.g. PHP4 and PHP5).
* @param string &$script The script will be modified in this method.
*/
protected function addColumnMutatorMethods(&$script)
{
foreach ($this->getTable()->getColumns() as $col) {
if ($col->isLobType()) {
$this->addLobMutator($script, $col);
} elseif ($col->getType() === PropelTypes::DATE || $col->getType() === PropelTypes::TIME || $col->getType() === PropelTypes::TIMESTAMP) {
$this->addTemporalMutator($script, $col);
} elseif ($col->getType() === PropelTypes::OBJECT) {
$this->addObjectMutator($script, $col);
} elseif ($col->getType() === PropelTypes::PHP_ARRAY) {
$this->addArrayMutator($script, $col);
if ($col->isNamePlural()) {
$this->addAddArrayElement($script, $col);
$this->addRemoveArrayElement($script, $col);
}
} elseif ($col->isEnumType()) {
$this->addEnumMutator($script, $col);
} elseif ($col->isBooleanType()) {
$this->addBooleanMutator($script, $col);
} else {
$this->addDefaultMutator($script, $col);
}
}
}
/**
* Gets the baseClass path if specified for table/db.
* If not, will return 'propel.om.BaseObject'
* @return string
*/
protected function getBaseClass() {
$class = $this->getTable()->getBaseClass();
if ($class === null) {
$class = "propel.om.BaseObject";
}
return $class;
}
/**
* Gets the interface path if specified for current table.
* If not, will return 'propel.om.Persistent'.
* @return string
*/
protected function getInterface() {
$interface = $this->getTable()->getInterface();
if ($interface === null && !$this->getTable()->isReadOnly()) {
$interface = "propel.om.Persistent";
}
return $interface;
}
/**
* Whether to add the generic mutator methods (setByName(), setByPosition(), fromArray()).
* This is based on the build property propel.addGenericMutators, and also whether the
* table is read-only or an alias.
*/
protected function isAddGenericMutators()
{
$table = $this->getTable();
return (!$table->isAlias() && $this->getBuildProperty('addGenericMutators') && !$table->isReadOnly());
}
/**
* Whether to add the generic accessor methods (getByName(), getByPosition(), toArray()).
* This is based on the build property propel.addGenericAccessors, and also whether the
* table is an alias.
*/
protected function isAddGenericAccessors()
{
$table = $this->getTable();
return (!$table->isAlias() && $this->getBuildProperty('addGenericAccessors'));
}
protected function hasDefaultValues()
{
foreach ($this->getTable()->getColumns() as $col) {
if($col->getDefaultValue() !== null) return true;
}
return false;
}
/**
* Checks whether any registered behavior on that table has a modifier for a hook
* @param string $hookName The name of the hook as called from one of this class methods, e.g. "preSave"
* @return boolean
*/
public function hasBehaviorModifier($hookName, $modifier = null)
{
return parent::hasBehaviorModifier($hookName, 'ObjectBuilderModifier');
}
/**
* Checks whether any registered behavior on that table has a modifier for a hook
* @param string $hookName The name of the hook as called from one of this class methods, e.g. "preSave"
* @param string &$script The script will be modified in this method.
*/
public function applyBehaviorModifier($hookName, &$script, $tab = " ")
{
return $this->applyBehaviorModifierBase($hookName, 'ObjectBuilderModifier', $script, $tab);
}
/**
* Checks whether any registered behavior content creator on that table exists a contentName
* @param string $contentName The name of the content as called from one of this class methods, e.g. "parentClassname"
*/
public function getBehaviorContent($contentName)
{
return $this->getBehaviorContentBase($contentName, 'ObjectBuilderModifier');
}
}