TextLine   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 2
Metric Value
eloc 29
c 2
b 0
f 2
dl 0
loc 60
rs 10
wmc 9

5 Methods

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