private function buildSetUp(array $definition)
{
$ret = array();
- $i = 0;
- if (isset($definition['relations']) && is_array($definition['relations']) && ! empty($definition['relations'])) {
+ if (isset($definition['relations']) && is_array($definition['relations']) && !empty($definition['relations'])) {
foreach ($definition['relations'] as $name => $relation) {
- $class = isset($relation['class']) ? $relation['class']:$name;
- $alias = (isset($relation['alias']) && $relation['alias'] !== $relation['class']) ? ' as ' . $relation['alias'] : '';
+ $class = isset($relation['class']) ? $relation['class'] : $name;
+ $alias = (isset($relation['alias']) && $relation['alias'] !== $relation['class']) ? $relation['alias'] : '';
- if ( ! isset($relation['type'])) {
+ if (!isset($relation['type']))
$relation['type'] = IPF_ORM_Relation::ONE;
- }
if ($relation['type'] === IPF_ORM_Relation::ONE ||
$relation['type'] === IPF_ORM_Relation::ONE_COMPOSITE) {
- $ret[$i] = " ".'$this->hasOne(\'' . $class . $alias . '\'';
+ $r = " \$table->hasOne('$class', '$alias'";
} else {
- $ret[$i] = " ".'$this->hasMany(\'' . $class . $alias . '\'';
+ $r = " \$table->hasMany('$class', '$alias'";
}
$a = array();
$a[] = '\'exclude\' => ' . self::varExport($relation['exclude']);
}
- if ( ! empty($a)) {
- $ret[$i] .= ', ' . 'array(';
- $length = strlen($ret[$i]);
- $ret[$i] .= implode(',' . PHP_EOL . str_repeat(' ', $length), $a) . ')';
- }
+ if (!empty($a))
+ $r .= ', array(' . implode(', ', $a) . ')';
- $ret[$i] .= ');'.PHP_EOL;
- $i++;
+ $ret[] = $r.');';
}
}
if (isset($definition['templates']) && is_array($definition['templates']) && !empty($definition['templates'])) {
- $ret[$i] = $this->buildTemplates($definition['templates']);
- $i++;
+ $this->buildTemplates($definition['templates'], $ret);
}
if (isset($definition['actAs']) && is_array($definition['actAs']) && !empty($definition['actAs'])) {
- $ret[$i] = $this->buildActAs($definition['actAs']);
- $i++;
+ $this->buildActAs($definition['actAs'], $ret);
}
if (isset($definition['listeners']) && is_array($definition['listeners']) && !empty($definition['listeners'])) {
- foreach($definition['listeners'] as $listener) {
- $ret[$i] = PHP_EOL.' $this->getTable()->listeners[\''.$listener.'\'] = new '.$listener.'();';
- $i++;
+ foreach ($definition['listeners'] as $listener) {
+ $ret[] = " \$table->listeners['$listener'] = new $listener;";
}
}
- $code = implode(PHP_EOL, $ret);
- $code = trim($code);
-
- // If the body of the function has contents then we need to
- if ($code) {
- // If the body of the function has contents and we are using inheritance
- // then we need call the parent::setUp() before the body of the function
- // Class table inheritance is the only one we shouldn't call parent::setUp() for
- if ($code && isset($definition['inheritance']['type']) && $definition['inheritance']['type'] != 'class_table') {
- $code = "parent::setUp();" . PHP_EOL . ' ' . $code;
- }
+ // If the body of the function has contents and we are using inheritance
+ // then we need call the parent::setUp() before the body of the function
+ // Class table inheritance is the only one we shouldn't call parent::setUp() for
+ if (count($ret) && isset($definition['inheritance']['type']) && $definition['inheritance']['type'] != 'class_table') {
+ array_unshift($ret, ' parent::setUp();');
}
// If we have some code for the function then lets define it and return it
- if ($code) {
- return ' public function setUp()' . PHP_EOL . ' {' . PHP_EOL . ' ' . $code . PHP_EOL . ' }';
+ if (count($ret)) {
+ array_unshift($ret,
+ ' public function setUp()',
+ ' {',
+ ' $table = $this->getTable();'
+ );
+ $ret[] = ' }';
}
+ return $ret;
}
private function buildColumns(array $columns)
return implode(PHP_EOL, $result);
}
- private function buildTemplates(array $templates)
+ private function buildTemplates(array $templates, array &$build)
{
- $build = '';
foreach ($templates as $name => $options) {
-
if (is_array($options) && !empty($options)) {
- $optionsPhp = self::varExport($options);
-
- $build .= " \$this->getTable()->addTemplate('" . $name . "', " . $optionsPhp . ");" . PHP_EOL;
+ $build[] = " \$table->addTemplate('$name', " . self::varExport($options) . ");";
+ } elseif (isset($templates[0])) {
+ $build[] = " \$table->addTemplate('$options');";
} else {
- if (isset($templates[0])) {
- $build .= " \$this->getTable()->addTemplate('" . $options . "');" . PHP_EOL;
- } else {
- $build .= " \$this->getTable()->addTemplate('" . $name . "');" . PHP_EOL;
- }
+ $build[] = " \$table->addTemplate('$name');";
}
}
-
- return $build;
}
- private function buildActAs($actAs)
+ private function buildActAs($actAs, array &$build)
{
// rewrite special case of actAs: [Behavior] which gave [0] => Behavior
if (is_array($actAs) && isset($actAs[0]) && !is_array($actAs[0])) {
if (!is_array($actAs))
$actAs = array($actAs => '');
- $build = '';
- foreach($actAs as $template => $options) {
+ foreach ($actAs as $template => $options) {
// find class matching $name
- if (class_exists("IPF_ORM_Template_$template", true)) {
- $classname = "IPF_ORM_Template_$template";
- } else {
+ if (class_exists('IPF_ORM_Template_'.$template, true))
+ $classname = 'IPF_ORM_Template_'.$template;
+ else
$classname = $template;
- }
if (is_array($options))
$options = self::varExport($options);
else
$options = '';
- $build .= " \$this->getTable()->addTemplate(new $classname($options));" . PHP_EOL;
+ $build[] = " \$table->addTemplate(new $classname($options));";
}
-
- return $build;
}
private function buildAttributes(array $attributes)
$code[] = '{';
$code[] = $this->buildTableDefinition($definition);
$code[] = '';
- $code[] = $this->buildSetUp($definition);
+ $code = array_merge($code, $this->buildSetUp($definition));
$code[] = '';
$code = array_merge($code, $this->buildShortcuts($definition));
$code[] = '}';
}
}
- public function ownsOne()
- {
- $this->_table->bind(func_get_args(), IPF_ORM_Relation::ONE_COMPOSITE);
-
- return $this;
- }
-
- public function ownsMany()
- {
- $this->_table->bind(func_get_args(), IPF_ORM_Relation::MANY_COMPOSITE);
- return $this;
- }
-
- public function hasOne()
- {
- $this->_table->bind(func_get_args(), IPF_ORM_Relation::ONE_AGGREGATE);
- return $this;
- }
-
- public function hasMany()
- {
- $this->_table->bind(func_get_args(), IPF_ORM_Relation::MANY_AGGREGATE);
- return $this;
- }
-
public function bindQueryParts(array $queryParts)
{
$this->_table->bindQueryParts($queryParts);
public function hasRelation($name)
{
- if ( ! isset($this->_pending[$name]) && ! isset($this->_relations[$name])) {
- return false;
- }
-
- return true;
+ return isset($this->_pending[$name]) || isset($this->_relations[$name]);
}
- public function bind($name, $options = array())
+ public function bind($name, $alias, $type, $options=array())
{
- $e = explode(' as ', $name);
- $name = $e[0];
- $alias = isset($e[1]) ? $e[1] : $name;
+ if (!$alias)
+ $alias = $name;
- if ( ! isset($options['type'])) {
- throw new IPF_ORM_Exception('Relation type not set.');
- }
-
- if ($this->hasRelation($alias)) {
- unset($this->relations[$alias]);
- unset($this->_pending[$alias]);
- }
+ unset($this->relations[$alias]);
- $this->_pending[$alias] = array_merge($options, array('class' => $name, 'alias' => $alias));
+ $options['class'] = $name;
+ $options['alias'] = $alias;
+ $options['type'] = $type;
- return $this->_pending[$alias];
+ $this->_pending[$alias] = $options;
}
public function getRelation($alias, $recursive = true)
$parser = $def['refTable']->getRelationParser();
if ( ! $parser->hasRelation($this->_table->getComponentName())) {
- $parser->bind($this->_table->getComponentName(),
- array('type' => IPF_ORM_Relation::ONE,
- 'local' => $def['local'],
- 'foreign' => $idColumnName,
- 'localKey' => true,
- ));
+ $parser->bind($this->_table->getComponentName(), null, IPF_ORM_Relation::ONE, array(
+ 'local' => $def['local'],
+ 'foreign' => $idColumnName,
+ 'localKey' => true,
+ ));
}
- if ( ! $this->hasRelation($def['refClass'])) {
- $this->bind($def['refClass'], array('type' => IPF_ORM_Relation::MANY,
- 'foreign' => $def['local'],
- 'local' => $idColumnName));
+ if (!$this->hasRelation($def['refClass'])) {
+ $this->bind($def['refClass'], null, IPF_ORM_Relation::MANY, array(
+ 'foreign' => $def['local'],
+ 'local' => $idColumnName,
+ ));
}
}
if (in_array($def['class'], $localClasses)) {
return false;
}
- public function bind($args, $type)
+ public function bind($class, $alias, $type, array $options)
{
- $options = array();
- $options['type'] = $type;
-
- if ( ! isset($args[1])) {
- $args[1] = array();
- }
-
- // the following is needed for backwards compatibility
- if (is_string($args[1])) {
- if ( ! isset($args[2])) {
- $args[2] = array();
- } elseif (is_string($args[2])) {
- $args[2] = (array) $args[2];
- }
-
- $classes = array_merge($this->_options['parents'], array($this->getComponentName()));
+ $this->_parser->bind($class, $alias, $type, $options);
+ }
- $e = explode('.', $args[1]);
- if (in_array($e[0], $classes)) {
- if ($options['type'] >= IPF_ORM_Relation::MANY) {
- $options['foreign'] = $e[1];
- } else {
- $options['local'] = $e[1];
- }
- } else {
- $e2 = explode(' as ', $args[0]);
- if ($e[0] !== $e2[0] && ( ! isset($e2[1]) || $e[0] !== $e2[1])) {
- $options['refClass'] = $e[0];
- }
+ public function ownsOne($class, $alias, $options=array())
+ {
+ $this->bind($class, $alias, IPF_ORM_Relation::ONE_COMPOSITE, $options);
+ }
- $options['foreign'] = $e[1];
- }
+ public function ownsMany($class, $alias, $options=array())
+ {
+ $this->bind($class, $alias, IPF_ORM_Relation::MANY_COMPOSITE, $options);
+ }
- $options = array_merge($args[2], $options);
- } else {
- $options = array_merge($args[1], $options);
- }
+ public function hasOne($class, $alias, $options=array())
+ {
+ $this->bind($class, $alias, IPF_ORM_Relation::ONE_AGGREGATE, $options);
+ }
- $this->_parser->bind($args[0], $options);
+ public function hasMany($class, $alias, $options=array())
+ {
+ $this->bind($class, $alias, IPF_ORM_Relation::MANY_AGGREGATE, $options);
}
public function hasRelation($alias)