1<?php
2
3
4namespace ComboStrap;
5
6/**
7 * Class Skin
8 * @package ComboStrap
9 * Processing the skin attribute
10 */
11class Skin
12{
13
14    const CANONICAL = "skin";
15
16
17    static $colors = array(
18        "info" => array(
19            ColorUtility::COLOR => "#0c5460",
20            Background::BACKGROUND_COLOR => "#d1ecf1",
21            ColorUtility::BORDER_COLOR => "#bee5eb"
22        ),
23        "tip" => array(
24            ColorUtility::COLOR => "#6c6400",
25            Background::BACKGROUND_COLOR => "#fff79f",
26            ColorUtility::BORDER_COLOR => "#FFF78c"
27        ),
28        "warning" => array(
29            ColorUtility::COLOR => "#856404",
30            Background::BACKGROUND_COLOR => "#fff3cd",
31            ColorUtility::BORDER_COLOR => "#ffeeba"
32        ),
33        "primary" => array(
34            ColorUtility::COLOR => "#fff",
35            Background::BACKGROUND_COLOR => "#007bff",
36            ColorUtility::BORDER_COLOR => "#007bff"
37        ),
38        "secondary" => array(
39            ColorUtility::COLOR => "#fff",
40            Background::BACKGROUND_COLOR => "#6c757d",
41            ColorUtility::BORDER_COLOR => "#6c757d"
42        ),
43        "success" => array(
44            ColorUtility::COLOR => "#fff",
45            Background::BACKGROUND_COLOR => "#28a745",
46            ColorUtility::BORDER_COLOR => "#28a745"
47        ),
48        "danger" => array(
49            ColorUtility::COLOR => "#fff",
50            Background::BACKGROUND_COLOR => "#dc3545",
51            ColorUtility::BORDER_COLOR => "#dc3545"
52        ),
53        "dark" => array(
54            ColorUtility::COLOR => "#fff",
55            Background::BACKGROUND_COLOR => "#343a40",
56            ColorUtility::BORDER_COLOR => "#343a40"
57        ),
58        "light" => array(
59            ColorUtility::COLOR => "#fff",
60            Background::BACKGROUND_COLOR => "#f8f9fa",
61            ColorUtility::BORDER_COLOR => "#f8f9fa"
62        )
63    );
64
65    public static function processSkinAttribute(TagAttributes &$attributes)
66    {
67        // Skin
68        $skinAttributes = "skin";
69        if ($attributes->hasComponentAttribute($skinAttributes)) {
70            $skinValue = $attributes->getValueAndRemove($skinAttributes);
71            if (!$attributes->hasComponentAttribute(TagAttributes::TYPE_KEY)) {
72                LogUtility::msg("A component type is mandatory when using the skin attribute", LogUtility::LVL_MSG_WARNING, self::CANONICAL);
73
74            } else {
75                $type = $attributes->getValue(TagAttributes::TYPE_KEY);
76                if (!isset(self::$colors[$type])) {
77                    $types = implode(", ", array_keys(self::$colors));
78                    LogUtility::msg("The type value ($type) is not supported. Only the following types value may be used: $types", LogUtility::LVL_MSG_WARNING, self::CANONICAL);
79                } else {
80                    $color = self::$colors[$type];
81                    switch ($skinValue) {
82                        case "contained":
83                            $attributes->addStyleDeclarationIfNotSet(ColorUtility::COLOR, $color[ColorUtility::COLOR]);
84                            $attributes->addStyleDeclarationIfNotSet(Background::BACKGROUND_COLOR, $color[Background::BACKGROUND_COLOR]);
85                            $attributes->addStyleDeclarationIfNotSet(ColorUtility::BORDER_COLOR, $color[ColorUtility::BORDER_COLOR]);
86                            Shadow::addMediumElevation($attributes);
87                            break;
88                        case "filled":
89                        case "solid":
90                            $attributes->addStyleDeclarationIfNotSet(ColorUtility::COLOR, $color[ColorUtility::COLOR]);
91                            $attributes->addStyleDeclarationIfNotSet(Background::BACKGROUND_COLOR, $color[Background::BACKGROUND_COLOR]);
92                            $attributes->addStyleDeclarationIfNotSet(ColorUtility::BORDER_COLOR, $color[ColorUtility::BORDER_COLOR]);
93                            break;
94                        case "outline":
95                            $primaryColor = $color[ColorUtility::COLOR];
96                            if ($primaryColor === "#fff") {
97                                $primaryColor = $color[Background::BACKGROUND_COLOR];
98                            }
99                            $attributes->addStyleDeclarationIfNotSet(ColorUtility::COLOR, $primaryColor);
100                            $attributes->addStyleDeclarationIfNotSet(Background::BACKGROUND_COLOR, "transparent");
101                            $borderColor = $color[Background::BACKGROUND_COLOR];
102                            if ($attributes->hasStyleDeclaration(ColorUtility::BORDER_COLOR)) {
103                                // Color in the `border` attribute
104                                // takes precedence in the `border-color` if located afterwards
105                                // We don't take the risk
106                                $borderColor = $attributes->getAndRemoveStyleDeclaration(ColorUtility::BORDER_COLOR);
107                            }
108                            $attributes->addStyleDeclarationIfNotSet("border", "1px solid " . $borderColor);
109
110                            break;
111                        case "text":
112                            $primaryColor = $color[ColorUtility::COLOR];
113                            if ($primaryColor === "#fff") {
114                                $primaryColor = $color[Background::BACKGROUND_COLOR];
115                            }
116                            $attributes->addStyleDeclarationIfNotSet(ColorUtility::COLOR, $primaryColor);
117                            $attributes->addStyleDeclarationIfNotSet(Background::BACKGROUND_COLOR, "transparent");
118                            $attributes->addStyleDeclarationIfNotSet(ColorUtility::BORDER_COLOR, "transparent");
119                            break;
120                    }
121                }
122            }
123        }
124    }
125
126}
127