Passed
Push — master ( d27343...ff86d8 )
by Sheldon
05:44
created

app/Base/Presenters/Presenter.php (1 issue)

Labels
Severity
1
<?php
2
3
namespace Yeelight\Base\Presenters;
4
5
use Illuminate\Pagination\AbstractPaginator;
6
use Illuminate\Support\Collection;
7
use Prettus\Repository\Presenter\FractalPresenter;
8
9
/**
10
 * Class Presenter
11
 *
12
 * @category Yeelight
13
 *
14
 * @package Yeelight\Base\Presenters
15
 *
16
 * @author Sheldon Lee <[email protected]>
17
 *
18
 * @license https://opensource.org/licenses/MIT MIT
19
 *
20
 * @link https://www.yeelight.com
21
 */
22
abstract class Presenter extends FractalPresenter
23
{
24
    protected $meta = [];
25
26
    /**
27
     * Prepare data to present.
28
     *
29
     * @param Collection $data data
30
     *
31
     * @throws \Exception
32
     *
33
     * @return mixed
34
     */
35
    public function present($data)
36
    {
37
        if (!class_exists('League\Fractal\Manager')) {
38
            throw new Exception(trans('repository::packages.league_fractal_required'));
0 ignored issues
show
The type Yeelight\Base\Presenters\Exception was not found. Did you mean Exception? If so, make sure to prefix the type with \.
Loading history...
39
        }
40
41
        if ($data instanceof Collection) {
42
            $this->resource = $this->transformCollection($data);
43
        } elseif ($data instanceof AbstractPaginator) {
44
            $this->resource = $this->transformPaginator($data);
45
        } else {
46
            $this->resource = $this->transformItem($data);
47
        }
48
49
        // set meta
50
        $this->resource->setMeta($this->meta);
51
52
        return $this->fractal->createData($this->resource)->toArray();
53
    }
54
55
    /**
56
     * Set Meta
57
     *
58
     * @param array $meta meta
59
     *
60
     * @return mixed
61
     */
62
    public function setMeta(array $meta)
63
    {
64
        $this->meta = $meta;
65
    }
66
}
67