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

FieldTrait   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 116
Duplicated Lines 0 %

Test Coverage

Coverage 11.54%

Importance

Changes 0
Metric Value
eloc 21
dl 0
loc 116
ccs 3
cts 26
cp 0.1154
rs 10
c 0
b 0
f 0
wmc 17

12 Methods

Rating   Name   Duplication   Size   Complexity  
A getTypeValue() 0 3 1
A pdfPrepareFont() 0 3 1
A hasColor() 0 3 1
A getMetaData() 0 3 1
A populateFromLetter() 0 3 1
A getColorArray() 0 10 5
A addMetaData() 0 3 1
A setTextTransform() 0 3 1
A getName() 0 3 1
A getValue() 0 9 2
A bootFieldTrait() 0 3 1
A addToPdf() 0 3 1
1
<?php
2
3
namespace ByTIC\DocumentGenerator\PdfLetters\Models\Fields;
4
5
use ByTIC\DataObjects\Casts\Metadata\Metadata;
6
use ByTIC\DocumentGenerator\PdfLetters\Models\Fields\Attributes\TextTransform;
7
use ByTIC\DocumentGenerator\PdfLetters\Models\Fields\Types\AbstractType;
8
use ByTIC\DocumentGenerator\PdfLetters\Models\PdfLetters\PdfLetterTrait;
9
use ByTIC\Models\SmartProperties\RecordsTraits\HasTypes\RecordTrait as HasTypeRecordTrait;
10
use Nip\Records\Traits\AbstractTrait\RecordTrait as Record;
11
use ByTIC\DataObjects\Casts\Metadata\AsMetadataObject;
12
use setasign\Fpdi\Fpdi;
13
14
/**
15
 * Class FieldTrait
16
 * @package ByTIC\DocumentGenerator\PdfLetters\Models\Fields
17
 *
18
 * @property string $id_letter
19
 * @property string $field
20
 * @property string $size
21
 * @property string $color
22
 * @property string $align
23
 * @property string $x
24
 * @property string $y
25
 * @property string|Metadata $metadata
26
 *
27
 * @method FieldsTrait getManager()
28
 * @method AbstractType getType()
29
 */
30
trait FieldTrait
31
{
32
    use HasTypeRecordTrait;
33
34
    public function bootFieldTrait()
35
    {
36
        $this->addCast('metadata', AsMetadataObject::class . ':json');
37
    }
38
39
    /**
40
     * @return string
41
     */
42
    public function getName()
43
    {
44
        return translator()->trans($this->field);
45
    }
46
47
    /**
48
     * @return mixed
49
     */
50
    public function getTypeValue()
51
    {
52
        return $this->getManager()->getFieldTypeFromMergeTag($this->field);
53
    }
54
55
    /**
56
     * @param PdfLetterTrait $letter
57
     */
58
    public function populateFromLetter($letter)
59
    {
60
        $this->id_letter = $letter->id;
61
    }
62
63
    /**
64
     * @param Fpdi $pdf
65
     * @param Record $model
66
     */
67 1
    public function addToPdf($pdf, $model)
68
    {
69 1
        $this->getType()->addToPdf($pdf, $model);
70
    }
71
72
    /**
73
     * @param Record $model
74
     * @return string
75 1
     */
76
    public function getValue($model)
77
    {
78
        if ($model->id > 0) {
79
            $valueType = $this->getType()->getValue($model);
80
81
            return $valueType;
82
        }
83
84
        return '<<' . $this->field . '>>';
85
    }
86
87
    /**
88
     * @return PdfLetterTrait
89
     */
90
    abstract public function getPdfLetter();
91
92
    /**
93
     * @return bool
94
     */
95
    public function hasColor()
96
    {
97
        return substr_count($this->color, ',') == 2;
98
    }
99
100
    /**
101
     * @return array|null
102
     */
103
    public function getColorArray()
104
    {
105
        if ($this->hasColor()) {
106
            list($red, $green, $blue) = explode(',', $this->color);
107
            if ($red && $green && $blue) {
108
                return [intval($red), intval($green), intval($blue)];
109
            }
110
        }
111
112
        return null;
113
    }
114
115
    public function setTextTransform($value)
116
    {
117
        $this->addMetaData(TextTransform::NAME, $value);
118
    }
119
120
    /**
121
     * @param $key
122
     * @param $value
123
     */
124
    public function addMetaData($key, $value)
125
    {
126
        $this->metadata->set($key, $value);
127
    }
128
129
130
    /**
131
     * @param $key
132
     * @param null $default
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $default is correct as it would always require null to be passed?
Loading history...
133
     * @return Metadata|mixed|string|null
134
     */
135
    public function getMetaData($key, $default = null)
136
    {
137
        return $this->metadata->get($key, $default);
138
    }
139
140
    /**
141
     * @param Fpdi $pdf
142
     */
143
    protected function pdfPrepareFont($pdf)
144
    {
145
        $pdf->SetFont('freesans', '', $this->size, '', true);
146
    }
147
}
148