Test Setup Failed
Push — v2 ( 74186a...c94b75 )
by Alexander
06:56
created

FractalTransformFactory::make()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 3
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
1
<?php
2
3
namespace Flugg\Responder;
4
5
use Flugg\Responder\Contracts\TransformFactory;
6
use League\Fractal\Manager;
7
use League\Fractal\Resource\ResourceInterface;
8
use League\Fractal\Serializer\SerializerAbstract;
9
10
/**
11
 * A factory class responsible for transforming and serializing data utilizing Fractal.
12
 *
13
 * @package flugger/laravel-responder
14
 * @author  Alexander Tømmerås <[email protected]>
15
 * @license The MIT License
16
 */
17
class FractalTransformFactory implements TransformFactory
18
{
19
    /**
20
     * A manager for executing transforms.
21
     *
22
     * @var \League\Fractal\Manager
23
     */
24
    protected $manager;
25
26
    /**
27
     * Construct the factory class.
28
     *
29
     * @param \League\Fractal\Manager $manager
30
     */
31
    public function __construct(Manager $manager)
32
    {
33
        $this->manager = $manager;
34
    }
35
36
    /**
37
     * Transform the given resource, and serialize the data with the given serializer.
38
     *
39
     * @param  \League\Fractal\Resource\ResourceInterface    $resource
40
     * @param  \League\Fractal\Serializer\SerializerAbstract $serializer
41
     * @param  array                                         $options
42
     * @return array
43
     */
44
    public function make(ResourceInterface $resource, SerializerAbstract $serializer, array $options = []): array
45
    {
46
        return $this->manager->setSerializer($serializer)
47
            ->parseIncludes($options['includes'] ?? [])
48
            ->parseExcludes($options['excludes'] ?? [])
49
            ->parseFieldsets($options['fields'] ?? [])
50
            ->createData($resource)
51
            ->toArray();
52
    }
53
}