WhereAdapter   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 2
dl 0
loc 72
ccs 44
cts 44
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 24 1
D fromArray() 0 27 10
1
<?php
2
3
namespace Minerva\FrontQL\Adapter\Where;
4
5
use Zend\Db\Sql\Where;
6
use Minerva\FrontQL\Adapter\Where\Basis\Exception\InvalidCommandException;
7
8
/**
9
 * Adaptador para comandos Where
10
 *
11
 * @author  Lucas A. de Araújo <[email protected]>
12
 * @package Minerva\FrontQL\Adapter\Where
13
 */
14
class WhereAdapter
15
{
16
    /**
17
     * Lista de comandos permitidos
18
     *
19
     * @var array
20
     */
21
    protected $allowed;
22
23
    /**
24
     * Adapter constructor.
25
     */
26 3
    public function __construct()
27
    {
28
        // Lista de comandos permitidos
29 3
        $this->allowed = [
30 3
            'between',
31 3
            'notBetween',
32 3
            'equalTo',
33 3
            'notEqualTo',
34 3
            'in',
35 3
            'notIn',
36 3
            'like',
37 3
            'notLike',
38 3
            'greaterThan',
39 3
            'greaterThanOrEqualTo',
40 3
            'lesserThan',
41 3
            'lesserThanOrEqualTo',
42 3
            'isNull',
43 3
            'isNotNull',
44 3
            'and',
45 3
            'or',
46 3
            'nest',
47
            'unnest'
48 3
        ];
49 3
    }
50
    
51
    /**
52
     * Converte um Where em JSON para PHP
53
     *
54
     * @param array $jWhere
55
     * @return Where
56
     * @throws InvalidCommandException
57
     */
58 3
    public function fromArray(array $jWhere)
59
    {
60 3
        $where = new Where();
61
62 3
        foreach($jWhere as $item){
63 3
            if(is_array($item)){
64 3
                if(!in_array($item[0], $this->allowed)) {
65 1
                    throw new InvalidCommandException();
66
                }
67 2
                $method = $item[0];
68 2
                unset($item[0]);
69 2
                $where = call_user_func_array([$where, $method], $item);
70 2
            }
71 3
            if(is_string($item)){
72 3
                if(in_array($item, $this->allowed))
73 3
                    if($item == 'and')
74 3
                        $where = $where->and;
75 3
                    else if ($item == 'or')
76 3
                       $where = $where->or;
77 3
                    else if ($item == 'nest')
78 3
                        $where = $where->nest();
79 2
                    else if ($item == 'unnest')
80 2
                        $where = $where->unnest();
81 3
            }
82 3
        }
83 2
        return $where;
84
    }
85
}
86