ComplexObjectGenerator::generateRandomObject()   B
last analyzed

Complexity

Conditions 7
Paths 21

Size

Total Lines 25
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 18
dl 0
loc 25
rs 8.8333
c 0
b 0
f 0
cc 7
nc 21
nop 1
1
<?php
2
namespace DBFaker\Generators;
3
4
use DBFaker\Exceptions\DBFakerException;
5
use Doctrine\DBAL\Schema\Column;
6
use Faker\Factory;
7
use Faker\Generator;
8
9
class ComplexObjectGenerator extends UniqueAbleGenerator
10
{
11
12
    /**
13
     * @var Generator
14
     */
15
    private $generator;
16
17
    /**
18
     * @var int
19
     */
20
    private $depth;
21
22
    /**
23
     * @var boolean
24
     */
25
    private $toArray;
26
27
    /**
28
     * ComplexObjectGenerator constructor.
29
     * @param Column $column
30
     * @param int|null $depth
31
     * @param bool $toArray
32
     * @param bool $generateUniqueValues
33
     */
34
    public function __construct(Column $column, int $depth = null, bool $toArray = true, $generateUniqueValues = false)
35
    {
36
        parent::__construct($column, $generateUniqueValues);
37
        $this->generator = Factory::create();
38
        $this->depth = $depth ?? random_int(2, 5);
39
        $this->toArray = $toArray;
40
    }
41
42
    /**
43
     * @param Column $column
44
     * @return mixed
45
     */
46
    protected function generateRandomValue(Column $column)
47
    {
48
        return $this->generateRandomObject($this->depth);
49
    }
50
51
    /**
52
     * @param int $depth
53
     * @return \stdClass|array
54
     * @throws \DBFaker\Exceptions\DBFakerException
55
     */
56
    private function generateRandomObject(int $depth)
57
    {
58
        $obj = new \stdClass();
59
        $nbProps = \random_int(2, 5);
60
        $hasGoneDeeper = false;
61
        for ($i = 0; $i < $nbProps; $i++) {
62
            $propName = $this->randomPropName();
63
            $goDeeper = $depth !== 0 && (\random_int(0, 10) > 7 || !$hasGoneDeeper);
64
            if ($goDeeper) {
65
                $hasGoneDeeper = true;
66
                $value = $this->generateRandomObject($depth - 1);
67
            } else {
68
                $value = $this->randomValue();
69
            }
70
            $obj->$propName = $value;
71
        }
72
73
        if ($this->toArray) {
74
            $obj = json_encode($obj, JSON_OBJECT_AS_ARRAY);
75
            if (!$obj) {
76
                throw new DBFakerException('Could not convert generated object to Json String');
77
            }
78
            $obj = json_decode($obj, true);
79
        }
80
        return $obj;
81
    }
82
83
    /**
84
     * @return mixed
85
     */
86
    private function randomValue()
87
    {
88
        $generators = [
89
            $this->generator->biasedNumberBetween(),
90
            $this->generator->boolean,
91
            $this->generator->century,
92
            $this->generator->city,
93
            $this->generator->creditCardExpirationDate,
94
            $this->generator->dateTime,
95
            $this->generator->longitude
96
        ];
97
        return $generators[array_rand($generators)];
98
    }
99
100
    /**
101
     * @return string
102
     */
103
    private function randomPropName() : string
104
    {
105
        return str_replace('.', '', $this->generator->userName);
106
    }
107
}
108