1<?php
2
3
4namespace ComboStrap;
5
6
7class Toggle
8{
9
10    /**
11     * An indicator attribute that tells if the target element is collapsed or not (accordion)
12     */
13    const COLLAPSED = "collapsed";
14
15
16    /**
17     * The collapse attribute are the same
18     * for all component except a link
19     * @param TagAttributes $attributes
20     */
21    public
22    static function processToggle(&$attributes)
23    {
24
25        $collapse = "toggleTargetId";
26        if ($attributes->hasComponentAttribute($collapse)) {
27            $targetId = $attributes->getValueAndRemoveIfPresent($collapse);
28        } else {
29            $targetId = $attributes->getValueAndRemoveIfPresent("collapse");
30        }
31        if ($targetId != null) {
32            $bootstrapNamespace = "bs-";
33            if (Bootstrap::getBootStrapMajorVersion() == Bootstrap::BootStrapFourMajorVersion) {
34                $bootstrapNamespace = "";
35            }
36            /**
37             * We can use it in a link
38             */
39            if (substr($targetId, 0, 1) != "#") {
40                $targetId = "#" . $targetId;
41            }
42            $attributes->addComponentAttributeValue("data-{$bootstrapNamespace}toggle", "collapse");
43            $attributes->addComponentAttributeValue("data-{$bootstrapNamespace}target", $targetId);
44
45        }
46
47        $collapsed = self::COLLAPSED;
48        if ($attributes->hasComponentAttribute($collapsed)) {
49            $value = $attributes->getValueAndRemove($collapsed);
50            if ($value) {
51                $attributes->addClassName("collapse");
52            }
53        }
54    }
55
56}
57