GrepFunctions::getPatternByType()
last analyzed

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 1
ccs 0
cts 0
cp 0
c 0
b 0
f 0
nc 1
1
<?php
2
namespace Mcustiel\PhpSimpleRegex\PregFunctions;
3
4
trait GrepFunctions
5
{
6
    /**
7
     * Return all strings in the array that match the given regex.
8
     *
9
     * @param string|array|\VerbalExpressions\PHPVerbalExpressions\VerbalExpressions|SelvinOrtiz\Utils\Flux\Flux|MarkWilson\VerbalExpression
10
     *          $pattern
11
     * @param array
12
     *          $input
13
     *
14
     * @throws \InvalidArgumentException
15
     * @return array
16
     */
17 2
    public function grep($pattern, $input)
18
    {
19 2
        $result = @preg_grep($this->getPatternByType($pattern), $input);
20 2
        $this->checkResultIsOkOrThrowException($result, $pattern);
21
22 1
        return $result;
23
    }
24
25
    /**
26
     * Return all strings in the array that do not match the given regex.
27
     *
28
     * @param string|array|\VerbalExpressions\PHPVerbalExpressions\VerbalExpressions|SelvinOrtiz\Utils\Flux\Flux|MarkWilson\VerbalExpression
29
     *          $pattern
30
     * @param array
31
     *          $input
32
     *
33
     * @throws \InvalidArgumentException
34
     * @return array
35
     */
36 2
    public function grepNotMatching($pattern, $input)
37
    {
38 2
        $result = @preg_grep($this->getPatternByType($pattern), $input, PREG_GREP_INVERT);
39 2
        $this->checkResultIsOkOrThrowException($result, $pattern);
40
41 1
        return $result;
42
    }
43
44
    /**
45
     * @param mixed $pattern
46
     * @throws \InvalidArgumentException
47
     * @return string
48
     */
49
    abstract protected function getPatternByType($pattern);
50
51
    /**
52
     * @param mixed  $result
53
     * @param string $pattern
54
     *
55
     * @throws \RuntimeException
56
     */
57
    abstract protected function checkResultIsOkOrThrowException($result, $pattern);
58
}
59