Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
declare(strict_types=1);
4
 
5
namespace Termwind\ValueObjects;
6
 
7
use Closure;
8
use Termwind\Actions\StyleToMethod;
9
use Termwind\Exceptions\InvalidColor;
10
 
11
/**
12
 * @internal
13
 */
14
final class Style
15
{
16
    /**
17
     * Creates a new value object instance.
18
     *
19
     * @param Closure(Styles $styles, string|int ...$argument): Styles  $callback
20
     */
21
    public function __construct(private Closure $callback, private string $color = '')
22
    {
23
        // ..
24
    }
25
 
26
    /**
27
     * Apply the given set of styles to the styles.
28
     */
29
    public function apply(string $styles): void
30
    {
31
        $callback = clone $this->callback;
32
 
33
        $this->callback = static function (
34
            Styles $formatter,
35
            string|int ...$arguments
36
        ) use ($callback, $styles): Styles {
37
            $formatter = $callback($formatter, ...$arguments);
38
 
39
            return StyleToMethod::multiple($formatter, $styles);
40
        };
41
    }
42
 
43
    /**
44
     * Sets the color to the style.
45
     */
46
    public function color(string $color): void
47
    {
48
        if (preg_match('/^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/', $color) < 1) {
49
            throw new InvalidColor(sprintf('The color %s is invalid.', $color));
50
        }
51
 
52
        $this->color = $color;
53
    }
54
 
55
    /**
56
     * Gets the color.
57
     */
58
    public function getColor(): string
59
    {
60
        return $this->color;
61
    }
62
 
63
    /**
64
     * Styles the given formatter with this style.
65
     */
66
    public function __invoke(Styles $styles, string|int ...$arguments): Styles
67
    {
68
        return ($this->callback)($styles, ...$arguments);
69
    }
70
}