Mistralys /
application-utils
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * @package Application Utils |
||
| 4 | * @subpackage Traits |
||
| 5 | * @see \AppUtils\Traits\RenderableTrait |
||
| 6 | */ |
||
| 7 | |||
| 8 | declare(strict_types=1); |
||
| 9 | |||
| 10 | namespace AppUtils\Traits; |
||
| 11 | |||
| 12 | use AppUtils\Interfaces\RenderableInterface; |
||
| 13 | use Throwable; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * Trait used to quickly implement the interface |
||
| 17 | * {@see RenderableInterface}: Only the `render()` |
||
| 18 | * method needs to be implemented. |
||
| 19 | * |
||
| 20 | * @package Application Utils |
||
| 21 | * @subpackage Traits |
||
| 22 | * @author Sebastian Mordziol <[email protected]> |
||
| 23 | * |
||
| 24 | * @see RenderableInterface |
||
| 25 | */ |
||
| 26 | trait RenderableTrait |
||
| 27 | { |
||
| 28 | public function display() : void |
||
| 29 | { |
||
| 30 | echo $this->render(); |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 31 | } |
||
| 32 | |||
| 33 | public function __toString() : string |
||
| 34 | { |
||
| 35 | try |
||
| 36 | { |
||
| 37 | return $this->render(); |
||
| 38 | } |
||
| 39 | catch (Throwable $e) |
||
| 40 | { |
||
| 41 | return sprintf( |
||
| 42 | 'Exception while rendering [%s]: %s', |
||
| 43 | get_class($this), |
||
| 44 | $e->getMessage() |
||
| 45 | ); |
||
| 46 | } |
||
| 47 | } |
||
| 48 | } |
||
| 49 |