Completed
Push — master ( 767c34...79d01e )
by Todd
02:06
created

ArraySet::setData()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 7
cts 7
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 4
nop 1
crap 3
1
<?php
2
/**
3
 * @author Todd Burry <[email protected]>
4
 * @copyright 2009-2017 Vanilla Forums Inc.
5
 * @license MIT
6
 */
7
8
namespace Garden\Db;
9
10
11
class ArraySet implements  \IteratorAggregate, DatasetInterface {
0 ignored issues
show
Coding Style introduced by
Expected 1 space before "IteratorAggregate"; 2 found
Loading history...
12
    use DatasetTrait;
13
14
    private $data;
15
16
    private $toSort = false;
17
18
    /**
19
     * Construct a new {@link ArraySet} object.
20
     *
21
     * @param array|\Traversable $data The initial data in the
22
     */
23 4
    public function __construct($data = []) {
24 4
        $this->setData($data);
25 4
    }
26
27 2
    protected function sortData() {
28 2
        $columns = $this->getOrder();
29 2
        $order = [];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
30 2
        foreach ($columns as $column) {
31 2
            if ($column[0] === '-') {
32 2
                $column = substr($column, 1);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 9 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
33 2
                $order[$column] = -1;
34
            } else {
35 2
                $order[$column] = 1;
36
            }
37
        }
38
39 2
        $cmp = function ($a, $b) use ($order) {
40 2
            foreach ($order as $column => $desc) {
41 2
                $r = strnatcmp($a[$column], $b[$column]);
42
43 2
                if ($r !== 0) {
44 2
                    return $r * $desc;
45
                }
46
            }
47
            return 0;
48 2
        };
49
50 2
        usort($this->data, $cmp);
51 2
    }
52
53
    /**
54
     * Get the underlying data array.
55
     *
56
     * @return array Returns the data.
57
     */
58 3
    public function getData() {
59 3
        if ($this->toSort && !empty($this->getOrder())) {
60 2
            $this->sortData();
61
        }
62
63 3
        if ($this->getLimit()) {
64 1
            return array_slice($this->data, $this->getOffset(), $this->getLimit());
65
        } else {
66 2
            return $this->data;
67
        }
68
    }
69
70
    /**
71
     * Set the underlying data array.
72
     *
73
     * @param array|\Traversable $data
74
     * @return $this
75
     */
76 4
    public function setData($data) {
77 4
        if ($data instanceof \Traversable) {
78 4
            $this->data = iterator_to_array($data);
79
        } else {
80 1
            $this->data = $data;
81
        }
82
83 4
        if (!empty($this->getOrder())) {
84 1
            $this->toSort = true;
85
        }
86
87 4
        return $this;
88
    }
89
90
    /**
91
     * {@inheritdoc}
92
     */
93 2
    public function setOrder(...$columns) {
94 2
        if ($columns !== $this->order) {
95 2
            $this->toSort = true;
96
        }
97
98 2
        $this->order = $columns;
99 2
        return $this;
100
    }
101
102
    /**
103
     * {@inheritdoc}
104
     */
105 1
    public function count() {
106 1
        $count = count($this->data);
1 ignored issue
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
107 1
        $limit = $this->getLimit();
1 ignored issue
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
108 1
        $offset = $this->getOffset();
109
110 1
        $count = $count - $offset;
111 1
        if ($limit > 0) {
112 1
            $count = min($count, $limit);
113
        }
114 1
        $count = max($count, 0);
115
116 1
        return $count;
117
    }
118
}
119