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

RowTrait::getValue()   C

Complexity

Conditions 8
Paths 12

Size

Total Lines 29
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 8

Importance

Changes 0
Metric Value
dl 0
loc 29
ccs 11
cts 11
cp 1
rs 5.3846
c 0
b 0
f 0
cc 8
eloc 11
nc 12
nop 3
crap 8
1
<?php
2
3
/**
4
 * TechDivision\Import\RowTrait
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 row 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 RowTrait
33
{
34
35
    /**
36
     * The actual row, that has to be processed.
37
     *
38
     * @var array
39
     */
40
    protected $row = array();
41
42
    /**
43
     * Set's the actual row, that has to be processed.
44
     *
45
     * @param array $row The row
46
     *
47
     * @return void
48
     */
49 11
    protected function setRow(array $row)
50
    {
51 11
        $this->row = $row;
52 11
    }
53
54
    /**
55
     * Return's the actual row.
56
     *
57
     * @return array The actual row
58
     */
59 15
    public function getRow()
60
    {
61 15
        return $this->row;
62
    }
63
64
    /**
65
     * Query whether or not a value for the column with the passed name exists.
66
     *
67
     * @param string $name The column name to query for a valid value
68
     *
69
     * @return boolean TRUE if the value is set, else FALSE
70
     */
71 2
    protected function hasValue($name)
72
    {
73
74
        // query whether or not the header is available
75 2
        if ($this->hasHeader($name)) {
0 ignored issues
show
Bug introduced by
It seems like hasHeader() must be provided by classes using this trait. How about adding it as abstract method to this trait?

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

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). 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.

Loading history...
76
            // load the key for the row
77 1
            $headerValue = $this->getHeader($name);
0 ignored issues
show
Bug introduced by
It seems like getHeader() must be provided by classes using this trait. How about adding it as abstract method to this trait?

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

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). 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.

Loading history...
78
79
            // query whether the rows column has a vaild value
80 1
            return (isset($this->row[$headerValue]) && $this->row[$headerValue] != '');
81
        }
82
83
        // return FALSE if not
84 1
        return false;
85
    }
86
87
    /**
88
     * Set the value in the passed column name.
89
     *
90
     * @param string $name  The column name to set the value for
91
     * @param mixed  $value The value to set
92
     *
93
     * @return void
94
     */
95 4
    protected function setValue($name, $value)
96
    {
97 4
        $this->row[$this->getHeader($name)] = $value;
0 ignored issues
show
Bug introduced by
It seems like getHeader() must be provided by classes using this trait. How about adding it as abstract method to this trait?

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

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). 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.

Loading history...
98 4
    }
99
100
    /**
101
     * Resolve's the value with the passed colum name from the actual row. If a callback will
102
     * be passed, the callback will be invoked with the found value as parameter. If
103
     * the value is NULL or empty, the default value will be returned.
104
     *
105
     * @param string        $name     The name of the column to return the value for
106
     * @param mixed|null    $default  The default value, that has to be returned, if the row's value is empty
107
     * @param callable|null $callback The callback that has to be invoked on the value, e. g. to format it
108
     *
109
     * @return mixed|null The, almost formatted, value
110
     */
111 9
    protected function getValue($name, $default = null, callable $callback = null)
112
    {
113
114
        // initialize the value
115 9
        $value = null;
116
117
        // query whether or not the header is available
118 9
        if ($this->hasHeader($name)) {
0 ignored issues
show
Bug introduced by
It seems like hasHeader() must be provided by classes using this trait. How about adding it as abstract method to this trait?

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

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). 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.

Loading history...
119
            // load the header value
120 8
            $headerValue = $this->getHeader($name);
0 ignored issues
show
Bug introduced by
It seems like getHeader() must be provided by classes using this trait. How about adding it as abstract method to this trait?

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

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). 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.

Loading history...
121
            // query wheter or not, the value with the requested key is available
122 8
            if ((isset($this->row[$headerValue]) && $this->row[$headerValue] != '')) {
123 8
                $value = $this->row[$headerValue];
124
            }
125
        }
126
127
        // query whether or not, a callback has been passed
128 9
        if ($value != null && is_callable($callback)) {
129 1
            $value = call_user_func($callback, $value);
130
        }
131
132
        // query whether or not
133 9
        if ($value == null && $default !== null) {
134 1
            $value = $default;
135
        }
136
137
        // return the value
138 9
        return $value;
139
    }
140
}
141