Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
26 / 26 |
|
100.00% |
8 / 8 |
CRAP | |
100.00% |
1 / 1 |
| AttributesTrait | |
100.00% |
26 / 26 |
|
100.00% |
8 / 8 |
16 | |
100.00% |
1 / 1 |
| addAttribute | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| addAttributes | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| hasAttributes | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| hasAttribute | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
3 | |||
| getAttributes | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getAttributesByName | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
3 | |||
| removeAttribute | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
| formatAttributes | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
3 | |||
| 1 | <?php |
| 2 | declare(strict_types=1); |
| 3 | /** |
| 4 | * Pop PHP Framework (https://www.popphp.org/) |
| 5 | * |
| 6 | * @link https://github.com/popphp/popphp-framework |
| 7 | * @author Nick Sagona, III <dev@noladev.com> |
| 8 | * @copyright Copyright (c) 2009-2027 NOLA Interactive, LLC. |
| 9 | * @license https://www.popphp.org/license New BSD License |
| 10 | */ |
| 11 | |
| 12 | /** |
| 13 | * @namespace |
| 14 | */ |
| 15 | namespace Pop\Code\Generator\Traits; |
| 16 | |
| 17 | use Pop\Code\Generator\AttributeGenerator; |
| 18 | |
| 19 | /** |
| 20 | * Attributes trait |
| 21 | * |
| 22 | * @category Pop |
| 23 | * @package Pop\Code |
| 24 | * @author Nick Sagona, III <dev@noladev.com> |
| 25 | * @copyright Copyright (c) 2009-2027 NOLA Interactive, LLC. |
| 26 | * @license https://www.popphp.org/license New BSD License |
| 27 | * @version 6.0.0 |
| 28 | */ |
| 29 | trait AttributesTrait |
| 30 | { |
| 31 | |
| 32 | /** |
| 33 | * Attributes. An indexed list, not keyed by name — attributes can legitimately repeat |
| 34 | * (Attribute::IS_REPEATABLE), so a name-keyed map would silently collapse repeats. |
| 35 | * @var array |
| 36 | */ |
| 37 | protected array $attributes = []; |
| 38 | |
| 39 | /** |
| 40 | * Add an attribute |
| 41 | * |
| 42 | * @param AttributeGenerator $attribute |
| 43 | * @return static |
| 44 | */ |
| 45 | public function addAttribute(AttributeGenerator $attribute): static |
| 46 | { |
| 47 | $this->attributes[] = $attribute; |
| 48 | return $this; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Add attributes |
| 53 | * |
| 54 | * @param array $attributes |
| 55 | * @return static |
| 56 | */ |
| 57 | public function addAttributes(array $attributes): static |
| 58 | { |
| 59 | foreach ($attributes as $attribute) { |
| 60 | $this->addAttribute($attribute); |
| 61 | } |
| 62 | return $this; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Has attributes |
| 67 | * |
| 68 | * @return bool |
| 69 | */ |
| 70 | public function hasAttributes(): bool |
| 71 | { |
| 72 | return !empty($this->attributes); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Has an attribute with the given name |
| 77 | * |
| 78 | * @param string $name |
| 79 | * @return bool |
| 80 | */ |
| 81 | public function hasAttribute(string $name): bool |
| 82 | { |
| 83 | foreach ($this->attributes as $attribute) { |
| 84 | if ($attribute->getName() === $name) { |
| 85 | return true; |
| 86 | } |
| 87 | } |
| 88 | return false; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Get all attributes |
| 93 | * |
| 94 | * @return array |
| 95 | */ |
| 96 | public function getAttributes(): array |
| 97 | { |
| 98 | return $this->attributes; |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Get all attributes with the given name (supports repeated attributes) |
| 103 | * |
| 104 | * @param string $name |
| 105 | * @return array |
| 106 | */ |
| 107 | public function getAttributesByName(string $name): array |
| 108 | { |
| 109 | $matches = []; |
| 110 | foreach ($this->attributes as $attribute) { |
| 111 | if ($attribute->getName() === $name) { |
| 112 | $matches[] = $attribute; |
| 113 | } |
| 114 | } |
| 115 | return $matches; |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Remove an attribute (by instance identity — there is no unique name to remove by) |
| 120 | * |
| 121 | * @param AttributeGenerator $attribute |
| 122 | * @return static |
| 123 | */ |
| 124 | public function removeAttribute(AttributeGenerator $attribute): static |
| 125 | { |
| 126 | $key = array_search($attribute, $this->attributes, true); |
| 127 | if ($key !== false) { |
| 128 | unset($this->attributes[$key]); |
| 129 | $this->attributes = array_values($this->attributes); |
| 130 | } |
| 131 | return $this; |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Format the attributes |
| 136 | * |
| 137 | * @param bool $indented |
| 138 | * @return string |
| 139 | */ |
| 140 | protected function formatAttributes(bool $indented = true): string |
| 141 | { |
| 142 | $output = ''; |
| 143 | $prefix = $indented ? $this->printIndent() : ''; |
| 144 | |
| 145 | foreach ($this->attributes as $attribute) { |
| 146 | $output .= $prefix . $attribute->render() . PHP_EOL; |
| 147 | } |
| 148 | |
| 149 | return $output; |
| 150 | } |
| 151 | |
| 152 | } |