Completed
Push — master ( adc131...6e9eb4 )
by Kevin
03:32
created

TextGenerator::__invoke()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 3
nc 4
nop 0
1
<?php
2
namespace DBFaker\Generators;
3
4
5
use Doctrine\DBAL\Schema\Column;
6
use Faker\Factory;
7
use Faker\Generator;
8
9
class TextGenerator implements FakeDataGeneratorInterface
10
{
11
12
    /**
13
     * @var Generator
14
     */
15
    private $faker;
16
17
    /**
18
     * @var Column
19
     */
20
    private $column;
21
22
    /**
23
     * TextGenerator constructor.
24
     * @param Column $column
25
     * @param bool $generateUniqueValues
26
     */
27
    public function __construct(Column $column, bool $generateUniqueValues = false)
28
    {
29
        $this->faker = Factory::create();
30
        if ($generateUniqueValues){
31
            $this->faker->unique();
32
        }
33
        $this->column = $column;
34
    }
35
36
    /**
37
     * @return string
38
     */
39
    public function __invoke() : string
40
    {
41
        $colLength = $this->column->getLength();
42
        $maxLength = $colLength > 5 ? max($colLength, 300) : $colLength;
43
        return $colLength > 5 ? $this->faker->text($maxLength) : substr($this->faker->text(5), 0, $colLength - 1);
44
    }
45
46
47
}