Passed
Push — master ( 90b3af...835a3e )
by Gabriel
14:13
created

TextLine   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 26
dl 0
loc 56
rs 10
c 1
b 0
f 1
wmc 9

5 Methods

Rating   Name   Duplication   Size   Complexity  
A writeLine() 0 8 3
A writeMultiLine() 0 8 2
A prepareStyles() 0 4 1
A write() 0 9 2
A populateFromField() 0 7 1
1
<?php
2
declare(strict_types=1);
3
4
namespace ByTIC\DocumentGenerator\PdfLetters\Writer;
5
6
use ByTIC\DocumentGenerator\PdfLetters\Models\Fields\Attributes\TextTransform;
7
use ByTIC\DocumentGenerator\PdfLetters\PdfHelper;
8
9
class TextLine extends AbstractObject
10
{
11
12
    protected $size;
13
14
    protected $colorArray;
15
16
    protected $multiline = true;
17
18
    /**
19
     * @inheritDoc
20
     */
21
    protected function populateFromField($field): void
22
    {
23
        parent::populateFromField($field);
24
        $this->size = $field->size;
25
        $this->colorArray = $field->getColorArray();
26
27
        $this->value = TextTransform::transform($this->value, $field->getMetaData(TextTransform::NAME));
28
    }
29
30
    public function write()
31
    {
32
        $this->prepareStyles();
33
        /** Set positions before Fonts and colors */
34
        if ($this->multiline) {
35
            $this->writeMultiLine($this->value);
36
            return;
37
        }
38
        $this->writeLine($this->value);
39
    }
40
41
    protected function writeMultiLine($value)
42
    {
43
        $lines = explode("\n", $value);
44
        $y = $this->y;
45
        $height = $this->size*0.5;
46
        foreach ($lines as $line) {
47
            $this->writeLine($line, null, $y);
48
            $y += $height;
49
        }
50
    }
51
    protected function writeLine($value, $x = null , $y = null)
52
    {
53
        $y = $y ?: $this->y;
54
        $x = $x ?: $this->x;
55
        $y = PdfHelper::pdfYPosition($this->pdf, $value, $y);
56
        $x = PdfHelper::pdfXPosition($this->pdf, $value, $x, $this->align);
57
58
        $this->pdf->Text($x, $y, $value);
59
    }
60
61
    protected function prepareStyles()
62
    {
63
        $this->pdf->setFont('freesans', '', $this->size, '', true);
64
        PdfHelper::pdfPrepareColor($this->pdf, $this->colorArray);
65
    }
66
}