public function guessFormat($name) { $name = strtolower($name); $generator = $this->generator; if (false !== strpos($name, 'email')) { return function () use($generator) { return $generator->email; }; } if (false !== strpos($name, 'phone')) { return function () use($generator) { return $generator->phoneNumber; }; } if (false !== strpos($name, 'pass')) { return function () use($generator) { return $generator->password; }; } $return = parent::guessFormat($name); if ($return) { return $return; } if (false !== strpos($name, 'company')) { return function () use($generator) { return $generator->company; }; } return false; }
public function guessColumnFormatters(Generator $generator) { $formatters = array(); $model = $this->_model; $nameGuesser = new Name($generator); $columnTypeGuesser = new ColumnTypeGuesser($generator); foreach ($model::schema()->fields() as $field => $config) { if ($field == $model::key()) { continue; } if ($formatter = $nameGuesser->guessFormat($field)) { $formatters[$field] = $formatter; continue; } if ($formatter = $columnTypeGuesser->guessFormat($field, $config['type'])) { $formatters[$field] = $formatter; continue; } } return $formatters; }
public function createFieldsFormatters($columnTypeGuesser, $generator) { $formatters = array(); $nameGuesser = new Name($generator); foreach ($this->class->getFieldNames() as $fieldName) { //echo "pola standardowe: " . $fieldName . "\n"; if ($this->class->isIdentifier($fieldName) || !$this->class->hasField($fieldName)) { //echo "pola standardowe kończę: " . $fieldName . "\n"; continue; } //echo "pola standardowe dalej: " . $fieldName . "\n"; if ($formatter = $nameGuesser->guessFormat($fieldName)) { $formatters[$fieldName] = $formatter; continue; } if ($formatter = $columnTypeGuesser->guessFormat($fieldName, $this->class)) { $formatters[$fieldName] = $formatter; continue; } } return $formatters; }
/** * @param Generator $generator * @return array */ public function guessColumnFormatters(Generator $generator) { $formatters = array(); $nameGuesser = new Name($generator); $columnTypeGuesser = new ColumnTypeGuesser($generator); $fields = $this->mapper->fields(); foreach ($fields as $fieldName => $field) { if ($field['primary'] === true) { continue; } if ($formatter = $nameGuesser->guessFormat($fieldName)) { $formatters[$fieldName] = $formatter; continue; } if ($formatter = $columnTypeGuesser->guessFormat($field)) { $formatters[$fieldName] = $formatter; continue; } } $entityName = $this->mapper->entity(); $entity = $this->mapper->build([]); $relations = $entityName::relations($this->mapper, $entity); foreach ($relations as $relation) { // We don't need any other relation here. if ($relation instanceof BelongsTo) { $fieldName = $relation->localKey(); $entityName = $relation->entityName(); $field = $fields[$fieldName]; $required = $field['required']; $locator = $this->locator; $formatters[$fieldName] = function ($inserted) use($required, $entityName, $locator) { if (!empty($inserted[$entityName])) { return $inserted[$entityName][mt_rand(0, count($inserted[$entityName]) - 1)]->getId(); } else { if ($required && $this->useExistingData) { // We did not add anything like this, but it's required, // So let's find something existing in DB. $mapper = $this->locator->mapper($entityName); $records = $mapper->all()->limit(self::RELATED_FETCH_COUNT)->toArray(); if (empty($records)) { return null; } $id = $records[mt_rand(0, count($records) - 1)]['id']; return $id; } else { return null; } } }; } } return $formatters; }
/** * @param string $name * @param int|null $size Length of field, if known * * @return mixed */ public function guessFormat($name, $size = null) { if (null !== ($value = parent::guessFormat($name))) { return $value; } switch ($name) { case 'mobile': case 'mobile_phone': case 'mobilePhone': return $this->generator->mobileNumber; case 'fax': return $this->generator->phoneNumber; } if (preg_match('/_name$/', $name) || preg_match('/Name$/', $name)) { return $this->generator->name; } if (preg_match('/_time$/', $name) || preg_match('/Time$/', $name)) { return $this->generator->time; } return; }