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

Text::setValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 9
c 2
b 0
f 0
nc 1
nop 1
dl 0
loc 11
ccs 9
cts 9
cp 1
crap 1
rs 9.9666
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