Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
79.03% |
49 / 62 |
|
90.00% |
9 / 10 |
CRAP | |
0.00% |
0 / 1 |
| Column | |
79.03% |
49 / 62 |
|
90.00% |
9 / 10 |
44.66 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| new | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| bold | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| italic | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| badge | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| date | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| sort | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| sortKey | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| get | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
7 | |||
| getValue | |
65.79% |
25 / 38 |
|
0.00% |
0 / 1 |
30.97 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Cosray; |
| 6 | |
| 7 | use Closure; |
| 8 | use Cosray\Field\FieldHydrator; |
| 9 | use Cosray\Node\Node; |
| 10 | |
| 11 | final class Column |
| 12 | { |
| 13 | private bool|Closure $bold = false; |
| 14 | private bool|Closure $italic = false; |
| 15 | private bool|Closure $badge = false; |
| 16 | private bool|Closure $date = false; |
| 17 | private ?string $sort = null; |
| 18 | private string|Closure $color = ''; |
| 19 | |
| 20 | public function __construct( |
| 21 | public readonly string $title, |
| 22 | public readonly string|Closure $field, |
| 23 | ) {} |
| 24 | |
| 25 | public static function new( |
| 26 | string|Closure $title, |
| 27 | string|Closure $field, |
| 28 | ): self { |
| 29 | return new self($title, $field); |
| 30 | } |
| 31 | |
| 32 | public function bold(bool|Closure $bold): self |
| 33 | { |
| 34 | $this->bold = $bold; |
| 35 | |
| 36 | return $this; |
| 37 | } |
| 38 | |
| 39 | public function italic(bool|Closure $italic): self |
| 40 | { |
| 41 | $this->italic = $italic; |
| 42 | |
| 43 | return $this; |
| 44 | } |
| 45 | |
| 46 | public function badge(bool|Closure $badge): self |
| 47 | { |
| 48 | $this->badge = $badge; |
| 49 | |
| 50 | return $this; |
| 51 | } |
| 52 | |
| 53 | public function date(bool|Closure $date): self |
| 54 | { |
| 55 | $this->date = $date; |
| 56 | |
| 57 | return $this; |
| 58 | } |
| 59 | |
| 60 | public function sort(?string $sort): self |
| 61 | { |
| 62 | $sort = trim((string) $sort); |
| 63 | $this->sort = $sort === '' ? null : $sort; |
| 64 | |
| 65 | return $this; |
| 66 | } |
| 67 | |
| 68 | public function sortKey(): ?string |
| 69 | { |
| 70 | return $this->sort; |
| 71 | } |
| 72 | |
| 73 | public function get(Node $node): array |
| 74 | { |
| 75 | return [ |
| 76 | 'value' => is_string($this->field) |
| 77 | ? $this->getValue($node, $this->field) |
| 78 | : ($this->field)($node), |
| 79 | 'bold' => is_bool($this->bold) ? $this->bold : ($this->bold)($node), |
| 80 | 'italic' => is_bool($this->italic) ? $this->italic : ($this->italic)($node), |
| 81 | 'badge' => is_bool($this->badge) ? $this->badge : ($this->badge)($node), |
| 82 | 'date' => is_bool($this->date) ? $this->date : ($this->date)($node), |
| 83 | 'color' => is_string($this->color) ? $this->color : ($this->color)($node), |
| 84 | ]; |
| 85 | } |
| 86 | |
| 87 | private function getValue(Node $node, string $field): mixed |
| 88 | { |
| 89 | $inner = Node::unwrap($node); |
| 90 | |
| 91 | switch ($field) { |
| 92 | case 'title': |
| 93 | return $node->title(); |
| 94 | case 'meta.name': |
| 95 | return $node->meta->name; |
| 96 | case 'meta.uid': |
| 97 | case 'meta.published': |
| 98 | case 'meta.hidden': |
| 99 | case 'meta.locked': |
| 100 | case 'meta.created': |
| 101 | case 'meta.changed': |
| 102 | case 'meta.deleted': |
| 103 | case 'meta.content': |
| 104 | case 'meta.handle': |
| 105 | $key = explode('.', $field)[1]; |
| 106 | |
| 107 | return $node->meta->get($key); |
| 108 | case 'meta.class': |
| 109 | return $inner::class; |
| 110 | case 'meta.classname': |
| 111 | return basename(str_replace('\\', '/', $inner::class)); |
| 112 | case 'meta.editor': |
| 113 | $editorData = (array) $node->meta->get('editor_data', []); |
| 114 | |
| 115 | return ( |
| 116 | escape( |
| 117 | $editorData['name'] ?? $node->meta->get('editor_username'), |
| 118 | ) ?? $node->meta->get('editor_email') |
| 119 | ); |
| 120 | case 'meta.creator': |
| 121 | $creatorData = (array) $node->meta->get('creator_data', []); |
| 122 | |
| 123 | return ( |
| 124 | escape( |
| 125 | $creatorData['name'] ?? $node->meta->get('creator_username'), |
| 126 | ) ?? $node->meta->get('creator_email') |
| 127 | ); |
| 128 | default: |
| 129 | $hydrator = new FieldHydrator(); |
| 130 | $fieldObj = $hydrator->getField($inner, $field); |
| 131 | $value = $fieldObj->value(); |
| 132 | |
| 133 | return $value->isset() ? (string) $value : null; |
| 134 | } |
| 135 | } |
| 136 | } |