PageRenderTime 62ms CodeModel.GetById 40ms app.highlight 8ms RepoModel.GetById 12ms app.codeStats 0ms

/src/Faker/ORM/Propel/EntityPopulator.php

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