Passed
Push — master ( 671b4b...06a963 )
by Gabriel
04:41
created

FieldsTrait::initMergeFields()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 4
c 0
b 0
f 0
dl 0
loc 7
ccs 0
cts 5
cp 0
rs 10
cc 2
nc 2
nop 0
crap 6
1
<?php
2
3
namespace ByTIC\DocumentGenerator\PdfLetters\Models\Fields;
4
5
use ByTIC\DocumentGenerator\PdfLetters\Models\Fields\Types\AbstractType;
6
use ByTIC\DocumentGenerator\PdfLetters\Models\PdfLetters\PdfLettersTrait;
7
use Nip\Records\Traits\AbstractTrait\RecordsTrait as AbstractRecordsTrait;
8
use ByTIC\Models\SmartProperties\RecordsTraits\HasTypes\RecordsTrait as HasTypeRecordsTrait;
9
10
/**
11
 * Class FieldsTrait
12
 * @package ByTIC\DocumentGenerator\PdfLetters\Models\Fields
13
 */
14
trait FieldsTrait
15
{
16
    use AbstractRecordsTrait;
17
    use HasTypeRecordsTrait;
18
19
    /**
20
     * @var null|array
21
     */
22
    protected $mergeFields = null;
23
    /**
24
     * @var null
25
     */
26
    protected $mergeFieldsType = null;
27
28
    /**
29
     * @return array
30
     */
31
    public function getMergeFields()
32
    {
33
        if ($this->mergeFields === null) {
34
            $this->initMergeFields();
35
        }
36
37
        return $this->mergeFields;
38
    }
39
40
    /**
41
     * @param AbstractType $type
42
     */
43
    public function populateTagsFromType($type)
44
    {
45
        $typeTags = (array)$type->providesTags();
46
        foreach ($typeTags as $tag) {
47
            $this->mergeFields[$type->getCategory()][] = $tag;
48
            $this->mergeFieldsType[$tag] = $type->getName();
49
        }
50
    }
51
52
    /**
53
     * @param $tag
54
     * @return null|string
55
     */
56
    public function getFieldTypeFromMergeTag($tag)
57
    {
58
        if ($this->mergeFieldsType === null) {
59
            $this->initMergeFields();
60
        }
61
        if (isset($this->mergeFieldsType[$tag])) {
62
            return $this->mergeFieldsType[$tag];
63
        }
64
65
        return null;
66
    }
67
68
    /**
69
     * @param array $params
70
     */
71
    public function injectParams(&$params = [])
72
    {
73
        /** @noinspection PhpUndefinedClassInspection */
74
        parent::injectParams($params);
75
        $params['order'][] = ['Y', 'ASC'];
76
        $params['order'][] = ['X', 'ASC', false];
77
    }
78
79
    /**
80
     * @return PdfLettersTrait
81
     */
82
    abstract public function getLetterManager();
83
84
    protected function initMergeFields()
85
    {
86
        /** @var AbstractType[] $types */
87
        $types = $this->getTypes();
88
        $this->mergeFields = [];
89
        foreach ($types as $type) {
90
            $this->populateTagsFromType($type);
91
        }
92
    }
93
}
94