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