Completed
Push — master ( 2001b3...f7416c )
by Pavel
02:39
created

JsonApiSerializer   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 51
ccs 14
cts 14
cp 1
rs 10
c 0
b 0
f 0
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A collection() 0 9 2
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
     * @param string $resourceKey
25
     * @param array  $data
26
     * @param bool   $includeAttributes
27
     *
28
     * @return array
29
     */
30 12
    public function collection($resourceKey, array $data, $includeAttributes = true)
31
    {
32 12
        $resources = [];
33
34 12
        foreach ($data as $resource) {
35 12
            $resources[] = $this->item($resourceKey, $resource, $includeAttributes)['data'];
36
        }
37
38 12
        return ['data' => $resources];
39
    }
40
41
    /**
42
     * @param string $resourceKey
43
     * @param array  $data
44
     * @param bool   $includeAttributes
45
     *
46
     * @return array
47
     */
48 17
    public function item($resourceKey, array $data, $includeAttributes = true)
49
    {
50 17
        $item = parent::item($resourceKey, $data);
51 17
        if (!$includeAttributes) {
52 4
            unset($item['data']['attributes']);
53
        }
54
55 17
        return $item;
56
    }
57
}
58