Completed
Push — master ( af7b98...388b72 )
by Vitaliy
02:45
created

src/Components/Base/TRenderable.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
namespace Nayjest\Grids\Components\Base;
3
4
use View;
5
6
/**
7
 * Trait TRenderable
8
 *
9
 * Default implementation of rendering facilities for grid component, etc.
10
 *
11
 * @todo Avoid usage of Laravel Facade aliases (?)
12
 * @todo Absence of getViewData isn't convenient (?)
13
 *
14
 * @package Nayjest\Grids\Components\Base
15
 */
16
trait TRenderable
17
{
18
    /**
19
     * Name of view template.
20
     *
21
     * @var  string
22
     */
23
    protected $template;
24
25
    /** @var bool */
26
    protected $is_rendered = false;
27
28
    /**
29
     * Renders object.
30
     *
31
     * @return string
32
     */
33
    public function render()
34
    {
35
        $this->is_rendered = true;
36
        return View::make(
37
            $this->getTemplate(),
38
            $this->getViewData()
0 ignored issues
show
It seems like getViewData() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
39
        )->render();
40
    }
41
42
    /**
43
     * Returns name of view template.
44
     *
45
     * @return string
46
     */
47
    public function getTemplate()
48
    {
49
        return $this->template;
50
    }
51
52
    /**
53
     * Allows to specify view template.
54
     *
55
     * @param string $template
56
     * @return $this
57
     */
58
    public function setTemplate($template)
59
    {
60
        $this->template = $template;
61
        return $this;
62
    }
63
64
    /**
65
     * Returns true if object already was rendered.
66
     *
67
     * @return bool
68
     */
69
    public function isRendered()
70
    {
71
        return $this->is_rendered;
72
    }
73
74
    /**
75
     * Renders object when it is treated like a string.
76
     *
77
     * @return string
78
     */
79
    public function __toString()
80
    {
81
        return (string)$this->render();
82
    }
83
}
84