Issues (138)

src/Traits/HasRendererTrait.php (1 issue)

Labels
Severity
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Nip\Form\Traits;
6
7
use Nip\Form\Renderer\AbstractRenderer;
8
9
/**
10
 * Trait HasRendererTrait
11
 * @package Nip\Form\Traits
12
 */
13
trait HasRendererTrait
14
{
15
    protected $_renderer;
16
17
    /**
18
     * @param $type
19
     * @return $this
20 4
     */
21
    public function setRendererType(string $type)
22 4
    {
23
        $this->setRenderer($this->getNewRenderer($type));
24 4
25
        return $this;
26
    }
27
28
    /**
29
     * @param string $class
30
     */
31
    protected function setRendererClass(string $class)
32
    {
33
        /** @var AbstractRenderer $renderer */
34
        $renderer = new $class();
35
        $renderer->setForm($this);
36
        $this->setRenderer($renderer);
37
    }
38
39
    /**
40
     * @param AbstractRenderer $renderer
41 4
     */
42
    public function setRenderer(AbstractRenderer $renderer)
43 4
    {
44 4
        $this->_renderer = $renderer;
45
    }
46
47
    /**
48
     * @param string $type
49
     * @return AbstractRenderer
50 10
     */
51
    public function getNewRenderer($type = 'basic')
52 10
    {
53
        $name = 'Nip_Form_Renderer_' . ucfirst($type);
54 10
        /** @var AbstractRenderer $renderer */
55 10
        $renderer = new $name();
56
        $renderer->setForm($this);
57 10
58
        return $renderer;
59
    }
60
61
    /**
62
     * @return null|string
63
     */
64
    public function __toString()
65
    {
66
        $backtrace = debug_backtrace();
67
        if ($backtrace[1]['class'] == 'Monolog\Formatter\NormalizerFormatter') {
68
            return null;
69
        }
70
        trigger_error('form __toString', E_USER_WARNING);
71
72
        return $this->render();
73
    }
74
75
    /**
76
     * @return string
77
     */
78
    public function render()
79
    {
80
        $this->initializeIfNotInitialized();
0 ignored issues
show
It seems like initializeIfNotInitialized() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

80
        $this->/** @scrutinizer ignore-call */ 
81
               initializeIfNotInitialized();
Loading history...
81
        return $this->getRenderer()->render();
82
    }
83
84
    /**
85 10
     * @return AbstractRenderer
86
     */
87 10
    public function getRenderer()
88 6
    {
89
        if (!$this->_renderer) {
90
            $this->initializeIfNotInitialized();
91 10
            $this->_renderer = $this->getNewRenderer();
92
        }
93
94
        return $this->_renderer;
95
    }
96
}
97