HandlesParameters::parameters()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace BeyondCode\Mailbox\Concerns;
4
5
trait HandlesParameters
6
{
7
    public function parameterNames()
8
    {
9
        preg_match_all('/\{(.*?)\}/', $this->pattern, $matches);
10
11
        return array_map(function ($m) {
12
            return trim($m, '?');
13
        }, $matches[1]);
14
    }
15
16
    public function parameters()
17
    {
18
        return $this->matchToKeys(array_slice($this->matches, 1));
19
    }
20
21
    public function parametersWithoutNulls()
22
    {
23
        return array_filter($this->parameters(), function ($p) {
24
            return ! is_null($p);
25
        });
26
    }
27
28
    protected function matchToKeys(array $matches)
29
    {
30
        if (empty($parameterNames = $this->parameterNames())) {
31
            return [];
32
        }
33
34
        $parameters = array_intersect_key($matches, array_flip($parameterNames));
35
36
        return array_filter($parameters, function ($value) {
37
            return is_string($value) && strlen($value) > 0;
38
        });
39
    }
40
}
41