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