1<?php 2 3 4namespace ComboStrap; 5 6 7class Boldness 8{ 9 10 const BOLDNESS_ATTRIBUTE = "boldness"; 11 const CANONICAL = self::BOLDNESS_ATTRIBUTE; 12 13 const BOOLEAN_ATTRIBUTES = ["bold", "bolder"]; 14 15 /** 16 * 17 * https://getbootstrap.com/docs/5.0/utilities/text/#font-weight-and-italics 18 * @param TagAttributes $tagAttributes 19 */ 20 public static function processBoldnessAttribute(TagAttributes &$tagAttributes) 21 { 22 23 if ($tagAttributes->hasComponentAttribute(self::BOLDNESS_ATTRIBUTE)) { 24 $value = $tagAttributes->getValueAndRemove(self::BOLDNESS_ATTRIBUTE); 25 } 26 27 foreach(Boldness::BOOLEAN_ATTRIBUTES as $booleanAttribute) { 28 if ($tagAttributes->hasComponentAttribute($booleanAttribute)) { 29 $tagAttributes->removeComponentAttribute($booleanAttribute); 30 $value = $booleanAttribute; 31 } 32 } 33 34 if (!empty($value)) { 35 if (in_array($value, ["bolder", "lighter"])) { 36 $tagAttributes->addClassName("fw-$value"); 37 } else { 38 $value = Boldness::toNumericValue($value); 39 $tagAttributes->addStyleDeclarationIfNotSet("font-weight", $value); 40 } 41 } 42 43 44 } 45 46 private static function toNumericValue($value) 47 { 48 if (is_numeric($value)) { 49 return $value; 50 } 51 $value = strtolower($value); 52 switch ($value) { 53 case 'thin': 54 return 100; 55 case 'extra-light': 56 return 200; 57 case 'light': 58 return 300; 59 case 'normal': 60 return 400; 61 case 'medium': 62 return 500; 63 case 'semi-bold': 64 return 600; 65 case 'bold': 66 return 700; 67 case 'extra-bold': 68 return 800; 69 case 'black': 70 return 900; 71 case 'extra-black': 72 return 950; 73 default: 74 LogUtility::msg("The boldness name ($value) is unknown. The attribute was not applied", LogUtility::LVL_MSG_ERROR, self::CANONICAL); 75 return 400; 76 } 77 78 } 79 80 81} 82