Completed
Push — master ( 4ee806...bc5900 )
by Vincent
20:15 queued 11s
created

DocumentFactory::toArray()   B

Complexity

Conditions 8
Paths 96

Size

Total Lines 24
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 8

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 24
ccs 3
cts 3
cp 1
rs 8.4444
c 0
b 0
f 0
cc 8
nc 96
nop 0
crap 8
1
<?php
2
3
declare(strict_types=1);
4
5
namespace VGirol\JsonApiFaker\Factory;
6
7
use VGirol\JsonApiFaker\Members;
8
9
/**
10
 * Factory for an entire document.
11
 */
12
class DocumentFactory extends BaseFactory
13
{
14
    use HasData;
0 ignored issues
show
Bug introduced by
The trait VGirol\JsonApiFaker\Factory\HasData requires the property $boolean which is not provided by VGirol\JsonApiFaker\Factory\DocumentFactory.
Loading history...
15
    use HasErrors;
16
    use HasLinks;
17
    use HasMeta;
18
    use HasJsonapi;
19
    use HasIncluded;
20
21
    /**
22
     * @inheritDoc
23
     * @return array<string,mixed>
24
     */
25
    public function toArray(): array
26
    {
27
        $json = [];
28
29
        if (isset($this->meta)) {
30
            $json[Members::META] = $this->meta;
31
        }
32
        if (isset($this->links)) {
33
            $json[Members::LINKS] = $this->links;
34
        }
35
        if (isset($this->errors)) {
36
            $json[Members::ERRORS] = $this->errors;
37
        }
38
        if ($this->dataHasBeenSet()) {
39
            $json[Members::DATA] = is_null($this->data) ? null : $this->data->toArray();
40 2
        }
41
        if (isset($this->included)) {
42 2
            $json[Members::INCLUDED] = $this->included->toArray();
43
        }
44 2
        if (isset($this->jsonapi)) {
45
            $json[Members::JSONAPI] = $this->jsonapi->toArray();
46
        }
47
48
        return $json;
49
    }
50
51
    /**
52
     * Undocumented function
53
     *
54 4
     * @param integer $options
55
     *
56 4
     * @return static
57
     */
58 4
    public function fake($options = null, $count = null)
59
    {
60
        if (is_null($options)) {
61
            $options = self::FAKE_SINGLE | self::FAKE_RESOURCE_OBJECT;
62
        }
63
64
        $withErrors = (($options & self::FAKE_ERRORS) == self::FAKE_ERRORS);
65 2
66
        $this->fakeLinks()
67 2
            ->fakeMeta()
68
            ->fakeJsonapi();
69 2
70 2
        return $withErrors ? $this->fakeErrors($count) : $this->fakeData($options, $count);
71
    }
72
}
73