1<?php
2/**
3 * Copyright (c) 2021. ComboStrap, Inc. and its affiliates. All Rights Reserved.
4 *
5 * This source code is licensed under the GPL license found in the
6 * COPYING  file in the root directory of this source tree.
7 *
8 * @license  GPL 3 (https://www.gnu.org/licenses/gpl-3.0.en.html)
9 * @author   ComboStrap <support@combostrap.com>
10 *
11 */
12
13namespace ComboStrap\TagAttribute;
14
15
16use ComboStrap\LogUtility;
17use ComboStrap\PluginUtility;
18use ComboStrap\SiteConfig;
19use ComboStrap\TagAttributes;
20
21class Shadow
22{
23
24    const ELEVATION_ATT = "elevation";
25    const SHADOW_ATT = "shadow";
26    const CANONICAL = "shadow";
27
28    const CONF_DEFAULT_VALUE = "defaultShadowLevel";
29
30    /**
31     * Historically, this is the shadow of material design for the button
32     */
33    const MEDIUM_ELEVATION_CLASS = "shadow-md";
34    const SNIPPET_ID = "shadow";
35
36    const CONF_SMALL_LEVEL_VALUE = "small";
37    const CONF_MEDIUM_LEVEL_VALUE = "medium";
38    const CONF_LARGE_LEVEL_VALUE = "large";
39    const CONF_EXTRA_LARGE_LEVEL_VALUE = "extra-large";
40
41    /**
42     * @param TagAttributes $attributes
43     */
44    public static function process(TagAttributes &$attributes)
45    {
46        $elevationValue = "";
47
48        if ($attributes->hasComponentAttribute(self::ELEVATION_ATT)) {
49            $elevationValue = $attributes->getValueAndRemove(self::ELEVATION_ATT);
50        } else {
51            if ($attributes->hasComponentAttribute(self::SHADOW_ATT)) {
52                $elevationValue = $attributes->getValueAndRemove(self::SHADOW_ATT);
53            }
54        }
55
56        if (!empty($elevationValue)) {
57
58            $shadowClass = self::getClass($elevationValue);
59            if (!empty($shadowClass)) {
60                $attributes->addClassName($shadowClass);
61            }
62
63        }
64
65    }
66
67
68    public
69    static function getDefaultClass()
70    {
71        $defaultValue = SiteConfig::getConfValue(self::CONF_DEFAULT_VALUE);
72        return self::getClass($defaultValue);
73    }
74
75    public
76    static function getClass($value)
77    {
78        // To string because if the value was true, the first string case
79        // would be chosen :(
80        if ($value === true) {
81            $value = "true";
82        }
83        /**
84         * The class are bootstrap class even without the bs suffix/prefix
85         * https://getbootstrap.com/docs/5.0/utilities/shadows/
86         */
87        switch ($value) {
88            case self::CONF_SMALL_LEVEL_VALUE:
89            case "sm":
90                return "shadow-sm";
91            case self::CONF_MEDIUM_LEVEL_VALUE:
92            case "md";
93                PluginUtility::getSnippetManager()->attachCssInternalStyleSheet(self::SNIPPET_ID);
94                return self::MEDIUM_ELEVATION_CLASS;
95            case self::CONF_LARGE_LEVEL_VALUE:
96            case "lg":
97                return "shadow";
98            case self::CONF_EXTRA_LARGE_LEVEL_VALUE:
99            case "xl":
100            case "high":
101                return "shadow-lg";
102                // Old deprecated: $styleProperties["box-shadow"] = "0 0 0 .2em rgba(3,102,214,0),0 13px 27px -5px rgba(50,50,93,.25),0 8px 16px -8px rgba(0,0,0,.3),0 -6px 16px -6px rgba(0,0,0,.025)";
103            case "true":
104            case "1":
105                return self::getDefaultClass();
106            default:
107                LogUtility::msg("The shadow / elevation value ($value) is unknown", LogUtility::LVL_MSG_ERROR, self::CANONICAL);
108                return null;
109        }
110    }
111
112    /**
113     * @param TagAttributes $attributes
114     */
115    public
116    static function addMediumElevation(&$attributes)
117    {
118        $attributes->addClassName(self::MEDIUM_ELEVATION_CLASS);
119    }
120
121}
122