Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
namespace Faker\ORM\CakePHP;
4
 
5
class Populator
6
{
7
    protected $generator;
8
    protected $entities = [];
9
    protected $quantities = [];
10
    protected $guessers = [];
11
 
12
    public function __construct(\Faker\Generator $generator)
13
    {
14
        $this->generator = $generator;
15
    }
16
 
17
    /**
18
     * @return \Faker\Generator
19
     */
20
    public function getGenerator()
21
    {
22
        return $this->generator;
23
    }
24
 
25
    /**
26
     * @return array
27
     */
28
    public function getGuessers()
29
    {
30
        return $this->guessers;
31
    }
32
 
33
    /**
34
     * @return $this
35
     */
36
    public function removeGuesser($name)
37
    {
38
        if ($this->guessers[$name]) {
39
            unset($this->guessers[$name]);
40
        }
41
 
42
        return $this;
43
    }
44
 
45
    /**
46
     * @throws \Exception
47
     *
48
     * @return $this
49
     */
50
    public function addGuesser($class)
51
    {
52
        if (!is_object($class)) {
53
            $class = new $class($this->generator);
54
        }
55
 
56
        if (!method_exists($class, 'guessFormat')) {
57
            throw new \Exception('Missing required custom guesser method: ' . get_class($class) . '::guessFormat()');
58
        }
59
 
60
        $this->guessers[get_class($class)] = $class;
61
 
62
        return $this;
63
    }
64
 
65
    /**
66
     * @param array $customColumnFormatters
67
     * @param array $customModifiers
68
     *
69
     * @return $this
70
     */
71
    public function addEntity($entity, $number, $customColumnFormatters = [], $customModifiers = [])
72
    {
73
        if (!$entity instanceof EntityPopulator) {
74
            $entity = new EntityPopulator($entity);
75
        }
76
 
77
        $entity->columnFormatters = $entity->guessColumnFormatters($this);
78
 
79
        if ($customColumnFormatters) {
80
            $entity->mergeColumnFormattersWith($customColumnFormatters);
81
        }
82
 
83
        $entity->modifiers = $entity->guessModifiers($this);
84
 
85
        if ($customModifiers) {
86
            $entity->mergeModifiersWith($customModifiers);
87
        }
88
 
89
        $class = $entity->class;
90
        $this->entities[$class] = $entity;
91
        $this->quantities[$class] = $number;
92
 
93
        return $this;
94
    }
95
 
96
    /**
97
     * @param array $options
98
     *
99
     * @return array
100
     */
101
    public function execute($options = [])
102
    {
103
        $insertedEntities = [];
104
 
105
        foreach ($this->quantities as $class => $number) {
106
            for ($i = 0; $i < $number; ++$i) {
107
                $insertedEntities[$class][] = $this->entities[$class]->execute($class, $insertedEntities, $options);
108
            }
109
        }
110
 
111
        return $insertedEntities;
112
    }
113
}