Completed
Pull Request — master (#178)
by ignace nyamagana
02:51
created

Validator::useInternalConverter()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 3
nc 3
nop 1
1
<?php
2
/**
3
* This file is part of the League.csv library
4
*
5
* @license http://opensource.org/licenses/MIT
6
* @link https://github.com/thephpleague/csv/
7
* @version 9.0.0
8
* @package League.csv
9
*
10
* For the full copyright and license information, please view the LICENSE
11
* file that was distributed with this source code.
12
*/
13
namespace League\Csv\Config;
14
15
use DOMDocument;
16
use DOMElement;
17
use InvalidArgumentException;
18
use League\Csv\AbstractCsv;
19
20
/**
21
 *  A trait to configure and check CSV file and content
22
 *
23
 * @package League.csv
24
 * @since  6.0.0
25
 *
26
 */
27
trait Validator
28
{
29
    /**
30
     * record name for XML conversion
31
     *
32
     * @var string
33
     */
34
    protected $row_name;
35
36
    /**
37
     * Cell name for XML conversion
38
     *
39
     * @var string
40
     */
41
    protected $cell_name;
42
43
    /**
44
     * Convert a row into a DOMElement
45
     *
46
     * @param array       $record Csv record
47
     * @param DOMDocument $doc
48
     *
49
     * @return DOMElement
50
     */
51
    protected function convertRecordToDOMNode(array $record, DOMDocument $doc)
52
    {
53
        $node = $doc->createElement($this->row_name);
54
        foreach ($record as $value) {
55
            $cell = $doc->createElement($this->cell_name);
56
            $cell->appendChild($doc->createTextNode($value));
57
            $node->appendChild($cell);
58
        }
59
60
        return $node;
61
    }
62
63
    /**
64
     * Convert a CSV record to UTF-8
65
     *
66
     * @param array  $record
67
     * @param string $input_encoding
68
     *
69
     * @return array
70
     */
71
    protected function convertRecordToUtf8(array $record, $input_encoding)
72
    {
73
        $convert = function ($value) use ($input_encoding) {
74
            return iconv($input_encoding, 'UTF-8//TRANSLIT', $value);
75
        };
76
77
        return array_map($convert, $record);
78
    }
79
80
    /**
81
     * Validate an integer
82
     *
83
     * @param int    $int
84
     * @param int    $min_value
85
     * @param string $error_message
86
     *
87
     * @throws InvalidArgumentException If the value is invalid
88
     *
89
     * @return int
90
     */
91
    protected function validateInteger($int, $min_value, $error_message)
92
    {
93
        $int = filter_var($int, FILTER_VALIDATE_INT, ['options' => ['min_range' => $min_value]]);
94
        if (false !== $int) {
95
            return $int;
96
        }
97
98
        throw new InvalidArgumentException($error_message);
99
    }
100
101
    /**
102
     * validate a string
103
     *
104
     * @param mixed $str the value to evaluate as a string
105
     *
106
     * @throws InvalidArgumentException if the submitted data can not be converted to string
107
     *
108
     * @return string
109
     */
110
    protected static function validateString($str)
111
    {
112
        if (is_string($str) || (is_object($str) && method_exists($str, '__toString'))) {
113
            return (string) $str;
114
        }
115
        throw new InvalidArgumentException('Expected data must be a string or stringable');
116
    }
117
118
    /**
119
     * Tell whether to use Stream Filter or not to convert the CSV
120
     *
121
     * @return bool
122
     */
123
    protected function useInternalConverter(AbstractCsv $csv)
124
    {
125
        return !('UTF-8' === $csv->getInputEncoding()
126
            || ($csv->isActiveStreamFilter() && STREAM_FILTER_READ === $csv->getStreamFilterMode()));
127
    }
128
129
    /**
130
     * Strip the BOM character from the record
131
     *
132
     * @param array  $record
133
     * @param string $bom
134
     * @param string $enclosure
135
     *
136
     * @return array
137
     */
138
    protected function stripBOM(array $record, $bom, $enclosure)
139
    {
140
        $bom_length = mb_strlen($bom);
141
        if (0 == $bom_length) {
142
            return $record;
143
        }
144
145
        $record[0] = mb_substr($record[0], $bom_length);
146
        if ($record[0][0] === $enclosure && mb_substr($record[0], -1, 1) === $enclosure) {
147
            $record[0] = mb_substr($record[0], 1, -1);
148
        }
149
150
        return $record;
151
    }
152
}
153