FieldTrait   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 117
Duplicated Lines 0 %

Test Coverage

Coverage 11.54%

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 22
dl 0
loc 117
ccs 3
cts 26
cp 0.1154
rs 10
c 1
b 1
f 0
wmc 17

12 Methods

Rating   Name   Duplication   Size   Complexity  
A getTypeValue() 0 3 1
A populateFromLetter() 0 3 1
A getName() 0 3 1
A bootFieldTrait() 0 3 1
A addToPdf() 0 3 1
A pdfPrepareFont() 0 3 1
A hasColor() 0 3 1
A getMetaData() 0 3 1
A getColorArray() 0 10 5
A addMetaData() 0 3 1
A setTextTransform() 0 3 1
A getValue() 0 10 2
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
            $valueType = html_entity_decode($valueType);
81
82
            return $valueType;
83
        }
84
85
        return '<<' . $this->field . '>>';
86
    }
87
88
    /**
89
     * @return PdfLetterTrait
90
     */
91
    abstract public function getPdfLetter();
92
93
    /**
94
     * @return bool
95
     */
96
    public function hasColor()
97
    {
98
        return substr_count($this->color, ',') == 2;
99
    }
100
101
    /**
102
     * @return array|null
103
     */
104
    public function getColorArray()
105
    {
106
        if ($this->hasColor()) {
107
            list($red, $green, $blue) = explode(',', $this->color);
108
            if ($red && $green && $blue) {
109
                return [intval($red), intval($green), intval($blue)];
110
            }
111
        }
112
113
        return null;
114
    }
115
116
    public function setTextTransform($value)
117
    {
118
        $this->addMetaData(TextTransform::NAME, $value);
119
    }
120
121
    /**
122
     * @param $key
123
     * @param $value
124
     */
125
    public function addMetaData($key, $value)
126
    {
127
        $this->metadata->set($key, $value);
128
    }
129
130
131
    /**
132
     * @param $key
133
     * @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...
134
     * @return Metadata|mixed|string|null
135
     */
136
    public function getMetaData($key, $default = null)
137
    {
138
        return $this->metadata->get($key, $default);
139
    }
140
141
    /**
142
     * @param Fpdi $pdf
143
     */
144
    protected function pdfPrepareFont($pdf)
145
    {
146
        $pdf->SetFont('freesans', '', $this->size, '', true);
147
    }
148
}
149