Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
8 / 8 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
| NameResolver | |
100.00% |
8 / 8 |
|
100.00% |
3 / 3 |
5 | |
100.00% |
1 / 1 |
| isAmbiguousName | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getSuffix | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| getDefIdFromName | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace pvc\html\factory; |
| 4 | |
| 5 | use pvc\htmlbuilder\definitions\types\DefinitionType; |
| 6 | |
| 7 | class NameResolver |
| 8 | { |
| 9 | /** |
| 10 | * there are several identifiers in html which are duplicates, i.e. out of context, you would not know whether |
| 11 | * you are referring to an attribute or an element. For this reason and because we are using a single container, |
| 12 | * there are some cases where the definition id needs to be different from the name of the object. The |
| 13 | * method of disambiguation is to append an _attr or _element to the names of the objects and make those the |
| 14 | * definition ids. For example, cite => cite_attr / cite_element. |
| 15 | * |
| 16 | * The ambiguous identifiers are: |
| 17 | * |
| 18 | * cite |
| 19 | * data |
| 20 | * form |
| 21 | * label |
| 22 | * span |
| 23 | * style |
| 24 | * title |
| 25 | * |
| 26 | * @var array<string> |
| 27 | */ |
| 28 | private static array $ambiguousIdentifiers = [ |
| 29 | 'cite', |
| 30 | 'data', |
| 31 | 'form', |
| 32 | 'label', |
| 33 | 'span', |
| 34 | 'style', |
| 35 | 'title', |
| 36 | 'type', |
| 37 | ]; |
| 38 | |
| 39 | /** |
| 40 | * @var string |
| 41 | */ |
| 42 | private static string $attributeSuffix = '_attr'; |
| 43 | |
| 44 | /** |
| 45 | * @var string |
| 46 | */ |
| 47 | private static string $elementSuffix = '_element'; |
| 48 | |
| 49 | /** |
| 50 | * @param string $name |
| 51 | * @return bool |
| 52 | */ |
| 53 | protected static function isAmbiguousName(string $name): bool |
| 54 | { |
| 55 | return(in_array($name, self::$ambiguousIdentifiers)); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * @param DefinitionType $defType |
| 60 | * |
| 61 | * @return string |
| 62 | */ |
| 63 | protected static function getSuffix(DefinitionType $defType): string |
| 64 | { |
| 65 | return ($defType === DefinitionType::Attribute) ? |
| 66 | self::$attributeSuffix : |
| 67 | self::$elementSuffix; |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * @param string $name |
| 72 | * @param DefinitionType $defType |
| 73 | * |
| 74 | * @return string |
| 75 | */ |
| 76 | public static function getDefIdFromName(string $name, DefinitionType $defType): string |
| 77 | { |
| 78 | $suffix = self::isAmbiguousName($name) ? |
| 79 | self::getSuffix($defType) : |
| 80 | ''; |
| 81 | return $name . $suffix; |
| 82 | } |
| 83 | } |