Completed
Branch master (bc5900)
by Vincent
01:31
created

DocumentFactory::toArray()   B

Complexity

Conditions 8
Paths 96

Size

Total Lines 24
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 8

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 24
ccs 15
cts 15
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 2
    public function toArray(): array
26
    {
27 2
        $json = [];
28
29 2
        if (isset($this->meta)) {
30 2
            $json[Members::META] = $this->meta;
31
        }
32 2
        if (isset($this->links)) {
33 2
            $json[Members::LINKS] = $this->links;
34
        }
35 2
        if (isset($this->errors)) {
36 1
            $json[Members::ERRORS] = $this->errors;
37
        }
38 2
        if ($this->dataHasBeenSet()) {
39 1
            $json[Members::DATA] = is_null($this->data) ? null : $this->data->toArray();
40
        }
41 2
        if (isset($this->included)) {
42 1
            $json[Members::INCLUDED] = $this->included->toArray();
43
        }
44 2
        if (isset($this->jsonapi)) {
45 2
            $json[Members::JSONAPI] = $this->jsonapi->toArray();
46
        }
47
48 2
        return $json;
49
    }
50
51
    /**
52
     * Undocumented function
53
     *
54
     * @param integer $options
55
     *
56
     * @return static
57
     */
58 1
    public function fake($options = null, $count = null)
59
    {
60 1
        if (is_null($options)) {
61 1
            $options = self::FAKE_SINGLE | self::FAKE_RESOURCE_OBJECT;
62
        }
63
64 1
        $withErrors = (($options & self::FAKE_ERRORS) == self::FAKE_ERRORS);
65
66 1
        $this->fakeLinks()
67 1
            ->fakeMeta()
68 1
            ->fakeJsonapi();
69
70 1
        return $withErrors ? $this->fakeErrors($count) : $this->fakeData($options, $count);
71
    }
72
}
73