1<?php
2
3
4namespace ComboStrap\TagAttribute;
5
6
7use ComboStrap\Bootstrap;
8use ComboStrap\ConditionalValue;
9use ComboStrap\ExceptionBadSyntax;
10use ComboStrap\LogUtility;
11use ComboStrap\PluginUtility;
12use ComboStrap\TagAttributes;
13
14class Toggle
15{
16
17    /**
18     * An indicator attribute that tells if the target element is collapsed or not (accordion)
19     */
20    const COLLAPSED = "collapsed";
21    const TOGGLE_STATE = "toggle-state";
22    const TOGGLE_STATE_NONE = "none";
23    const TOGGLE_STATE_EXPANDED = "expanded";
24    const TOGGLE_STATE_COLLAPSED = "collapsed";
25    const CANONICAL = "toggle";
26
27
28    /**
29     * The collapse attribute are the same
30     * for all component except a link
31     * @param TagAttributes $attributes
32     *
33     */
34    public
35    static function processToggle(TagAttributes $attributes)
36    {
37
38
39        /**
40         * Toggle state
41         */
42        $value = $attributes->getValueAndRemove(self::TOGGLE_STATE);
43        if ($value !== null) {
44            if ($value === self::TOGGLE_STATE_NONE) {
45                return;
46            }
47            $values = explode(" ", $value);
48            foreach ($values as $value) {
49                if (empty($value)) {
50                    continue;
51                }
52                switch ($value) {
53                    case self::TOGGLE_STATE_EXPANDED:
54                        $attributes->addClassName("collapse show");
55                        break;
56                    case self::TOGGLE_STATE_COLLAPSED:
57                        $attributes->addClassName("collapse");
58                        break;
59                    default:
60                        /**
61                         * It may be a conditional breakpoint collapse
62                         */
63                        try {
64                            $conditionalValue = ConditionalValue::createFrom($value);
65                        } catch (ExceptionBadSyntax $e) {
66                            LogUtility::msg("The toggle state ($value) is invalid. It should be (expanded, collapsed or breakpoint-expanded)", LogUtility::LVL_MSG_ERROR, self::CANONICAL);
67                            continue 2;
68                        }
69
70                        $toggleStateValue = $conditionalValue->getValue();
71                        if ($toggleStateValue !== self::TOGGLE_STATE_EXPANDED) {
72                            LogUtility::msg("The toggle breakpoint ($value) supports only `expanded` as value, not $toggleStateValue.", LogUtility::LVL_MSG_ERROR, self::CANONICAL);
73                            continue 2;
74                        }
75                        $id = $attributes->getValue("id");
76                        if (empty($id)) {
77                            LogUtility::msg("A conditional toggle breakpoint ($value) needs an id attribute.", LogUtility::LVL_MSG_ERROR, self::CANONICAL);
78                            continue 2;
79                        }
80                        $breakpoint = $conditionalValue->getBreakpointSize();
81                        $styleSheet = <<<EOF
82@media (min-width: {$breakpoint}px) {
83   #{$id} {
84        display: block!important
85   }
86}
87EOF;
88                        /**
89                         * The snippet id is dependent on id
90                         * if there is more than one
91                         */
92                        $snippetId = self::CANONICAL."-$id";
93                        PluginUtility::getSnippetManager()->attachCssInternalStyleSheet($snippetId, $styleSheet);
94
95                }
96            }
97        }
98
99        /**
100         * Old
101         * https://combostrap.com/release/deprecated/toggle
102         * @deprecated
103         */
104        $collapse = "toggleTargetId";
105        if ($attributes->hasComponentAttribute($collapse)) {
106            $targetId = $attributes->getValueAndRemoveIfPresent($collapse);
107        } else {
108            $targetId = $attributes->getValueAndRemoveIfPresent("collapse");
109        }
110        if ($targetId != null) {
111            $bootstrapNamespace = "bs-";
112            if (Bootstrap::getBootStrapMajorVersion() == Bootstrap::BootStrapFourMajorVersion) {
113                $bootstrapNamespace = "";
114            }
115            /**
116             * We can use it in a link
117             */
118            if (substr($targetId, 0, 1) != "#") {
119                $targetId = "#" . $targetId;
120            }
121            $attributes->addOutputAttributeValue("data-{$bootstrapNamespace}toggle", "collapse");
122            $attributes->addOutputAttributeValue("data-{$bootstrapNamespace}target", $targetId);
123
124        }
125
126        /**
127         * Toggle state
128         * @deprecated
129         * https://combostrap.com/release/deprecated/toggle
130         */
131        $collapsed = self::COLLAPSED;
132        if ($attributes->hasComponentAttribute($collapsed)) {
133            $value = $attributes->getValueAndRemove($collapsed);
134            if ($value) {
135                $attributes->addClassName("collapse");
136            }
137        }
138
139
140    }
141
142    public static function disableEntity(string $mode): bool
143    {
144        if ($mode === "entity") {
145            return false;
146        }
147        return true;
148    }
149
150}
151