| 148 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
declare(strict_types=1);
|
|
|
4 |
|
|
|
5 |
namespace Termwind\Components;
|
|
|
6 |
|
|
|
7 |
use Symfony\Component\Console\Output\OutputInterface;
|
|
|
8 |
use Termwind\Actions\StyleToMethod;
|
|
|
9 |
use Termwind\Html\InheritStyles;
|
|
|
10 |
use Termwind\ValueObjects\Styles;
|
|
|
11 |
|
|
|
12 |
/**
|
|
|
13 |
* @internal
|
|
|
14 |
*
|
|
|
15 |
* @method Element inheritFromStyles(Styles $styles)
|
|
|
16 |
* @method Element fontBold()
|
|
|
17 |
* @method Element strong()
|
|
|
18 |
* @method Element italic()
|
|
|
19 |
* @method Element underline()
|
|
|
20 |
* @method Element lineThrough()
|
|
|
21 |
* @method int getLength()
|
|
|
22 |
* @method int getInnerWidth()
|
|
|
23 |
* @method array getProperties()
|
|
|
24 |
* @method Element href(string $href)
|
|
|
25 |
* @method bool hasStyle(string $style)
|
|
|
26 |
* @method Element addStyle(string $style)
|
|
|
27 |
*/
|
|
|
28 |
abstract class Element
|
|
|
29 |
{
|
|
|
30 |
/** @var string[] */
|
|
|
31 |
protected static array $defaultStyles = [];
|
|
|
32 |
|
|
|
33 |
protected Styles $styles;
|
|
|
34 |
|
|
|
35 |
/**
|
|
|
36 |
* Creates an element instance.
|
|
|
37 |
*
|
|
|
38 |
* @param array<int, Element|string>|string $content
|
|
|
39 |
*/
|
|
|
40 |
final public function __construct(
|
|
|
41 |
protected OutputInterface $output,
|
|
|
42 |
protected array|string $content,
|
|
|
43 |
Styles|null $styles = null
|
|
|
44 |
) {
|
|
|
45 |
$this->styles = $styles ?? new Styles(defaultStyles: static::$defaultStyles);
|
|
|
46 |
$this->styles->setElement($this);
|
|
|
47 |
}
|
|
|
48 |
|
|
|
49 |
/**
|
|
|
50 |
* Creates an element instance with the given styles.
|
|
|
51 |
*
|
|
|
52 |
* @param array<int, Element|string>|string $content
|
|
|
53 |
* @param array<string, mixed> $properties
|
|
|
54 |
*/
|
|
|
55 |
final public static function fromStyles(OutputInterface $output, array|string $content, string $styles = '', array $properties = []): static
|
|
|
56 |
{
|
|
|
57 |
$element = new static($output, $content);
|
|
|
58 |
if ($properties !== []) {
|
|
|
59 |
$element->styles->setProperties($properties);
|
|
|
60 |
}
|
|
|
61 |
|
|
|
62 |
$elementStyles = StyleToMethod::multiple($element->styles, $styles);
|
|
|
63 |
|
|
|
64 |
return new static($output, $content, $elementStyles);
|
|
|
65 |
}
|
|
|
66 |
|
|
|
67 |
/**
|
|
|
68 |
* Get the string representation of the element.
|
|
|
69 |
*/
|
|
|
70 |
public function toString(): string
|
|
|
71 |
{
|
|
|
72 |
if (is_array($this->content)) {
|
|
|
73 |
$inheritance = new InheritStyles();
|
|
|
74 |
$this->content = implode('', $inheritance($this->content, $this->styles));
|
|
|
75 |
}
|
|
|
76 |
|
|
|
77 |
return $this->styles->format($this->content);
|
|
|
78 |
}
|
|
|
79 |
|
|
|
80 |
/**
|
|
|
81 |
* @param array<int, mixed> $arguments
|
|
|
82 |
*/
|
|
|
83 |
public function __call(string $name, array $arguments): mixed
|
|
|
84 |
{
|
|
|
85 |
if (method_exists($this->styles, $name)) {
|
|
|
86 |
$result = $this->styles->{$name}(...$arguments);
|
|
|
87 |
|
|
|
88 |
if (str_starts_with($name, 'get') || str_starts_with($name, 'has')) {
|
|
|
89 |
return $result;
|
|
|
90 |
}
|
|
|
91 |
}
|
|
|
92 |
|
|
|
93 |
return $this;
|
|
|
94 |
}
|
|
|
95 |
|
|
|
96 |
/**
|
|
|
97 |
* Sets the content of the element.
|
|
|
98 |
*
|
|
|
99 |
* @param array<int, Element|string>|string $content
|
|
|
100 |
*/
|
|
|
101 |
final public function setContent(array|string $content): static
|
|
|
102 |
{
|
|
|
103 |
return new static($this->output, $content, $this->styles);
|
|
|
104 |
}
|
|
|
105 |
|
|
|
106 |
/**
|
|
|
107 |
* Renders the string representation of the element on the output.
|
|
|
108 |
*/
|
|
|
109 |
final public function render(int $options): void
|
|
|
110 |
{
|
|
|
111 |
$this->output->writeln($this->toString(), $options);
|
|
|
112 |
}
|
|
|
113 |
|
|
|
114 |
/**
|
|
|
115 |
* Get the string representation of the element.
|
|
|
116 |
*/
|
|
|
117 |
final public function __toString(): string
|
|
|
118 |
{
|
|
|
119 |
return $this->toString();
|
|
|
120 |
}
|
|
|
121 |
}
|