1<?php
2
3namespace ComboStrap;
4
5
6class TextAlign
7{
8
9    const ATTRIBUTE_NAME = "text-align";
10    const CANONICAL = "text-align";
11
12
13    /**
14     * @param TagAttributes $attributes
15     * https://getbootstrap.com/docs/5.0/utilities/text/#text-alignment
16     */
17    public static function processTextAlign(&$attributes)
18    {
19
20        if ($attributes->hasComponentAttribute(self::ATTRIBUTE_NAME)) {
21
22            $textAlignValues = $attributes->getValuesAndRemove(self::ATTRIBUTE_NAME);
23            foreach ($textAlignValues as $textAlignValue) {
24                $conditionalTextAlignValue = ConditionalValue::createFrom($textAlignValue);
25
26                $bootstrapMajorVersion = Bootstrap::getBootStrapMajorVersion();
27                if ($bootstrapMajorVersion == Bootstrap::BootStrapFourMajorVersion) {
28                    $breakpoint = $conditionalTextAlignValue->getBreakpoint();
29                    if (!empty($breakpoint)) {
30                        LogUtility::msg("Bootstrap 4 does not support conditional value for the attribute (" . self::ATTRIBUTE_NAME . "). Therefore, the value ($textAlignValue) cannot be applied", LogUtility::LVL_MSG_WARNING, self::CANONICAL);
31                    }
32                    $value = $conditionalTextAlignValue->getValue();
33                    // Bootstrap 4
34                    switch ($value) {
35                        case "left":
36                        case "start":
37                            $attributes->addStyleDeclarationIfNotSet(self::ATTRIBUTE_NAME, "left");
38                            break;
39                        case "right":
40                        case "end":
41                            $attributes->addStyleDeclarationIfNotSet(self::ATTRIBUTE_NAME, "right");
42                            break;
43                        case "center":
44                        case "justify":
45                            $attributes->addStyleDeclarationIfNotSet(self::ATTRIBUTE_NAME, $value);
46                            break;
47                        default:
48                            LogUtility::msg("The text-align value ($value) is unknown.", LogUtility::LVL_MSG_ERROR, self::CANONICAL);
49                            break;
50                    }
51
52                } else {
53                    $breakpoint = $conditionalTextAlignValue->getBreakpoint();
54                    if (!empty($breakpoint)) {
55                        switch ($breakpoint) {
56                            case "sm":
57                            case "small":
58                                $breakpoint = "sm";
59                                break;
60                            case "md":
61                            case "medium":
62                                $breakpoint = "md";
63                                break;
64                            case "lg":
65                            case "large":
66                                $breakpoint = "lg";
67                                break;
68                            case "xl":
69                            case "extra-large":
70                                $breakpoint = "xl";
71                                break;
72                            default:
73                                LogUtility::msg("The breakpoint ($breakpoint) of the text-align value ($textAlignValue) is not correct.", LogUtility::LVL_MSG_ERROR, self::CANONICAL);
74                                $breakpoint = "";
75                                break;
76                        }
77                    }
78                    $value = $conditionalTextAlignValue->getValue();
79                    // Bootstrap 5
80                    switch ($value) {
81                        case "start":
82                        case "left": // from bs4
83                            $valueClass = "start";
84                            if (empty($breakpoint)) {
85                                $attributes->addClassName("text-$valueClass");
86                            } else {
87                                $attributes->addClassName("text-$breakpoint-$valueClass");
88                            }
89                            break;
90                        case "end":
91                        case "right": // from bs4
92                            $valueClass = "end";
93                            if (empty($breakpoint)) {
94                                $attributes->addClassName("text-$valueClass");
95                            } else {
96                                $attributes->addClassName("text-$breakpoint-$valueClass");
97                            }
98                            break;
99                        case "center":
100                            $valueClass = "center";
101                            if (empty($breakpoint)) {
102                                $attributes->addClassName("text-$valueClass");
103                            } else {
104                                $attributes->addClassName("text-$breakpoint-$valueClass");
105                            }
106                            break;
107                        case "justify":
108                            if (!empty($breakpoint)) {
109                                LogUtility::msg("The `justify` value of the text-align attribute does not support actually breakpoint. The breakpoint value ($$breakpoint) was then not applied.", LogUtility::LVL_MSG_WARNING, self::CANONICAL);
110                            }
111                            $attributes->addStyleDeclarationIfNotSet(self::ATTRIBUTE_NAME, $value);
112                            break;
113                        default:
114                            LogUtility::msg("The text-align value ($value) is unknown.", LogUtility::LVL_MSG_ERROR, self::CANONICAL);
115                            break;
116                    }
117                }
118            }
119        }
120    }
121}
122