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

JsonApiSerializer::item()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 3
dl 0
loc 8
ccs 5
cts 5
cp 1
crap 2
rs 9.4285
c 0
b 0
f 0
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