Completed
Pull Request — master (#89)
by Tim
02:19
created

HeaderTrait::hasHeader()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
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
     * Set's the array containing header row.
44
     *
45
     * @param array $headers The array with the header row
46
     *
47
     * @return void
48
     */
49 9
    public function setHeaders(array $headers)
50
    {
51 9
        $this->headers = $headers;
52 9
    }
53
54
    /**
55
     * Return's the array containing header row.
56
     *
57
     * @return array The array with the header row
58
     */
59 2
    public function getHeaders()
60
    {
61 2
        return $this->headers;
62
    }
63
64
    /**
65
     * Queries whether or not the header with the passed name is available.
66
     *
67
     * @param string $name The header name to query
68
     *
69
     * @return boolean TRUE if the header is available, else FALSE
70
     */
71 8
    public function hasHeader($name)
72
    {
73 8
        return isset($this->headers[$name]);
74
    }
75
76
    /**
77
     * Return's the header value for the passed name.
78
     *
79
     * @param string $name The name of the header to return the value for
80
     *
81
     * @return mixed The header value
82
     * @throws \InvalidArgumentException Is thrown, if the header with the passed name is NOT available
83
     */
84 7
    public function getHeader($name)
85
    {
86
87
        // query whether or not, the header is available
88 7
        if (isset($this->headers[$name])) {
89 6
            return $this->headers[$name];
90
        }
91
92
        // throw an exception, if not
93 1
        throw new \InvalidArgumentException(sprintf('Header %s is not available', $name));
94
    }
95
96
    /**
97
     * Add's the header with the passed name and position, if not NULL.
98
     *
99
     * @param string $name The header name to add
100
     *
101
     * @return integer The new headers position
102
     */
103 2
    public function addHeader($name)
104
    {
105
106
        // add the header
107 2
        $this->headers[$name] = $position = sizeof($this->headers);
108
109
        // return the new header's position
110 2
        return $position;
111
    }
112
}
113