1<?php
2
3
4namespace ComboStrap;
5
6use splitbrain\phpcli\Colors;
7
8/**
9 * Class Skin
10 * @package ComboStrap
11 * Processing the skin attribute
12 */
13class Skin
14{
15
16    const CANONICAL = self::SKIN_ATTRIBUTE;
17    const SKIN_ATTRIBUTE = "skin";
18    const FILLED_VALUE = "filled";
19
20
21    static $colorsWithoutPrimaryAndSecondary = array(
22        "info" => array(
23            ColorRgb::COLOR => "#0c5460",
24            Background::BACKGROUND_COLOR => "#d1ecf1",
25            ColorRgb::BORDER_COLOR => "#bee5eb"
26        ),
27        "tip" => array(
28            ColorRgb::COLOR => "#6c6400",
29            Background::BACKGROUND_COLOR => "#fff79f",
30            ColorRgb::BORDER_COLOR => "#FFF78c"
31        ),
32        "warning" => array(
33            ColorRgb::COLOR => "#856404",
34            Background::BACKGROUND_COLOR => "#fff3cd",
35            ColorRgb::BORDER_COLOR => "#ffeeba"
36        ),
37        "success" => array(
38            ColorRgb::COLOR => "#fff",
39            Background::BACKGROUND_COLOR => "#28a745",
40            ColorRgb::BORDER_COLOR => "#28a745"
41        ),
42        "danger" => array(
43            ColorRgb::COLOR => "#fff",
44            Background::BACKGROUND_COLOR => "#dc3545",
45            ColorRgb::BORDER_COLOR => "#dc3545"
46        ),
47        "dark" => array(
48            ColorRgb::COLOR => "#fff",
49            Background::BACKGROUND_COLOR => "#343a40",
50            ColorRgb::BORDER_COLOR => "#343a40"
51        ),
52        "light" => array(
53            ColorRgb::COLOR => "#fff",
54            Background::BACKGROUND_COLOR => "#f8f9fa",
55            ColorRgb::BORDER_COLOR => "#f8f9fa"
56        )
57    );
58
59    public static function getSkinColors(): array
60    {
61        $primaryColorRgbHex = Site::getPrimaryColor("#007bff")->toRgbHex();
62        $secondaryColorRgbHex = Site::getSecondaryColor("#6c757d")->toRgbHex();
63        $brandingColors = array(ColorRgb::PRIMARY_VALUE => array(
64            ColorRgb::COLOR => "#fff",
65            Background::BACKGROUND_COLOR => $primaryColorRgbHex,
66            ColorRgb::BORDER_COLOR => $primaryColorRgbHex
67        ),
68            ColorRgb::SECONDARY_VALUE => array(
69                ColorRgb::COLOR => "#fff",
70                Background::BACKGROUND_COLOR => $secondaryColorRgbHex,
71                ColorRgb::BORDER_COLOR => $secondaryColorRgbHex
72            ));
73        return array_merge($brandingColors, self::$colorsWithoutPrimaryAndSecondary);
74    }
75
76    /**
77     * Used with button
78     * @param TagAttributes $attributes
79     */
80    public static function processSkinAttribute(TagAttributes &$attributes)
81    {
82        // Skin
83        if (!$attributes->hasComponentAttribute(self::SKIN_ATTRIBUTE)) {
84            return;
85        }
86        $skinValue = $attributes->getValue(self::SKIN_ATTRIBUTE);
87        if (!$attributes->hasComponentAttribute(TagAttributes::TYPE_KEY)) {
88
89            LogUtility::msg("A component type is mandatory when using the skin attribute", LogUtility::LVL_MSG_WARNING, self::CANONICAL);
90
91        } else {
92            $type = $attributes->getValue(TagAttributes::TYPE_KEY);
93            if (
94                $skinValue === self::FILLED_VALUE
95                && ($attributes->hasClass("btn-$type")||$attributes->hasClass("alert-$type"))
96            ) {
97                $isBrandingColor = in_array($type, [ColorRgb::PRIMARY_VALUE, ColorRgb::SECONDARY_VALUE]);
98                if (!$isBrandingColor) {
99                    // example: light
100                    return;
101                }
102                if (!Site::isBrandingColorInheritanceFunctional()) {
103                    // example: primary, secondary
104                    return;
105                }
106            }
107
108            $skinColors = self::getSkinColors();
109            if (!isset($skinColors[$type])) {
110                $types = implode(", ", array_keys($skinColors));
111                LogUtility::msg("The type value ($type) is not supported. Only the following types value may be used: $types", LogUtility::LVL_MSG_WARNING, self::CANONICAL);
112            } else {
113                $color = $skinColors[$type];
114                switch ($skinValue) {
115                    case "contained":
116                        $attributes->addStyleDeclarationIfNotSet(ColorRgb::COLOR, $color[ColorRgb::COLOR]);
117                        $attributes->addStyleDeclarationIfNotSet(Background::BACKGROUND_COLOR, $color[Background::BACKGROUND_COLOR]);
118                        $attributes->addStyleDeclarationIfNotSet(ColorRgb::BORDER_COLOR, $color[ColorRgb::BORDER_COLOR]);
119                        Shadow::addMediumElevation($attributes);
120                        break;
121                    case self::FILLED_VALUE:
122                    case "solid":
123                        $attributes->addStyleDeclarationIfNotSet(ColorRgb::COLOR, $color[ColorRgb::COLOR]);
124                        $attributes->addStyleDeclarationIfNotSet(Background::BACKGROUND_COLOR, $color[Background::BACKGROUND_COLOR]);
125                        $attributes->addStyleDeclarationIfNotSet(ColorRgb::BORDER_COLOR, $color[ColorRgb::BORDER_COLOR]);
126                        break;
127                    case "outline":
128                        $primaryColor = $color[ColorRgb::COLOR];
129                        if ($primaryColor === "#fff") {
130                            $primaryColor = $color[Background::BACKGROUND_COLOR];
131                        }
132                        $attributes->addStyleDeclarationIfNotSet(ColorRgb::COLOR, $primaryColor);
133                        $attributes->addStyleDeclarationIfNotSet(Background::BACKGROUND_COLOR, "transparent");
134                        $borderColor = $color[Background::BACKGROUND_COLOR];
135                        if ($attributes->hasStyleDeclaration(ColorRgb::BORDER_COLOR)) {
136                            // Color in the `border` attribute
137                            // takes precedence in the `border-color` if located afterwards
138                            // We don't take the risk
139                            $borderColor = $attributes->getAndRemoveStyleDeclaration(ColorRgb::BORDER_COLOR);
140                        }
141                        $attributes->addStyleDeclarationIfNotSet("border", "1px solid " . $borderColor);
142
143                        break;
144                    case "text":
145                        $primaryColor = $color[ColorRgb::COLOR];
146                        if ($primaryColor === "#fff") {
147                            $primaryColor = $color[Background::BACKGROUND_COLOR];
148                        }
149                        $attributes->addStyleDeclarationIfNotSet(ColorRgb::COLOR, "$primaryColor!important");
150                        $attributes->addStyleDeclarationIfNotSet(Background::BACKGROUND_COLOR, "transparent");
151                        $attributes->addStyleDeclarationIfNotSet(ColorRgb::BORDER_COLOR, "transparent");
152                        break;
153                }
154            }
155        }
156    }
157
158}
159