Passed
Push — main ( 56908f...4059b3 )
by ANDREY
02:30
created

Text   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Test Coverage

Coverage 95.83%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 48
c 4
b 0
f 0
dl 0
loc 69
ccs 46
cts 48
cp 0.9583
rs 10
wmc 13

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setValue() 0 11 1
A getValue() 0 3 1
A splitText() 0 13 5
B render() 0 32 6
1
<?php
2
3
namespace VPA\Console\Glyphs;
4
5
use VPA\Console\FrameConfigInterface;
6
use VPA\Console\Symbol;
7
8
class Text extends GlyphInline
9
{
10
    private string $text = '';
11
12 31
    public function setValue(string $text): GlyphInline
13
    {
14 31
        $this->text = $text;
15 31
        $batches = $this->splitText();
16 31
        $lengths = array_map(function ($it) {
17 31
            return strlen($it);
18
        }, $batches);
19 31
        $this->width = max($lengths);
20 31
        $this->height = count($batches);
21 31
        $this->render();
22 31
        return $this;
23
    }
24
25
    public function getValue(): string
26
    {
27
        return $this->text;
28
    }
29
30 31
    public function render(): GlyphInline
31
    {
32 31
        $batches = $this->splitText();
33 31
        $lengths = array_map(function ($it) {
34 31
            return strlen($it);
35
        }, $batches);
36 31
        $this->height = count($batches);
37 31
        $maxLength = max($lengths);
38 31
        $parent = $this->getParent();
39 31
        $contentWidth = $parent && $parent instanceof GlyphBlock ? $parent->getContentWidth() : 0;
40 31
        $this->width = $contentWidth > 0 ? $contentWidth : min($maxLength, $this->__get('maxWidth'));
41 31
        $this->__set('maxWidth', max($this->width, $this->__get('maxWidth')));
42 31
        $padding = match ($this->__get('textAlign')) {
43 29
            default => STR_PAD_RIGHT,
44 1
            'right' => STR_PAD_LEFT,
45 1
            'center' => STR_PAD_BOTH,
46
        };
47 31
        $resultString = '';
48 31
        foreach ($batches as $batch) {
49 31
            $resultString .= str_pad(ltrim($batch), $this->getWidth(), ' ', $padding);
50
        }
51 31
        $symbols = str_split($resultString) ?? [];
52 31
        $symbolConfig = [
53 31
            'mode' => $this->__get('mode'),
54 31
            'color' => $this->__get('color'),
55 31
            'backgroundColor' => $this->__get('backgroundColor'),
56
        ];
57 31
        $this->renderMap = $this->getWidth() ? array_chunk(array_map(function ($value) use ($symbolConfig) {
58 31
            return (new Symbol($value))->setConfig($symbolConfig);
59 31
        }, $symbols), $this->getWidth()) : [];
0 ignored issues
show
Bug introduced by
It seems like $symbols can also be of type true; however, parameter $array of array_map() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

59
        }, /** @scrutinizer ignore-type */ $symbols), $this->getWidth()) : [];
Loading history...
60 31
        $this->height = count($this->renderMap);
61 31
        return $this;
62
    }
63
64 31
    private function splitText(): array
65
    {
66 31
        $parent = $this->getParent();
67 31
        $deltaWidth = $parent && $parent instanceof GlyphBlock ? $parent->getDeltaWidth() : 0;
68 31
        $result = [];
69 31
        $batches = explode("\n", $this->text);
70 31
        foreach ($batches as $batch) {
71 31
            $chunks = str_split(ltrim($batch), $this->__get('maxWidth') - $deltaWidth);
72 31
            foreach ($chunks as $chunk) {
73 31
                $result[] = $chunk;
74
            }
75
        }
76 31
        return $result;
77
    }
78
}
79