Completed
Branch master (b52e58)
by Pierre
03:02 queued 37s
created

AbstractSearch   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 131
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 24
c 0
b 0
f 0
dl 0
loc 131
rs 10
wmc 8

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A setFilter() 0 4 1
A getService() 0 3 1
A setFilename() 0 4 1
A readFromStream() 0 10 2
A setSeparator() 0 4 1
A get() 0 3 1
1
<?php
2
3
namespace App\Model;
4
5
use App\Container;
6
7
/**
8
 * Class App\Model\AbstractSearch
9
 *
10
 * is abstract search class from csv based file
11
 *
12
 */
13
abstract class AbstractSearch
14
{
15
16
    /**
17
     * filename to read from
18
     *
19
     * @var String
20
     */
21
    protected $filename;
22
23
    /**
24
     * stack
25
     *
26
     * @var array
27
     */
28
    protected $datas;
29
30
    /**
31
     * filter
32
     *
33
     * @var String
34
     */
35
    protected $filter;
36
37
    /**
38
     * field separator
39
     *
40
     * @var String
41
     */
42
    protected $separator;
43
44
    /**
45
     * container
46
     *
47
     * @var Container
48
     */
49
    protected $container;
50
51
    /**
52
     * instanciate
53
     *
54
     * @param Container $container
55
     */
56
    public function __construct(Container $container)
57
    {
58
        $this->container = $container;
59
        $this->datas = [];
60
        return $this;
61
    }
62
63
    /**
64
     * return service for serviceName
65
     *
66
     * @param string $serviceName
67
     * @return mixed
68
     */
69
    public function getService(string $serviceName)
70
    {
71
        return $this->container->getService($serviceName);
72
    }
73
74
    /**
75
     * set file name
76
     *
77
     * @param string $filename
78
     * @return AbstractSearch
79
     */
80
    public function setFilename(string $filename): AbstractSearch
81
    {
82
        $this->filename = $filename;
83
        return $this;
84
    }
85
86
    /**
87
     * set regex filter
88
     *
89
     * @param string $filter
90
     * @return AbstractSearch
91
     */
92
    public function setFilter(string $filter): AbstractSearch
93
    {
94
        $this->filter = $filter;
95
        return $this;
96
    }
97
98
    /**
99
     * set field separator
100
     *
101
     * @param string $separator
102
     * @return AbstractSearch
103
     */
104
    public function setSeparator(string $separator): AbstractSearch
105
    {
106
        $this->separator = $separator;
107
        return $this;
108
    }
109
110
    /**
111
     * stack items from streamed asset file
112
     *
113
     * @return AbstractSearch
114
     */
115
    public function readFromStream(): AbstractSearch
116
    {
117
        $stream = new \SplFileObject($this->filename);
118
        $lines = new \RegexIterator($stream, $this->filter);
119
        foreach ($lines as $line) {
120
            $data = explode($this->separator, $line);
121
            $this->setItem($data);
122
        }
123
        unset($lines, $stream);
124
        return $this;
125
    }
126
127
    /**
128
     * returns unfiltered datas
129
     *
130
     * @return array
131
     */
132
    public function get(): array
133
    {
134
        return $this->datas;
135
    }
136
137
    /**
138
     * add route item to stack from dat
139
     *
140
     * @param array $data
141
     * @return AbstractSearch
142
     */
143
    abstract protected function setItem(array $data): AbstractSearch;
144
}
145