Completed
Branch develop (c4bcdd)
by Stephen
06:03
created

TransformableCollection   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 94
rs 10
wmc 8
lcom 1
cbo 4

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getNamespace() 0 4 1
A getTransformer() 0 7 2
A getTransformerClass() 0 10 2
A runTransformation() 0 6 1
A transformTo() 0 4 1
A transformToWithPagination() 0 10 1
1
<?php
2
3
namespace Spinen\Transformers;
4
5
use Illuminate\Database\Eloquent\Collection;
6
use Illuminate\Pagination\LengthAwarePaginator;
7
use Illuminate\Support\Facades\Input;
8
use Spinen\Transformers\Exceptions\TransformerNotFoundException;
9
10
/**
11
 * Class TransformableCollection
12
 *
13
 * @package App\Support\Transformation
14
 */
15
class TransformableCollection extends Collection
16
{
17
    /**
18
     * Fully Qualified Namespace to the Transformers
19
     *
20
     * @var string
21
     */
22
    protected $namespace = 'Spinen\Transformers';
23
24
    /**
25
     * Build out the namespace to the transformers
26
     *
27
     * @return string
28
     */
29
    public function getNamespace()
30
    {
31
        return trim($this->namespace, '\\');
32
    }
33
34
    /**
35
     * Make a transformer
36
     *
37
     * Allow the full path to the transformer class be specified or just the short name where it is concatenated to the
38
     * getNamespace method.
39
     *
40
     * @param string $class
41
     *
42
     * @return \Illuminate\Foundation\Application|mixed
43
     */
44
    public function getTransformer($class)
45
    {
46
        // If a full path to a class was passed in, use it, otherwise a
47
        $class = (class_exists($class)) ? $class : $this->getTransformerClass($class);
48
49
        return app($class);
50
    }
51
52
    public function getTransformerClass($class)
53
    {
54
        $class = $this->getNamespace() . '\\' . $class;
55
56
        if (class_exists($class)) {
57
            return $class;
58
        }
59
60
        throw new TransformerNotFoundException(sprintf('Could not locate Transformer [%s]', $class));
61
    }
62
63
    /**
64
     * Transform the data
65
     *
66
     * @param string $class
67
     *
68
     * @return array
69
     */
70
    protected function runTransformation($class)
71
    {
72
        return $this->getTransformer($class)
73
                    ->transformCollection($this)
74
                    ->toArray();
75
    }
76
77
    /**
78
     * Transform the collection
79
     *
80
     * You pass in a class name that is assume that the classes are located in app/Transformers/
81
     *
82
     * @param string $class
83
     *
84
     * @return array
85
     */
86
    public function transformTo($class)
87
    {
88
        return ['data' => $this->runTransformation($class)];
89
    }
90
91
    /**
92
     * Transform the collection to a paginated collection
93
     *
94
     * @param string $class
95
     *
96
     * @return LengthAwarePaginator
97
     */
98
    public function transformToWithPagination($class)
99
    {
100
        $page = Input::get('page', 1);
101
        $perPage = Input::get('limit', 5);
102
        $offset = ($page * $perPage) - $perPage;
103
        // TODO: Is there a way to slice before transforming, so that we don't waste time transforming unwanted data?
104
        $arraySlice = array_slice($this->runTransformation($class), $offset, $perPage, true);
105
106
        return new LengthAwarePaginator($arraySlice, $this->count(), $perPage);
107
    }
108
}
109