1<?php
2
3
4namespace ComboStrap;
5
6
7class TextColor
8{
9
10    const TEXT_COLOR_ATTRIBUTE = "text-color";
11    const CSS_ATTRIBUTE = "color";
12    const CANONICAL = self::TEXT_COLOR_ATTRIBUTE;
13
14    const TEXT_COLORS = array(
15        'primary',
16        'secondary',
17        'success',
18        'danger',
19        'warning',
20        'info',
21        'light',
22        'dark',
23        'body',
24        'muted',
25        'white',
26        'black-50',
27        'white-50'
28    );
29
30    /**
31     * @param TagAttributes $attributes
32     */
33    public static function processTextColorAttribute(TagAttributes &$attributes)
34    {
35
36        $colorAttributes = [TextColor::CSS_ATTRIBUTE, TextColor::TEXT_COLOR_ATTRIBUTE];
37        foreach ($colorAttributes as $colorAttribute) {
38            if ($attributes->hasComponentAttribute($colorAttribute)) {
39                $colorValue = $attributes->getValueAndRemove($colorAttribute);
40                $lowerCaseColorValue = strtolower($colorValue);
41
42                /**
43                 * text is based in the text-colorname class
44                 * Not yet on variable or color object
45                 * We overwrite it here
46                 */
47                switch ($lowerCaseColorValue) {
48                    case ColorRgb::PRIMARY_VALUE:
49                        $primaryColor = Site::getPrimaryColor();
50                        if ($primaryColor !== null) {
51                            // important because we set the text-class below and they already have an important value
52                            $attributes->addStyleDeclarationIfNotSet(TextColor::CSS_ATTRIBUTE, "{$primaryColor->toRgbHex()}!important");
53                        }
54                        break;
55                    case ColorRgb::SECONDARY_VALUE:
56                        $secondaryColor = Site::getSecondaryColor();
57                        if ($secondaryColor !== null) {
58                            // important because we set the text-class below and they already have an important value
59                            $attributes->addStyleDeclarationIfNotSet(TextColor::CSS_ATTRIBUTE, "{$secondaryColor->toRgbHex()}!important");
60                        }
61                        break;
62                }
63
64                if (in_array($lowerCaseColorValue, self::TEXT_COLORS)) {
65                    /**
66                     * The bootstrap text class
67                     * https://getbootstrap.com/docs/5.0/utilities/colors/#colors
68                     */
69                    $attributes->addClassName("text-$lowerCaseColorValue");
70                } else {
71                    /**
72                     * Other Text Colors
73                     */
74                    try {
75                        $colorValue = ColorRgb::createFromString($colorValue)->toCssValue();
76                    } catch (ExceptionCompile $e) {
77                        LogUtility::msg("The text color value ($colorValue) is not a valid color. Error: {$e->getMessage()}");
78                        return;
79                    }
80                    if (!empty($colorValue)) {
81                        $attributes->addStyleDeclarationIfNotSet(TextColor::CSS_ATTRIBUTE, $colorValue);
82                    }
83                }
84                break;
85            }
86        }
87
88
89    }
90
91
92
93}
94