Skip to content

Commit

Permalink
Package rewrite for Laravel 8
Browse files Browse the repository at this point in the history
  • Loading branch information
guidocella committed Sep 14, 2020
1 parent 31b4b32 commit b5cc435
Show file tree
Hide file tree
Showing 56 changed files with 274 additions and 4,006 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## v3.0 (2020-09-14)

- Populator has been rewritten to integrate with the improved Laravel 8 model factories instead of wrapping them.
- Removed the integration with Translatable.

## v2.1.5 (2018-11-27)

- Prevented the population of dynamic relationships - inspired by https://reinink.ca/articles/dynamic-relationships-in-laravel-using-subqueries
Expand Down
494 changes: 37 additions & 457 deletions README.md

Large diffs are not rendered by default.

20 changes: 9 additions & 11 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,30 +1,28 @@
{
"name": "guidocella/eloquent-populator",
"type": "library",
"description": "Automatically populate Eloquent models",
"description": "Guess attributes for Eloquent model factories",
"keywords": ["laravel", "eloquent", "database", "faker"],
"license": "MIT",
"require": {
"php": ">=5.6.4",
"illuminate/database": "^5.3||^6||^7",
"php": ">=7.4",
"illuminate/database": "^8",
"fzaninotto/faker": "^1.4",
"doctrine/dbal": "^2.5"
},
"require-dev": {
"laravel/laravel": "^7",
"phpunit/phpunit": "^8"
"guidocella/laravel-multilingual": "*",
"laravel/laravel": "^8",
"phpunit/phpunit": "^9"
},
"autoload": {
"psr-4": {
"EloquentPopulator\\": "src"
},
"files": [
"src/helper.php"
]
"GuidoCella\\EloquentPopulator\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"EloquentPopulator\\": "tests"
"GuidoCella\\EloquentPopulator\\": "tests"
}
}
}
8 changes: 0 additions & 8 deletions phpunit.xml.dist

This file was deleted.

74 changes: 17 additions & 57 deletions src/ColumnTypeGuesser.php
Original file line number Diff line number Diff line change
@@ -1,58 +1,31 @@
<?php

namespace EloquentPopulator;
namespace GuidoCella\EloquentPopulator;

use Carbon\Carbon;
use Closure;
use Doctrine\DBAL\Schema\Column;
use Faker\Generator;

class ColumnTypeGuesser
{
/**
* Faker's generator.
*
* @var Generator
*/
protected $generator;
protected Generator $generator;

/**
* ColumnTypeGuesser constructor.
*
* @param Generator $generator
*/
public function __construct(Generator $generator)
{
$this->generator = $generator;
}

/**
* Guess a column's formatter based on its type.
*
* @param Column $column
* @param string $tableName
* @return \Closure|null
*
* @throws \InvalidArgumentException
*/
public function guessFormat(Column $column, $tableName)
public function guessFormat(Column $column, string $tableName): ?Closure
{
switch ($column->getType()->getName()) {
case 'smallint':
return function () {
return mt_rand(0, 65535);
};
return fn () => rand(0, 65535);
case 'integer':
return function () {
return mt_rand(0, intval('2147483647'));
};
return fn () => rand(0, 2147483647);
case 'bigint':
return function () {
return mt_rand(0, intval('18446744073709551615'));
};
return fn () => rand(0, intval('18446744073709551615'));
case 'float':
return function () {
return mt_rand(0, intval('4294967295')) / mt_rand(1, intval('4294967295'));
};
return fn () => rand(0, 4294967295) / rand(1, 4294967295);
case 'decimal':
$maxDigits = $column->getPrecision();
$maxDecimalDigits = $column->getScale();
Expand Down Expand Up @@ -88,8 +61,8 @@ public function guessFormat(Column $column, $tableName)

throw new \InvalidArgumentException(
"$columnName is a string shorter than 5 characters,"
. " but Faker's text() can only generate text of at least 5 characters." . PHP_EOL
. "Please specify a more accurate formatter for $columnName."
." but Faker's text() can only generate text of at least 5 characters.".PHP_EOL
."Please specify a more accurate formatter for $columnName."
);

// Of course we could just use str_random($size) here,
Expand All @@ -98,35 +71,22 @@ public function guessFormat(Column $column, $tableName)
// e.g. $faker->countryCode for sender_country.
};
case 'text':
return function () {
return $this->generator->text;
};
return fn () => $this->generator->text;
case 'guid':
return function () {
return $this->generator->uuid;
};
return fn () => $this->generator->uuid;
case 'date':
case 'datetime':
case 'datetimetz':
return function () {
return $this->generator->datetime;
};
return fn () => $this->generator->datetime;
case 'time':
return function () {
return $this->generator->time;
};
return fn () => $this->generator->time;
case 'boolean':
return function () {
return $this->generator->boolean;
};
// Sadly Doctrine maps all TINYINT to BooleanType.
return fn () => $this->generator->boolean;
// Unfortunately Doctrine maps all TINYINT to BooleanType.
case 'json':
case 'json_array':
return function () {
return json_encode([$this->generator->word => $this->generator->word]);
};
return fn () => json_encode([$this->generator->word => $this->generator->word]);
default:
// No smart way to guess what the user expects here.
return null;
}
}
Expand Down
Loading

0 comments on commit b5cc435

Please sign in to comment.