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\Propel;
4
 
5
use Faker\Provider\Base;
6
 
7
/**
8
 * Service class for populating a table through a Propel ActiveRecord class.
9
 */
10
class EntityPopulator
11
{
12
    protected $class;
13
    protected $columnFormatters = [];
14
    protected $modifiers = [];
15
 
16
    /**
17
     * @param string $class A Propel ActiveRecord classname
18
     */
19
    public function __construct($class)
20
    {
21
        $this->class = $class;
22
    }
23
 
24
    /**
25
     * @return string
26
     */
27
    public function getClass()
28
    {
29
        return $this->class;
30
    }
31
 
32
    public function setColumnFormatters($columnFormatters)
33
    {
34
        $this->columnFormatters = $columnFormatters;
35
    }
36
 
37
    /**
38
     * @return array
39
     */
40
    public function getColumnFormatters()
41
    {
42
        return $this->columnFormatters;
43
    }
44
 
45
    public function mergeColumnFormattersWith($columnFormatters)
46
    {
47
        $this->columnFormatters = array_merge($this->columnFormatters, $columnFormatters);
48
    }
49
 
50
    /**
51
     * @return array
52
     */
53
    public function guessColumnFormatters(\Faker\Generator $generator)
54
    {
55
        $formatters = [];
56
        $class = $this->class;
57
        $peerClass = $class::PEER;
58
        $tableMap = $peerClass::getTableMap();
59
        $nameGuesser = new \Faker\Guesser\Name($generator);
60
        $columnTypeGuesser = new \Faker\ORM\Propel\ColumnTypeGuesser($generator);
61
 
62
        foreach ($tableMap->getColumns() as $columnMap) {
63
            // skip behavior columns, handled by modifiers
64
            if ($this->isColumnBehavior($columnMap)) {
65
                continue;
66
            }
67
 
68
            if ($columnMap->isForeignKey()) {
69
                $relatedClass = $columnMap->getRelation()->getForeignTable()->getClassname();
70
                $formatters[$columnMap->getPhpName()] = static function ($inserted) use ($relatedClass, $generator) {
71
                    return isset($inserted[$relatedClass]) ? $generator->randomElement($inserted[$relatedClass]) : null;
72
                };
73
 
74
                continue;
75
            }
76
 
77
            if ($columnMap->isPrimaryKey()) {
78
                continue;
79
            }
80
 
81
            if ($formatter = $nameGuesser->guessFormat($columnMap->getPhpName(), $columnMap->getSize())) {
82
                $formatters[$columnMap->getPhpName()] = $formatter;
83
 
84
                continue;
85
            }
86
 
87
            if ($formatter = $columnTypeGuesser->guessFormat($columnMap)) {
88
                $formatters[$columnMap->getPhpName()] = $formatter;
89
 
90
                continue;
91
            }
92
        }
93
 
94
        return $formatters;
95
    }
96
 
97
    /**
98
     * @return bool
99
     */
100
    protected function isColumnBehavior(\ColumnMap $columnMap)
101
    {
102
        foreach ($columnMap->getTable()->getBehaviors() as $name => $params) {
103
            $columnName = Base::toLower($columnMap->getName());
104
 
105
            switch ($name) {
106
                case 'nested_set':
107
                    $columnNames = [$params['left_column'], $params['right_column'], $params['level_column']];
108
 
109
                    if (in_array($columnName, $columnNames, false)) {
110
                        return true;
111
                    }
112
 
113
                    break;
114
 
115
                case 'timestampable':
116
                    $columnNames = [$params['create_column'], $params['update_column']];
117
 
118
                    if (in_array($columnName, $columnNames, false)) {
119
                        return true;
120
                    }
121
 
122
                    break;
123
            }
124
        }
125
 
126
        return false;
127
    }
128
 
129
    public function setModifiers($modifiers)
130
    {
131
        $this->modifiers = $modifiers;
132
    }
133
 
134
    /**
135
     * @return array
136
     */
137
    public function getModifiers()
138
    {
139
        return $this->modifiers;
140
    }
141
 
142
    public function mergeModifiersWith($modifiers)
143
    {
144
        $this->modifiers = array_merge($this->modifiers, $modifiers);
145
    }
146
 
147
    /**
148
     * @return array
149
     */
150
    public function guessModifiers(\Faker\Generator $generator)
151
    {
152
        $modifiers = [];
153
        $class = $this->class;
154
        $peerClass = $class::PEER;
155
        $tableMap = $peerClass::getTableMap();
156
 
157
        foreach ($tableMap->getBehaviors() as $name => $params) {
158
            switch ($name) {
159
                case 'nested_set':
160
                    $modifiers['nested_set'] = static function ($obj, $inserted) use ($class, $generator): void {
161
                        if (isset($inserted[$class])) {
162
                            $queryClass = $class . 'Query';
163
                            $parent = $queryClass::create()->findPk($generator->randomElement($inserted[$class]));
164
                            $obj->insertAsLastChildOf($parent);
165
                        } else {
166
                            $obj->makeRoot();
167
                        }
168
                    };
169
 
170
                    break;
171
 
172
                case 'sortable':
173
                    $modifiers['sortable'] = static function ($obj, $inserted) use ($class, $generator): void {
174
                        $obj->insertAtRank($generator->numberBetween(1, count($inserted[$class] ?? []) + 1));
175
                    };
176
 
177
                    break;
178
            }
179
        }
180
 
181
        return $modifiers;
182
    }
183
 
184
    /**
185
     * Insert one new record using the Entity class.
186
     */
187
    public function execute($con, $insertedEntities)
188
    {
189
        $obj = new $this->class();
190
 
191
        foreach ($this->getColumnFormatters() as $column => $format) {
192
            if (null !== $format) {
193
                $obj->setByName($column, is_callable($format) ? $format($insertedEntities, $obj) : $format);
194
            }
195
        }
196
 
197
        foreach ($this->getModifiers() as $modifier) {
198
            $modifier($obj, $insertedEntities);
199
        }
200
        $obj->save($con);
201
 
202
        return $obj->getPrimaryKey();
203
    }
204
}