xref: /plugin/combo/ComboStrap/Spacing.php (revision 04fd306c7c155fa133ebb3669986875d65988276)
137748cd8SNickeau<?php
237748cd8SNickeau
337748cd8SNickeau
437748cd8SNickeaunamespace ComboStrap;
537748cd8SNickeau
637748cd8SNickeau
737748cd8SNickeauclass Spacing
837748cd8SNickeau{
937748cd8SNickeau    const SPACING_ATTRIBUTE = "spacing";
1037748cd8SNickeau
1137748cd8SNickeau    /**
1237748cd8SNickeau     * Process the attributes that have an impact on the class
1337748cd8SNickeau     * @param TagAttributes $attributes
1437748cd8SNickeau     */
1537748cd8SNickeau    public static function processSpacingAttributes(&$attributes)
1637748cd8SNickeau    {
1737748cd8SNickeau
1837748cd8SNickeau        // Spacing is just a class
1937748cd8SNickeau        $spacing = self::SPACING_ATTRIBUTE;
2037748cd8SNickeau        if ($attributes->hasComponentAttribute($spacing)) {
2137748cd8SNickeau
2237748cd8SNickeau            $spacingValue = $attributes->getValueAndRemove($spacing);
2337748cd8SNickeau
2437748cd8SNickeau            /**
2537748cd8SNickeau             * Applying a padding on svg is not really recommended
2637748cd8SNickeau             * because it makes the icon invisble
2737748cd8SNickeau             */
2837748cd8SNickeau            $logicalTag = $attributes->getLogicalTag();
2937748cd8SNickeau            if ($logicalTag == SvgImageLink::CANONICAL) {
3037748cd8SNickeau                if (StringUtility::startWiths($spacingValue, "p")) {
3137748cd8SNickeau                    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);
3237748cd8SNickeau                    return;
3337748cd8SNickeau                }
3437748cd8SNickeau            }
3537748cd8SNickeau            if ($logicalTag == \syntax_plugin_combo_cell::TAG) {
3637748cd8SNickeau                if (StringUtility::startWiths($spacingValue, "m")) {
3737748cd8SNickeau                    $expectedValue = "p" . substr($spacingValue, 1);
38*04fd306cSNickeau                    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, GridTag::CANONICAL);
3937748cd8SNickeau                    $spacingValue = $expectedValue;
4037748cd8SNickeau                }
4137748cd8SNickeau            }
4237748cd8SNickeau
4337748cd8SNickeau            $spacingNames = preg_split("/\s/", $spacingValue);
4437748cd8SNickeau            $bootstrapVersion = Bootstrap::getBootStrapMajorVersion();
4537748cd8SNickeau            foreach ($spacingNames as $spacingClass) {
4637748cd8SNickeau                if ($bootstrapVersion == Bootstrap::BootStrapFiveMajorVersion) {
4737748cd8SNickeau
4837748cd8SNickeau                    // The sides r and l has been renamed to e and s
4937748cd8SNickeau                    // https://getbootstrap.com/docs/5.0/migration/#utilities-2
5037748cd8SNickeau                    //
5137748cd8SNickeau
5237748cd8SNickeau                    // https://getbootstrap.com/docs/5.0/utilities/spacing/
5337748cd8SNickeau                    // By default, we consider tha there is no size and breakpoint
5437748cd8SNickeau                    $sizeAndBreakPoint = "";
5537748cd8SNickeau                    $propertyAndSide = $spacingClass;
5637748cd8SNickeau
5737748cd8SNickeau                    $minusCharacter = "-";
5837748cd8SNickeau                    $minusLocation = strpos($spacingClass, $minusCharacter);
5937748cd8SNickeau                    if ($minusLocation !== false) {
6037748cd8SNickeau                        // There is no size or break point
6137748cd8SNickeau                        $sizeAndBreakPoint = substr($spacingClass, $minusLocation + 1);
6237748cd8SNickeau                        $propertyAndSide = substr($spacingClass, 0, $minusLocation);
6337748cd8SNickeau                    }
6437748cd8SNickeau                    $propertyAndSide = str_replace("r", "e", $propertyAndSide);
6537748cd8SNickeau                    $propertyAndSide = str_replace("l", "s", $propertyAndSide);
6637748cd8SNickeau                    if ($sizeAndBreakPoint === "") {
6737748cd8SNickeau                        $spacingClass = $propertyAndSide;
6837748cd8SNickeau                    } else {
6937748cd8SNickeau                        $spacingClass = $propertyAndSide . $minusCharacter . $sizeAndBreakPoint;
7037748cd8SNickeau                    }
7137748cd8SNickeau
7237748cd8SNickeau                }
7337748cd8SNickeau                $attributes->addClassName($spacingClass);
7437748cd8SNickeau            }
7537748cd8SNickeau        }
7637748cd8SNickeau
7737748cd8SNickeau    }
7837748cd8SNickeau}
79