1<?php 2 3namespace ComboStrap\TagAttribute; 4 5 6use ComboStrap\Bootstrap; 7use ComboStrap\ConditionalLength; 8use ComboStrap\ExceptionBadArgument; 9use ComboStrap\LogUtility; 10use ComboStrap\TagAttributes; 11 12/** 13 * @deprecated 14 */ 15class TextAlign 16{ 17 18 const ATTRIBUTE_NAME = "text-align"; 19 const CANONICAL = "text-align"; 20 21 22 /** 23 * @param TagAttributes $attributes 24 * https://getbootstrap.com/docs/5.0/utilities/text/#text-alignment 25 */ 26 public static function processTextAlign(&$attributes) 27 { 28 29 if ($attributes->hasComponentAttribute(self::ATTRIBUTE_NAME)) { 30 31 LogUtility::warning("The text-align attribute has been deprecated for the align attribute.", self::CANONICAL); 32 33 try { 34 $textAlignValues = $attributes->getValuesAndRemove(self::ATTRIBUTE_NAME); 35 } catch (ExceptionBadArgument $e) { 36 LogUtility::error("Unable to retrieve the tex-align attribute. Error: {$e->getMessage()}", self::CANONICAL); 37 return; 38 } 39 foreach ($textAlignValues as $textAlignValue) { 40 try { 41 $conditionalTextAlignValue = ConditionalLength::createFromString($textAlignValue); 42 } catch (ExceptionBadArgument $e) { 43 LogUtility::error("The text-align value($textAlignValue) is not valid. Error: {$e->getMessage()}", self::CANONICAL); 44 return; 45 } 46 47 $bootstrapMajorVersion = Bootstrap::getBootStrapMajorVersion(); 48 if ($bootstrapMajorVersion == Bootstrap::BootStrapFourMajorVersion) { 49 $breakpoint = $conditionalTextAlignValue->getBreakpoint(); 50 if (!empty($breakpoint)) { 51 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); 52 } 53 $value = $conditionalTextAlignValue->getLength(); 54 // Bootstrap 4 55 switch ($value) { 56 case "left": 57 case "start": 58 $attributes->addStyleDeclarationIfNotSet(self::ATTRIBUTE_NAME, "left"); 59 break; 60 case "right": 61 case "end": 62 $attributes->addStyleDeclarationIfNotSet(self::ATTRIBUTE_NAME, "right"); 63 break; 64 case "center": 65 case "justify": 66 $attributes->addStyleDeclarationIfNotSet(self::ATTRIBUTE_NAME, $value); 67 break; 68 default: 69 LogUtility::msg("The text-align value ($value) is unknown.", LogUtility::LVL_MSG_ERROR, self::CANONICAL); 70 break; 71 } 72 73 } else { 74 $breakpoint = $conditionalTextAlignValue->getBreakpoint(); 75 if (!empty($breakpoint)) { 76 switch ($breakpoint) { 77 case "sm": 78 case "small": 79 $breakpoint = "sm"; 80 break; 81 case "md": 82 case "medium": 83 $breakpoint = "md"; 84 break; 85 case "lg": 86 case "large": 87 $breakpoint = "lg"; 88 break; 89 case "xl": 90 case "extra-large": 91 $breakpoint = "xl"; 92 break; 93 default: 94 LogUtility::msg("The breakpoint ($breakpoint) of the text-align value ($textAlignValue) is not correct.", LogUtility::LVL_MSG_ERROR, self::CANONICAL); 95 $breakpoint = ""; 96 break; 97 } 98 } 99 $value = $conditionalTextAlignValue->getLength(); 100 // Bootstrap 5 101 switch ($value) { 102 case "start": 103 case "left": // from bs4 104 $valueClass = "start"; 105 if (empty($breakpoint)) { 106 $attributes->addClassName("text-$valueClass"); 107 } else { 108 $attributes->addClassName("text-$breakpoint-$valueClass"); 109 } 110 break; 111 case "end": 112 case "right": // from bs4 113 $valueClass = "end"; 114 if (empty($breakpoint)) { 115 $attributes->addClassName("text-$valueClass"); 116 } else { 117 $attributes->addClassName("text-$breakpoint-$valueClass"); 118 } 119 break; 120 case "center": 121 $valueClass = "center"; 122 if (empty($breakpoint)) { 123 $attributes->addClassName("text-$valueClass"); 124 } else { 125 $attributes->addClassName("text-$breakpoint-$valueClass"); 126 } 127 break; 128 case "justify": 129 if (!empty($breakpoint)) { 130 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); 131 } 132 $attributes->addStyleDeclarationIfNotSet(self::ATTRIBUTE_NAME, $value); 133 break; 134 default: 135 LogUtility::msg("The text-align value ($value) is unknown.", LogUtility::LVL_MSG_ERROR, self::CANONICAL); 136 break; 137 } 138 } 139 } 140 } 141 } 142} 143