Skip to content

Commit

Permalink
Merge pull request #53 from Zizaco/refactor-factory
Browse files Browse the repository at this point in the history
Cleanup the way we get the id from factory Kind
  • Loading branch information
scottrobertson committed Jul 6, 2014
2 parents 3695491 + a49b500 commit f71df5a
Showing 1 changed file with 29 additions and 13 deletions.
42 changes: 29 additions & 13 deletions src/Zizaco/FactoryMuff/Kind/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,40 @@

class Factory extends Kind
{
private $methods = array(
'getKey',
'pk',
);

private $properties = array(
'id',
'_id'
);

public function generate()
{
$factory = new \Zizaco\FactoryMuff\FactoryMuff;
$related = $factory->create(substr($this->kind, 8));

if (method_exists($related, 'getKey')) {
return $related->getKey();
} elseif (method_exists($related, 'pk')) { // Kohana Primary Key

return $related->pk();
} elseif (isset($related->id)) { // id Attribute
$model = $factory->create(substr($this->kind, 8));
return $this->getId($model);
}

return $related->id;
} elseif (isset($related->_id)) { // Mongo _id attribute
private function getId($model)
{
// Check to see if we can get an ID via our defined methods
foreach ($this->methods as $method) {
if (method_exists($model, $method)) {
return $model->$method();
}
}

return $related->_id;
} else {
return null;
// Check to see if we can get an ID via our defined methods
foreach ($this->properties as $property) {
if (isset($model->$property)) {
return $model->$property;
}
}

// We cannot find an ID
return null;
}
}

0 comments on commit f71df5a

Please sign in to comment.