Completed
Pull Request — 2.x (#43)
by jake
02:49 queued 50s
created

AbstractSeries::addElement()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 2
crap 1
1
<?php
2
/**
3
 *
4
 * This file is part of Aura for PHP.
5
 *
6
 * @license http://opensource.org/licenses/bsd-license.php BSD
7
 *
8
 */
9
namespace Aura\Html\Helper;
10
11
/**
12
 *
13
 * Abstract helper for an element series with positional ordering.
14
 *
15
 * @package Aura.Html
16
 *
17
 */
18
abstract class AbstractSeries extends AbstractHelper
19
{
20
    /**
21
     *
22
     * The array of all elements in the series, by position.
23
     *
24
     * @var array
25
     *
26
     */
27
    protected $elements = array();
28
29
    /**
30
     *
31
     * Returns the helper so you can call methods on it.
32
     *
33
     * If you pass arguments to __invoke(), it will call `$this->add()` with
34
     * those arguments.
35
     *
36
     * @return self
37
     *
38
     */
39
    public function __invoke()
40
    {
41
        $args = func_get_args();
42
        if ($args) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $args of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
43
            call_user_func_array(array($this, 'add'), $args);
44
        }
45
        return $this;
46
    }
47
48
    /**
49
     *
50
     * Returns the elements in order as a single string and resets the elements.
51
     *
52
     * @return string The elements as a string.
53
     *
54
     */
55 14
    public function __toString()
56
    {
57 14
        $html = '';
58
        ksort($this->elements);
59 14
        foreach ($this->elements as $pos => $elements) {
60
            foreach ($elements as $element) {
61 4
                $html .= $this->indent . $element . PHP_EOL;
62
            }
63
        }
64 14
        $this->elements = array();
65 14
        return $html;
66
    }
67
68
    /**
69
     *
70
     * Adds an element at a certain position.
71
     *
72
     * @param int $pos The element position.
73
     *
74
     * @param string $element The element itself.
75
     *
76
     * @return null
77
     *
78
     */
79 4
    protected function addElement($pos, $element)
80
    {
81 4
        $this->elements[(int) $pos][] = $element;
82 4
    }
83
}
84