Completed
Pull Request — master (#208)
by
unknown
01:47
created

Filters::checkFilterSyntax()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 1
1
<?php
2
3
namespace Spatie\Analytics;
4
5
use Spatie\Analytics\Exceptions\InvalidFilter;
6
7
class Filters
8
{
9
    private $filters;
10
11
    public static function create($filters = []): Filters
12
    {
13
        return new static($filters);
14
    }
15
16
    public function __construct($filters = [])
17
    {
18
        foreach ($filters as $filter) {
19
            $this->checkFilterSyntax($filter);
20
        }
21
22
        $this->filters = $filters;
23
    }
24
25
    public function addFilter($filter)
26
    {
27
        $this->checkFilterSyntax($filter);
28
        $this->filters[] = $filter;
29
    }
30
31
    private function checkFilterSyntax($filter)
32
    {
33
        if (strpos($filter, 'ga:') !== 0) {
34
            throw InvalidFilter::filterMustContainGA($filter);
35
        }
36
    }
37
38
    public function __toString()
39
    {
40
        return implode(';', $this->filters);
41
    }
42
}
43