ResourceObjectFactory   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
eloc 27
dl 0
loc 58
ccs 29
cts 29
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A fake() 0 7 1
A toArray() 0 31 6
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\ResourceObjectContract;
9
10
/**
11
 * A factory for resource object
12
 */
13
class ResourceObjectFactory extends BaseFactory implements ResourceObjectContract
14
{
15 1
    use HasAttributes;
16 1
    use HasIdentification;
17 1
    use HasLinks;
18 1
    use HasMeta;
19 1
    use HasRelationships;
20
21
    /**
22
     * Exports the factory as an array.
23
     *
24
     * @return array
25
     */
26 21
    public function toArray(): array
27
    {
28 21
        $resource = [];
29 21
        $identification = $this->getIdentification();
30 21
        if ($identification !== null) {
31 18
            $resource = $identification;
32
        }
33
34 21
        if (isset($this->attributes)) {
35 18
            $resource[Members::ATTRIBUTES] = $this->attributes;
36
        }
37 21
        if (isset($this->meta)) {
38 15
            $resource[Members::META] = $this->meta;
39
        }
40 21
        if (isset($this->links)) {
41 12
            $resource[Members::LINKS] = $this->links;
42
        }
43 21
        if (isset($this->relationships)) {
44 15
            $resource[Members::RELATIONSHIPS] = array_map(
45
                /**
46
                 * @param RelationshipFactory $relationship
47
                 * @return array
48
                 */
49 15
                function ($relationship) {
50 15
                    return $relationship->toArray();
51 15
                },
52 15
                $this->relationships
53
            );
54
        }
55
56 21
        return $resource;
57
    }
58
59
    /**
60
     * Fill the resource object with fake values ("type", "id", "attributes", "meta", "links" and "relationships").
61
     *
62
     * @return static
63
     */
64 15
    public function fake()
65
    {
66 15
        return $this->fakeIdentification()
67 15
            ->fakeAttributes()
68 15
            ->fakeMeta()
69 15
            ->fakeLinks()
70 15
            ->fakeRelationships();
71
    }
72
}
73