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

Filters::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
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