This repository has been archived by the owner on Mar 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TextualElement.php
80 lines (70 loc) · 1.61 KB
/
TextualElement.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<?php
/**
* Copyright (C) GrizzIT, Inc. All rights reserved.
* See LICENSE for license details.
*/
namespace Ulrack\Cli\Component\Element;
use Ulrack\Cli\Common\Io\WriterInterface;
use Ulrack\Cli\Component\Theme\VoidStyle;
use Ulrack\Cli\Common\Theme\StyleInterface;
use Ulrack\Cli\Common\Element\ElementInterface;
class TextualElement implements ElementInterface
{
/**
* Contains the writer to display the text.
*
* @var WriterInterface
*/
private $writer;
/**
* Contains the style of the text field.
*
* @var StyleInterface
*/
private $style;
/**
* Contains the content of the element.
*
* @var string
*/
private $content;
/**
* Whether a new line should be used for the element.
*
* @var bool
*/
private $newLine;
/**
* Constructor.
*
* @param string $content
* @param WriterInterface $writer
* @param bool $newLine
* @param StyleInterface $style
*/
public function __construct(
string $content,
WriterInterface $writer,
bool $newLine = true,
StyleInterface $style = null
) {
$this->content = $content;
$this->writer = $writer;
$this->newLine = $newLine;
$this->style = $style ?? new VoidStyle();
}
/**
* Renders the element.
*
* @return void
*/
public function render(): void
{
$this->style->apply();
$this->writer->write($this->content);
$this->style->reset();
if ($this->newLine) {
$this->writer->writeLine('');
}
}
}