RelationshipFactory::toArray()   A
last analyzed

Complexity

Conditions 4
Paths 8

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 14
ccs 8
cts 8
cp 1
rs 10
c 0
b 0
f 0
cc 4
nc 8
nop 0
crap 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace VGirol\JsonApiFaker\Factory;
6
7
use VGirol\JsonApiConstant\Members;
8
use VGirol\JsonApiFaker\Contract\RelationshipContract;
9
use VGirol\JsonApiFaker\Exception\JsonApiFakerException;
10
11
/**
12
 * A factory for "relationship" object
13
 */
14
class RelationshipFactory extends BaseFactory implements RelationshipContract
15
{
16 1
    use HasData;
17 1
    use HasLinks;
18 1
    use HasMeta;
19
20
    /**
21
     * @return array
22
     */
23 33
    public function toArray(): array
24
    {
25 33
        $resource = [];
26
27 33
        $resource[Members::DATA] = ($this->data === null) ? null : $this->data->toArray();
28
29 33
        if (isset($this->meta)) {
30 18
            $resource[Members::META] = $this->meta;
31
        }
32 33
        if (isset($this->links)) {
33 18
            $resource[Members::LINKS] = $this->links;
34
        }
35
36 33
        return $resource;
37
    }
38
39
    /**
40
     * Fill the relationship with fake values ("data", "meta" and "links").
41
     *
42
     * @param integer $options
43
     * @param integer $count   In case of collection, it represents the number of resource identifier to generate
44
     *
45
     * @return static
46
     * @throws JsonApiFakerException
47
     */
48 21
    public function fake($options = 0, $count = 5)
49
    {
50 21
        if ($options === 0) {
51 21
            $options = Options::FAKE_COLLECTION;
52
        }
53 21
        $options |= Options::FAKE_RESOURCE_IDENTIFIER;
54 21
        $options &= ~Options::FAKE_RESOURCE_OBJECT;
55
56 21
        return $this->fakeData($options, $count)
57 21
            ->fakeMeta()
58 21
            ->fakeLinks();
59
    }
60
}
61