1<?php
2
3
4namespace ComboStrap;
5
6
7class Spacing
8{
9    const SPACING_ATTRIBUTE = "spacing";
10
11    /**
12     * Process the attributes that have an impact on the class
13     * @param TagAttributes $attributes
14     */
15    public static function processSpacingAttributes(&$attributes)
16    {
17
18        // Spacing is just a class
19        $spacing = self::SPACING_ATTRIBUTE;
20        if ($attributes->hasComponentAttribute($spacing)) {
21
22            $spacingValue = $attributes->getValueAndRemove($spacing);
23
24            /**
25             * Applying a padding on svg is not really recommended
26             * because it makes the icon invisble
27             */
28            $logicalTag = $attributes->getLogicalTag();
29            if ($logicalTag == SvgImageLink::CANONICAL) {
30                if (StringUtility::startWiths($spacingValue, "p")) {
31                    LogUtility::msg("We didn't apply the padding value ($spacingValue) on your svg or icon because it will make the icon invisible. Apply a margin or apply the spacing to the container component.", LogUtility::LVL_MSG_WARNING, SvgImageLink::CANONICAL);
32                    return;
33                }
34            }
35            if ($logicalTag == \syntax_plugin_combo_cell::TAG) {
36                if (StringUtility::startWiths($spacingValue, "m")) {
37                    $expectedValue = "p" . substr($spacingValue, 1);
38                    LogUtility::msg("We didn't apply the margin value ($spacingValue) on your column because it will make the last column in the grid to go to the line. Apply the following padding instead ($expectedValue)", LogUtility::LVL_MSG_WARNING, \syntax_plugin_combo_row::CANONICAL);
39                    $spacingValue = $expectedValue;
40                }
41            }
42
43            $spacingNames = preg_split("/\s/", $spacingValue);
44            $bootstrapVersion = Bootstrap::getBootStrapMajorVersion();
45            foreach ($spacingNames as $spacingClass) {
46                if ($bootstrapVersion == Bootstrap::BootStrapFiveMajorVersion) {
47
48                    // The sides r and l has been renamed to e and s
49                    // https://getbootstrap.com/docs/5.0/migration/#utilities-2
50                    //
51
52                    // https://getbootstrap.com/docs/5.0/utilities/spacing/
53                    // By default, we consider tha there is no size and breakpoint
54                    $sizeAndBreakPoint = "";
55                    $propertyAndSide = $spacingClass;
56
57                    $minusCharacter = "-";
58                    $minusLocation = strpos($spacingClass, $minusCharacter);
59                    if ($minusLocation !== false) {
60                        // There is no size or break point
61                        $sizeAndBreakPoint = substr($spacingClass, $minusLocation + 1);
62                        $propertyAndSide = substr($spacingClass, 0, $minusLocation);
63                    }
64                    $propertyAndSide = str_replace("r", "e", $propertyAndSide);
65                    $propertyAndSide = str_replace("l", "s", $propertyAndSide);
66                    if ($sizeAndBreakPoint === "") {
67                        $spacingClass = $propertyAndSide;
68                    } else {
69                        $spacingClass = $propertyAndSide . $minusCharacter . $sizeAndBreakPoint;
70                    }
71
72                }
73                $attributes->addClassName($spacingClass);
74            }
75        }
76
77    }
78}
79