Completed
Push — master ( a5189e...da7714 )
by Pavel
02:31
created

JsonApiSerializer   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Test Coverage

Coverage 87.5%

Importance

Changes 0
Metric Value
dl 0
loc 59
ccs 14
cts 16
cp 0.875
rs 10
c 0
b 0
f 0
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A request() 0 3 1
A collection() 0 9 2
A __construct() 0 4 1
A item() 0 8 2
1
<?php namespace Pz\Doctrine\Rest\Fractal;
2
3
use Pz\Doctrine\Rest\Contracts\RestRequestContract;
4
5
class JsonApiSerializer extends \League\Fractal\Serializer\JsonApiSerializer
6
{
7
    /**
8
     * @var RestRequestContract
9
     */
10
    protected $request;
11
12
    /**
13
     * JsonApiSerializer constructor.
14
     *
15
     * @param RestRequestContract $request
16
     */
17 18
    public function __construct(RestRequestContract $request)
18
    {
19 18
        parent::__construct($request->getBaseUrl());
20 18
        $this->request = $request;
21 18
    }
22
23
    /**
24
     * @return RestRequestContract
25
     */
26
    public function request()
27
    {
28
        return $this->request;
29
    }
30
31
    /**
32
     * @param string $resourceKey
33
     * @param array  $data
34
     * @param bool   $includeAttributes
35
     *
36
     * @return array
37
     */
38 12
    public function collection($resourceKey, array $data, $includeAttributes = true)
39
    {
40 12
        $resources = [];
41
42 12
        foreach ($data as $resource) {
43 12
            $resources[] = $this->item($resourceKey, $resource, $includeAttributes)['data'];
44
        }
45
46 12
        return ['data' => $resources];
47
    }
48
49
    /**
50
     * @param string $resourceKey
51
     * @param array  $data
52
     * @param bool   $includeAttributes
53
     *
54
     * @return array
55
     */
56 17
    public function item($resourceKey, array $data, $includeAttributes = true)
57
    {
58 17
        $item = parent::item($resourceKey, $data);
59 17
        if (!$includeAttributes) {
60 3
            unset($item['data']['attributes']);
61
        }
62
63 17
        return $item;
64
    }
65
}
66