Completed
Push — master ( 883f28...097345 )
by Vincent
03:45 queued 11s
created

BaseFactory::fakeMemberName()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 5

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 14
ccs 8
cts 8
cp 1
rs 9.6111
c 0
b 0
f 0
cc 5
nc 4
nop 2
crap 5
1
<?php
2
3
declare(strict_types=1);
4
5
namespace VGirol\JsonApiFaker\Factory;
6
7
use Faker\Generator;
8
use VGirol\JsonApiFaker\Messages;
9
10
abstract class BaseFactory
11
{
12
    const FAKE_RESOURCE_OBJECT = 1;
13
    const FAKE_RESOURCE_IDENTIFIER = 2;
14
    const FAKE_SINGLE = 4;
15
    const FAKE_COLLECTION = 8;
16
    const FAKE_CAN_BE_NULL = 16;
17
18
    /**
19
     * Undocumented function
20
     *
21
     * @return array|null
22
     */
23
    abstract public function toArray(): ?array;
24
25
    /**
26
     * Undocumented function
27
     *
28
     * @return static
29
     */
30
    abstract public function fake();
31
32
    /**
33
     * Undocumented function
34
     *
35
     * @param string $object
36
     * @param string $name
37
     * @param mixed $value
38
     *
39
     * @return void
40
     */
41 18
    public function addToObject(string $object, string $name, $value): void
42
    {
43 18
        if (!isset($this->{$object})) {
44 18
            $this->{$object} = [];
45
        }
46
47 18
        $this->{$object}[$name] = $value;
48 18
    }
49
50
    /**
51
     * Undocumented function
52
     *
53
     * @param string $object
54
     * @param mixed $value
55
     *
56
     * @return void
57
     */
58 2
    public function addToArray(string $object, $value): void
59
    {
60 2
        if (!isset($this->{$object})) {
61 1
            $this->{$object} = [];
62
        }
63
64 2
        $this->{$object}[] = $value;
65 2
    }
66
67
    /**
68
     * Undocumented function
69
     *
70
     * @param integer $options
71
     *
72
     * @return string
73
     */
74 2
    public function toJson($options = 0): string
75
    {
76 2
        $json = json_encode($this->toArray(), $options);
77
78 2
        if ($json === false) {
79 1
            throw new \Exception(
80 1
                sprintf(Messages::JSON_ENCODE_ERROR, json_last_error_msg())
81
            );
82
        }
83
84 1
        return $json;
85
    }
86
87
    /**
88
     * Undocumented function
89
     *
90
     * The fake member is filled with the null value :
91
     * $options = [
92
     *      'member name' => null
93
     * ]
94
     *
95
     * The default array of methods is used to fill the fake value :
96
     * $options = [
97
     *      'member name' => []
98
     * ]
99
     *
100
     * Only the provided methods are used to fill the fake value :
101
     * $options = [
102
     *      'member name' => [ array of methods allowed to fake ]
103
     * ]
104
     *
105
     * If no key name is provided, a fake member name is generated :
106
     * $options = [
107
     *      null,
108
     *      [],
109
     *      [ array of methods allowed to fake ]
110
     * ]
111
     *
112
     * @param integer|array $options The number of keys to generate or an array of keys to use.
113
     *
114
     * @return array
115
     */
116 13
    protected function fakeMembers($options): array
117
    {
118 13
        $faker = \Faker\Factory::create();
119
120 13
        if (is_int($options)) {
121 12
            $count = $options;
122 12
            $options = [];
123 12
            for ($i = 0; $i < $count; $i++) {
124 12
                $options[] = [];
125
            }
126
        }
127
128 13
        $values = [];
129 13
        foreach ($options as $key => $providers) {
130 13
            $name = $this->fakeMemberName($faker, $key);
131 13
            $value = $this->fakeValue($faker, $providers);
132
133 13
            $values[$name] = $value;
134
        }
135
136 13
        return $values;
137
    }
138
139
    /**
140
     * Undocumented function
141
     *
142
     * @param Generator $faker
143
     * @param string|integer|null $name
144
     *
145
     * @return string
146
     */
147 14
    protected function fakeMemberName(Generator $faker, $name = null): string
148
    {
149 14
        if (\is_null($name) || \is_int($name)) {
150 13
            $forbidden = ['id', 'type'];
151
            do {
152 13
                $name = $faker->unique()->word;
153 13
            } while (in_array($name, $forbidden));
154
        }
155
156 14
        if (\strpos($name, '/') === false) {
157 14
            return $name;
158
        }
159
160 1
        return $faker->unique()->regexify($name);
161
    }
162
163
    /**
164
     * Undocumented function
165
     *
166
     * @param Generator $faker
167
     * @param array|null $providers
168
     * @return mixed
169
     */
170 14
    protected function fakeValue(Generator $faker, ?array $providers = [])
171
    {
172
        $allowed = [
173 14
            'word',
174
            'sentence',
175
            'boolean',
176
            'randomNumber',
177
            'randomFloat',
178
            'date'
179
        ];
180
181 14
        if (is_null($providers)) {
0 ignored issues
show
introduced by
The condition is_null($providers) is always false.
Loading history...
182 1
            return null;
183
        }
184
185 14
        $method = $faker->randomElement(
186 14
            empty($providers) ? $allowed : $providers
187
        );
188
189 14
        return $faker->{$method};
190
    }
191
}
192