Completed
Push — master ( 943281...5c35c6 )
by Vladimir
02:38
created

WhereFilter::compare()   B

Complexity

Conditions 9
Paths 9

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 9

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 9
eloc 13
nc 9
nop 4
dl 0
loc 21
ccs 14
cts 14
cp 1
crap 9
rs 7.041
c 2
b 0
f 0
1
<?php
2
3
namespace allejo\stakx\Twig;
4
5
use Twig_Error_Syntax;
6
7
class WhereFilter
8
{
9
    /**
10
     * @param  array|\ArrayAccess[] $array      The elements to filter through
11
     * @param  string               $key        The key value in an associative array or FrontMatter
12
     * @param  string               $comparison The actual comparison symbols being used
13
     * @param  mixed                $value      The value we're searching for
14
     *
15
     * @return array
16
     */
17 21
    public function __invoke ($array, $key, $comparison, $value)
18
    {
19 21
        $results = array();
20 21
        $this->search_r($array, $key, $comparison, $value, $results);
21
22 20
        return $results;
23
    }
24
25
    /**
26
     * @return \Twig_SimpleFilter
27
     */
28 2
    public static function get ()
29
    {
30 2
        return new \Twig_SimpleFilter('where', new self());
31
    }
32
33
    /**
34
     * Recursive searching calling our comparison
35
     *
36
     * @param array|\ArrayAccess[] $array      The elements to filter through
37
     * @param string               $key        The key value in an associative array or FrontMatter
38
     * @param string               $comparison The actual comparison symbols being used
39
     * @param string               $value      The value we're searching for
40
     * @param array                $results    The reference to where to keep the filtered elements
41
     */
42 21
    private function search_r ($array, $key, $comparison, $value, &$results)
0 ignored issues
show
Coding Style introduced by
This method is not in camel caps format.

This check looks for method names that are not written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection seeker becomes databaseConnectionSeeker.

Loading history...
43
    {
44 21
        if (!is_array($array) && !($array instanceof \ArrayAccess))
45 21
        {
46 19
            return;
47
        }
48
49 21
        if ($this->compare($array, $key, $comparison, $value))
50 21
        {
51 15
            $results[] = $array;
52 15
        }
53
54 21
        foreach ($array as $subarray)
55
        {
56 21
            $this->search_r($subarray, $key, $comparison, $value, $results);
57 20
        }
58 20
    }
59
60
    /**
61
     * The logic for determining if an element matches the filter
62
     *
63
     * @param  array|\ArrayAccess[] $array      The elements to filter through
64
     * @param  string               $key        The key value in an associative array or FrontMatter
65
     * @param  string               $comparison The actual comparison symbols being used
66
     * @param  mixed                $value      The value we're searching for
67
     *
68
     * @return bool
69
     *
70
     * @throws Twig_Error_Syntax
71
     */
72 21
    private function compare ($array, $key, $comparison, $value)
73
    {
74 21
        if (!isset($array[$key]))
75 21
        {
76 21
            return false;
77
        }
78
79
        switch ($comparison)
80
        {
81 20
            case "==": return ($array[$key] === $value);
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
82 14
            case "!=": return ($array[$key] !== $value);
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
83 11
            case ">" : return ($array[$key] > $value);
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
84 9
            case ">=": return ($array[$key] >= $value);
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
85 7
            case "<" : return ($array[$key] < $value);
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
86 5
            case "<=": return ($array[$key] <= $value);
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
87 3
            case "~=": return ($this->contains($array[$key], $value));
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
88
89 1
            default:
90 1
                throw new Twig_Error_Syntax("Invalid where comparison ({$comparison})");
91 1
        }
92
    }
93
94 2
    private function contains ($haystack, $needle)
95
    {
96 2
        return ((is_array($haystack) && in_array($needle, $haystack)) ||
97 2
                (is_string($haystack) && strpos($haystack, $needle) !== false));
98
    }
99
}