1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Khepin\YamlFixturesBundle\Fixture; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Util\Inflector; |
6
|
|
|
|
7
|
|
|
class OrmYamlFixture extends AbstractFixture |
8
|
|
|
{ |
9
|
|
|
public function createObject($class, $data, $metadata, $options = array()) |
10
|
|
|
{ |
11
|
|
|
$mapping = array_keys($metadata->fieldMappings); |
12
|
|
|
$associations = array_keys($metadata->associationMappings); |
13
|
|
|
|
14
|
|
|
$object = $this->makeInstance($class, $data); |
15
|
|
|
unset($data['__construct']); |
16
|
|
|
|
17
|
|
|
foreach ($data as $field => $value) { |
18
|
|
|
// Add the fields defined in the fistures file |
19
|
|
|
$method = Inflector::camelize('set_' . $field); |
20
|
|
|
// |
21
|
|
|
if (in_array($field, $mapping)) { |
22
|
|
|
// Dates need to be converted to DateTime objects |
23
|
|
|
$type = $metadata->fieldMappings[$field]['type']; |
24
|
|
View Code Duplication |
if ($type == 'datetime' || $type == 'date' || $type == 'time') { |
|
|
|
|
25
|
|
|
$value = new \DateTime($value); |
26
|
|
|
} |
27
|
|
|
$object->$method($value); |
28
|
|
|
} elseif (in_array($field, $associations)) { // This field is an association |
29
|
|
|
if (is_array($value)) { // The field is an array of associations |
30
|
|
|
$referenceArray = array(); |
31
|
|
|
foreach ($value as $referenceObject) { |
32
|
|
|
$referenceArray[] = $this->loader->getReference($referenceObject); |
33
|
|
|
} |
34
|
|
|
$object->$method($referenceArray); |
35
|
|
|
} else { |
36
|
|
|
$object->$method($this->loader->getReference($value)); |
37
|
|
|
} |
38
|
|
|
} else { |
39
|
|
|
// It's a method call that will set a field named differently |
40
|
|
|
// eg: FOSUserBundle ->setPlainPassword sets the password after |
41
|
|
|
// Encrypting it |
42
|
|
|
$object->$method($value); |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
$this->runServiceCalls($object); |
46
|
|
|
|
47
|
|
|
return $object; |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.