Completed
Push — 15.x ( b45baa...4ca9b4 )
by Tim
02:13
created

HeaderTrait   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 124
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 0
dl 0
loc 124
ccs 22
cts 22
cp 1
rs 10
c 0
b 0
f 0

7 Methods

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