Skip to content
This repository has been archived by the owner on Aug 28, 2018. It is now read-only.

Commit

Permalink
Merge pull request #65 from khepin/mongo-constructor-args
Browse files Browse the repository at this point in the history
Mongo constructor args
  • Loading branch information
khepin authored Jul 26, 2017
2 parents 2143b00 + c04d10a commit f496a82
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 25 deletions.
49 changes: 49 additions & 0 deletions Fixture/AbstractFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,55 @@ public function isReverseSaveOrder()
return true;
}

/**
* Extract the constructor arguments
*
* @param array $arguments
* @return mixed
*/
public function constructorArgs($arguments)
{
$constructArguments = array();

if (is_array($arguments)) {
foreach ($arguments as $argument) {
if (is_array($argument)) {
if ($argument['type'] == 'datetime') {
$constructArguments[] = new \DateTime($argument['value']);
} elseif ($argument['type'] == 'reference') {
$constructArguments[] = $this->loader->getReference($argument['value']);
} else {
$constructArguments[] = $argument['value'];
}
} else {
$constructArguments[] = $argument;
}
}
} else {
$constructArguments[] = $arguments;
}

return $constructArguments;
}

/**
* Creates an instance with any given constructor args
*
* @param string $class
* @param array $data
* @return void
*/
public function makeInstance($class, $data)
{
$class = new \ReflectionClass($class);
$constructArguments = [];
if (isset($data['__construct'])) {
$constructArguments = $this->constructorArgs($data['__construct']);
}

return $class->newInstanceArgs($constructArguments);
}

/**
* Creates and returns one object based on the given data and metadata
*
Expand Down
3 changes: 2 additions & 1 deletion Fixture/MongoYamlFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ public function createObject($class, $data, $metadata, $options = array())
$embedded = isset($options['embedded']);
$mapping = array_keys($metadata->fieldMappings);
// Instantiate new object
$object = new $class;
$object = $this->makeInstance($class, $data);
unset($data['__construct']);

foreach ($data as $field => $value) {
// Add the fields defined in the fixtures file
Expand Down
26 changes: 2 additions & 24 deletions Fixture/OrmYamlFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,8 @@ public function createObject($class, $data, $metadata, $options = array())
$mapping = array_keys($metadata->fieldMappings);
$associations = array_keys($metadata->associationMappings);

$class = new \ReflectionClass($class);
$constructArguments = array();
if (isset($data['__construct'])) {
$arguments = $data['__construct'];
if (is_array($arguments)) {
foreach ($arguments as $argument) {
if (is_array($argument)) {
if ($argument['type'] == 'datetime') {
$constructArguments[] = new \DateTime($argument['value']);
} elseif ($argument['type'] == 'reference') {
$constructArguments[] = $this->loader->getReference($argument['value']);
} else {
$constructArguments[] = $argument['value'];
}
} else {
$constructArguments[] = $argument;
}
}
} else {
$constructArguments[] = $arguments;
}
unset($data['__construct']);
}
$object = $class->newInstanceArgs($constructArguments);
$object = $this->makeInstance($class, $data);
unset($data['__construct']);

foreach ($data as $field => $value) {
// Add the fields defined in the fistures file
Expand Down
7 changes: 7 additions & 0 deletions tests/Khepin/Tests/mongo/DataFixtures/cars_constructor.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
model: Khepin\Fixture\Document\Car
tags: [ construct ]
fixtures:
ford:
__construct:
- Ford
- {type: datetime, value: "2012-01-01"}
7 changes: 7 additions & 0 deletions tests/Khepin/Tests/mongo/DataFixtures/owners_constructor.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
model: Khepin\Fixture\Document\Owner
tags: [ construct ]
fixtures:
some_small_company:
__construct:
- {type: invalid, value: "Some Small Company"}
- {type: reference, value: "ford"}

0 comments on commit f496a82

Please sign in to comment.