Test Failed
Push — master ( d5f3a1...1f9a79 )
by Sebastian
03:03
created

RenderableBufferedTrait::render()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 7
rs 10
cc 1
nc 1
nop 0
1
<?php
2
/**
3
 * @package Application Utils
4
 * @subpackage Traits
5
 * @see \AppUtils\Traits\RenderableBufferedTrait
6
 */
7
8
declare(strict_types=1);
9
10
namespace AppUtils\Traits;
11
12
use AppUtils\Interfaces\RenderableInterface;
13
use AppUtils\OutputBuffering;
14
use Throwable;
15
16
/**
17
 * Like the renderable trait, but uses output buffering
18
 * to get the rendered content. The method {@see RenderableBufferedTrait::generateOutput()}
19
 * is used to generate the output to use.
20
 *
21
 * @package Application Utils
22
 * @subpackage Traits
23
 * @author Sebastian Mordziol <[email protected]>
24
 *
25
 * @see RenderableInterface
26
 */
27
trait RenderableBufferedTrait
28
{
29
    public function render() : string
30
    {
31
        OutputBuffering::start();
32
33
        $this->generateOutput();
34
35
        return OutputBuffering::get();
36
    }
37
38
    abstract protected function generateOutput() : void;
39
40
    public function display() : void
41
    {
42
        echo $this->render();
43
    }
44
45
    public function __toString() : string
46
    {
47
        try
48
        {
49
            return $this->render();
50
        }
51
        catch (Throwable $e)
52
        {
53
            return sprintf(
54
                'Exception while rendering [%s]: %s',
55
                get_class($this),
56
                $e->getMessage()
57
            );
58
        }
59
    }
60
}
61