Test Failed
Push — master ( e0e26b...b7d9e6 )
by Sergey
04:46
created

TxtRecord::splitTxtData()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 1
cts 1
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
crap 2
1
<?php
2
/**
3
 * @author: Viskov Sergey
4
 * @date  : 4/12/16
5
 * @time  : 1:00 PM
6
 */
7
8
namespace LTDBeget\dns\configurator\zoneEntities\record;
9
10
use LTDBeget\dns\configurator\errors\ValidationError;
11
use LTDBeget\dns\configurator\zoneEntities\Node;
12
use LTDBeget\dns\configurator\zoneEntities\record\base\Record;
13
use LTDBeget\dns\enums\eErrorCode;
14
use LTDBeget\dns\enums\eRecordType;
15
16
/**
17
 * Class TxtRecord
18
 *
19
 * @package LTDBeget\dns\configurator\zoneEntities\record
20
 */
21
class TxtRecord extends Record
22
{
23
    /**
24
     * @var String
25
     */
26
    protected $txtData;
27
28
    /**
29
     * TxtRecord constructor.
30
     *
31
     * @param Node   $node
32
     * @param int    $ttl
33
     * @param string $txtData
34
     */
35 1
    public function __construct(Node $node, int $ttl, string $txtData)
36
    {
37 1
        $this->txtData = $this->sanitizeTxtData($txtData);
38 1
        parent::__construct($node, eRecordType::TXT(), $ttl);
39 1
    }
40
41
    /**
42
     * @param string $txtData
43
     * @return string
44
     */
45 1
    private function sanitizeTxtData(string $txtData) : string
46
    {
47 1
        $txtDataArray = explode('\"', $txtData);
48
        $txtDataArray = array_map(function ($value) {
49 1
            return str_replace('"', '\"', $value);
50 1
        }, $txtDataArray);
51
52 1
        return implode('\"', $txtDataArray);
53
    }
54
55
    /**
56
     * @return string
57
     */
58 1
    public function __toString() : string
59
    {
60 1
        $splitted_string      = $this->splitTxtData();
61 1
        $spited_with_quotes = array_map(function ($value) {
62 1
            return "\"$value\"";
63 1
        }, $splitted_string);
64
65 1
        $char_sets          = implode("\n", $spited_with_quotes);
66
67 1
        if (count($spited_with_quotes) > 1) {
68
            $rdataString = " ( \n" . $char_sets . "\n ) \n";
69
        } else {
70 1
            $rdataString = ' ' . $char_sets;
71
        }
72
73 1
        return $this->getMainRecordPart() . $rdataString;
74
    }
75
76
    /**
77
     * @param int $length
78
     * @return array
79 1
     */
80
    private function splitTxtData(int $length = 255) : array
81 1
    {
82
        $splitted_string = str_split($this->getTxtData(), $length);
83
84
        if(count(array_filter($splitted_string, function (string $value) { return $value === '"';}))) {
85
            $splitted_string = $this->splitTxtData(--$length);
86
        }
87
88
        return $splitted_string;
89
    }
90
91
    /**
92
     * @return String
93
     */
94
    public function getTxtData() : string
95
    {
96 1
        return $this->txtData;
97
    }
98 1
99
    /**
100 1
     * @param string $txtData
101
     * @return TxtRecord
102
     */
103
    public function setTxtData(string $txtData) : TxtRecord
104 1
    {
105
        return $this->setAttribute('txtData', $this->sanitizeTxtData($txtData));
106
    }
107
108 1
    /**
109
     * @return bool
110
     */
111
    public function validate() : bool
112
    {
113 1
        $errorStorage = $this->getNode()->getZone()->getErrorsStore();
114
115
        if (strlen($this->getTxtData()) === 0) {
116
            $errorStorage->add(ValidationError::makeRecordError($this, eErrorCode::EMPTY_TXT(), 'txtData'));
117
        }
118
119 1
        if (!ctype_print($this->getTxtData())) {
120
            $errorStorage->add(ValidationError::makeRecordError($this, eErrorCode::CONTAINS_CONTROL_SYMBOLS(), 'txtData'));
121
        }
122 1
123
        if (substr($this->getTxtData(), -1) === '\\' && substr($this->getTxtData(), -2) !== "\\\\") {
124
            $errorStorage->add(ValidationError::makeRecordError($this, eErrorCode::INCORRECT_ESCAPING(), 'txtData'));
125
        }
126
127
        /** @noinspection PhpInternalEntityUsedInspection */
128
        return parent::validate();
129
    }
130
131
    /**
132
     * @return array
133
     */
134
    protected function recordDataToArray() : array
135
    {
136
        return [
137
            'TXTDATA' => $this->getTxtData()
138
        ];
139
    }
140
}