|
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 InvalidArgumentException; |
|
16
|
|
|
use LimitIterator; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Trait to configure the CSV header |
|
20
|
|
|
* |
|
21
|
|
|
* @package League.csv |
|
22
|
|
|
* @since 9.0.0 |
|
23
|
|
|
* |
|
24
|
|
|
*/ |
|
25
|
|
|
trait Header |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* Csv Header Info |
|
29
|
|
|
* |
|
30
|
|
|
* @var array|int |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $header = []; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Tell whether the current header is internal |
|
36
|
|
|
* or user submitted |
|
37
|
|
|
* |
|
38
|
|
|
* @return int|null |
|
39
|
|
|
*/ |
|
40
|
|
|
public function getHeaderOffset() |
|
41
|
|
|
{ |
|
42
|
|
|
if (is_array($this->header)) { |
|
43
|
|
|
return null; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
return $this->header; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Returns the CSV header |
|
51
|
|
|
* |
|
52
|
|
|
* @return array |
|
53
|
|
|
*/ |
|
54
|
|
|
public function getHeader() |
|
55
|
|
|
{ |
|
56
|
|
|
if (is_array($this->header)) { |
|
57
|
|
|
return $this->header; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
return $this->getHeaderFromDocument(); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Get the Header from a CSV record |
|
65
|
|
|
* |
|
66
|
|
|
* @return array |
|
67
|
|
|
*/ |
|
68
|
|
|
protected function getHeaderFromDocument() |
|
69
|
|
|
{ |
|
70
|
|
|
$iterator = new LimitIterator($this->getIterator(), $this->header); |
|
|
|
|
|
|
71
|
|
|
$iterator->rewind(); |
|
72
|
|
|
$header = $iterator->current(); |
|
73
|
|
|
if ($iterator->key() !== $this->header) { |
|
74
|
|
|
throw new InvalidArgumentException('the select offset does not exist'); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
if (0 !== $this->header) { |
|
78
|
|
|
return $header; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
return $this->formatHeader($header, $this->getInputBOM(), $this->getEnclosure()); |
|
|
|
|
|
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Format the Document Header |
|
86
|
|
|
* |
|
87
|
|
|
* @param string[] $header |
|
88
|
|
|
* @param string $bom |
|
89
|
|
|
* @param string $enclosure |
|
90
|
|
|
* |
|
91
|
|
|
* @return string[] |
|
92
|
|
|
*/ |
|
93
|
|
|
protected function formatHeader(array $header, $bom, $enclosure) |
|
94
|
|
|
{ |
|
95
|
|
|
$header = $this->stripBOM($header, $bom, $enclosure); |
|
|
|
|
|
|
96
|
|
|
if (!$this->useInternalConverter($this)) { |
|
|
|
|
|
|
97
|
|
|
return $header; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
return $this->convertRecordToUtf8($header, $this->getInputEncoding()); |
|
|
|
|
|
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Selects the array to be used as key for the fetchAssoc method |
|
105
|
|
|
* |
|
106
|
|
|
* @param int|null|array $offset_or_keys the assoc key OR the row Index to be used |
|
107
|
|
|
* as the key index |
|
108
|
|
|
* |
|
109
|
|
|
* @return $this |
|
110
|
|
|
*/ |
|
111
|
|
|
public function setHeader($offset_or_keys) |
|
112
|
|
|
{ |
|
113
|
|
|
if (is_array($offset_or_keys)) { |
|
114
|
|
|
$this->header = $this->validateHeader($offset_or_keys); |
|
|
|
|
|
|
115
|
|
|
return $this; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
if (null === $offset_or_keys) { |
|
119
|
|
|
$this->header = []; |
|
120
|
|
|
return $this; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
$this->header = $this->validateInteger($offset_or_keys, 0, 'the header offset is invalid'); |
|
|
|
|
|
|
124
|
|
|
return $this; |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idableprovides a methodequalsIdthat in turn relies on the methodgetId(). If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()as an abstract method to the trait will make sure it is available.