Completed
Push — master ( 1aef1b...ec0bcd )
by Sébastien
24:45 queued 21:48
created

Search   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 1
dl 0
loc 107
ccs 37
cts 37
cp 1
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A all() 0 4 1
A notIn() 0 11 3
A in() 0 12 3
A regexp() 0 12 3
A findByType() 0 11 3
A findVirtual() 0 11 3
1
<?php
2
3
/*
4
 * soluble-flexstore library
5
 *
6
 * @author    Vanvelthem Sébastien
7
 * @link      https://github.com/belgattitude/soluble-flexstore
8
 * @copyright Copyright (c) 2016-2017 Vanvelthem Sébastien
9
 * @license   MIT License https://github.com/belgattitude/soluble-flexstore/blob/master/LICENSE.md
10
 *
11
 */
12
13
namespace Soluble\FlexStore\Column\ColumnModel;
14
15
use Soluble\FlexStore\Column\ColumnModel\Search\Result;
16
use Soluble\FlexStore\Column\Exception;
17
use ArrayObject;
18
19
class Search
20
{
21
    /**
22
     * @var ArrayObject
23
     */
24
    protected $columns;
25
26 11
    public function __construct(ArrayObject $columns)
27
    {
28 11
        $this->columns = $columns;
29 11
    }
30
31
    /**
32
     * @return Result
33
     */
34 3
    public function all()
35
    {
36 3
        return new Result(array_keys((array) $this->columns), $this->columns);
37
    }
38
39
    /**
40
     * Search ColumnModel for columns that are not in the $columns parameter.
41
     *
42
     * @throws Exception\InvalidArgumentException
43
     *
44
     * @return Result
45
     */
46 1
    public function notIn(array $columns)
47
    {
48 1
        $results = [];
49 1
        foreach ($this->columns as $name => $column) {
50 1
            if (!in_array($name, $columns, true)) {
51 1
                $results[] = $name;
52
            }
53
        }
54
55 1
        return new Result($results, $this->columns);
56
    }
57
58
    /**
59
     * @return Result
60
     */
61 9
    public function in(array $columns)
62
    {
63 9
        $results = [];
64 9
        foreach ($columns as $column) {
65 9
            $column = trim($column);
66 9
            if ($this->columns->offsetExists($column)) {
67 9
                $results[] = $column;
68
            }
69
        }
70
71 9
        return new Result($results, $this->columns);
72
    }
73
74
    /**
75
     * @param string $regexp
76
     *
77
     * @return Result
78
     */
79 4
    public function regexp($regexp)
80
    {
81 4
        $results = [];
82 4
        foreach ($this->columns as $name => $column) {
83 4
            $column = trim($column);
84 4
            if (preg_match($regexp, $name)) {
85 4
                $results[] = $name;
86
            }
87
        }
88
89 4
        return new Result($results, $this->columns);
90
    }
91
92
    /**
93
     * @param string $type
94
     *
95
     * @return Result
96
     */
97 1
    public function findByType($type)
98
    {
99 1
        $results = [];
100 1
        foreach ($this->columns as $name => $column) {
101 1
            if ($column->getType() == $type) {
102 1
                $results[] = $name;
103
            }
104
        }
105
106 1
        return new Result($results, $this->columns);
107
    }
108
109
    /**
110
     * Return virtual column (not materialized by the underlying datasource).
111
     *
112
     * @return Result
113
     */
114 1
    public function findVirtual()
115
    {
116 1
        $results = [];
117 1
        foreach ($this->columns as $name => $column) {
118 1
            if ($column->isVirtual()) {
119 1
                $results[] = $name;
120
            }
121
        }
122
123 1
        return new Result($results, $this->columns);
124
    }
125
}
126