-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathHeaderTrait.php
More file actions
147 lines (127 loc) · 3.67 KB
/
Copy pathHeaderTrait.php
File metadata and controls
147 lines (127 loc) · 3.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php
/**
* TechDivision\Import\HeaderTrait
*
* PHP version 7
*
* @author Tim Wagner <t.wagner@techdivision.com>
* @copyright 2016 TechDivision GmbH <info@techdivision.com>
* @license https://opensource.org/licenses/MIT
* @link https://github.com/techdivision/import
* @link http://www.techdivision.com
*/
namespace TechDivision\Import;
/**
* A trait that provides header handling.
*
* @author Tim Wagner <t.wagner@techdivision.com>
* @copyright 2016 TechDivision GmbH <info@techdivision.com>
* @license https://opensource.org/licenses/MIT
* @link https://github.com/techdivision/import
* @link http://www.techdivision.com
*/
trait HeaderTrait
{
/**
* Contain's the column names from the header line.
*
* @var array
*/
protected $headers = array();
/**
* Mappings for attribute code => CSV column header.
*
* @var array
*/
protected $headerMappings = array();
/**
* Set's the array containing header row.
*
* @param array $headers The array with the header row
*
* @return void
*/
public function setHeaders(array $headers)
{
$this->headers = $headers;
}
/**
* Return's the array containing header row.
*
* @return array The array with the header row
*/
public function getHeaders()
{
return $this->headers;
}
/**
* Return's the header mappings for the actual entity.
*
* @return array The header mappings
*/
public function getHeaderMappings()
{
return $this->headerMappings;
}
/**
* Queries whether or not the header with the passed name is available.
*
* @param string $name The header name to query
*
* @return boolean TRUE if the header is available, else FALSE
*/
public function hasHeader($name)
{
return isset($this->headers[$this->mapAttributeCodeByHeaderMapping($name)]);
}
/**
* Return's the header value for the passed name.
*
* @param string $name The name of the header to return the value for
*
* @return mixed The header value
* @throws \InvalidArgumentException Is thrown, if the header with the passed name is NOT available
*/
public function getHeader($name)
{
// map column => attribute name
$name = $this->mapAttributeCodeByHeaderMapping($name);
// query whether or not, the header is available
if (isset($this->headers[$name])) {
return $this->headers[$name];
}
// throw an exception, if not
throw new \InvalidArgumentException(sprintf('Header %s is not available', $name));
}
/**
* Add's the header with the passed name and position, if not NULL.
*
* @param string $name The header name to add
*
* @return integer The new headers position
*/
public function addHeader($name)
{
// add the header
$this->headers[$name] = $position = sizeof($this->headers);
// return the new header's position
return $position;
}
/**
* Map the passed attribute code, if a header mapping exists and return the
* mapped mapping.
*
* @param string $attributeCode The attribute code to map
*
* @return string The mapped attribute code, or the original one
*/
public function mapAttributeCodeByHeaderMapping($attributeCode)
{
$headerMappings = $this->getHeaderMappings();
$attributeCode = $attributeCode ?? '';
if (isset($headerMappings[$attributeCode])) {
$attributeCode = $headerMappings[$attributeCode];
}
return $attributeCode;
}
}