Passed
Push — master ( 4b6b7b...8885c8 )
by Richard
02:15
created
Maphper/Lib/Entity.php 1 patch
Indentation   +8 added lines, -8 removed lines patch added patch discarded remove patch
@@ -8,7 +8,7 @@  discard block
 block discarded – undo
8 8
 	private $parent;
9 9
 
10 10
 	public function __construct(\Maphper\Maphper $parent, $className = null) {
11
-        $this->parent = $parent;
11
+		$this->parent = $parent;
12 12
 		$this->className = $className;
13 13
 	}
14 14
 
@@ -25,12 +25,12 @@  discard block
 block discarded – undo
25 25
 		return $object;
26 26
 	}
27 27
 
28
-    private function addRelationData($object, $name, $relation, $siblings) {
29
-        if (isset($object->$name) && !($object->$name instanceof \Maphper\Relation) ) {
30
-            //After overwriting the relation, does the parent object ($object) need overwriting as well?
31
-            if ($relation->overwrite($object, $object->$name)) $this->parent[] = $object;
32
-        }
28
+	private function addRelationData($object, $name, $relation, $siblings) {
29
+		if (isset($object->$name) && !($object->$name instanceof \Maphper\Relation) ) {
30
+			//After overwriting the relation, does the parent object ($object) need overwriting as well?
31
+			if ($relation->overwrite($object, $object->$name)) $this->parent[] = $object;
32
+		}
33 33
 
34
-        $object->$name = $relation->getData($object, $siblings);
35
-    }
34
+		$object->$name = $relation->getData($object, $siblings);
35
+	}
36 36
 }
Please login to merge, or discard this patch.
Maphper/Lib/CrudBuilder.php 1 patch
Indentation   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -8,7 +8,7 @@
 block discarded – undo
8 8
 	public function delete($table, $criteria, $args, $limit = null, $offset = null, $order = null) {
9 9
 		$limit = $limit ? ' LIMIT ' . $limit : '';
10 10
 		$offset = $offset ? ' OFFSET ' . $offset : '';
11
-        $order = $order ? ' ORDER BY ' . $order : '';
11
+		$order = $order ? ' ORDER BY ' . $order : '';
12 12
 		return new Query('DELETE FROM ' . $table . ' WHERE ' . ($criteria ?: '1 = 1 ') . $order . $limit . $offset, $args);
13 13
 	}
14 14
 
Please login to merge, or discard this patch.
Maphper/Lib/ArrayFilter.php 1 patch
Indentation   +54 added lines, -54 removed lines patch added patch discarded remove patch
@@ -3,58 +3,58 @@
 block discarded – undo
3 3
 use Maphper\Maphper;
4 4
 
5 5
 class ArrayFilter {
6
-    private $array;
7
-
8
-    public function __construct(array $array) {
9
-        $this->array = $array;
10
-    }
11
-
12
-    public function filter($fields) {
13
-        $filteredArray = array_filter($this->array, $this->getSearchFieldFunction($fields, \Maphper\Maphper::FIND_EXACT | \Maphper\Maphper::FIND_AND));
14
-        // Need to reset indexes
15
-        $filteredArray = array_values($filteredArray);
16
-        return $filteredArray;
17
-    }
18
-
19
-    private function getSearchFieldFunction($fields, $mode) {
20
-        return function ($data) use ($fields, $mode) {
21
-            foreach ($fields as $key => $val) {
22
-                $currentFieldResult = $this->getIfFieldMatches($key, $val, $data, $mode);
23
-
24
-                if (Maphper::FIND_OR & $mode && $currentFieldResult === true) return true;
25
-                else if (Maphper::FIND_OR ^ $mode && $currentFieldResult === false) return false;
26
-            }
27
-            return (bool)(Maphper::FIND_OR ^ $mode);
28
-        };
29
-    }
30
-
31
-    private function getIfFieldMatches($key, $val, $data, $mode) {
32
-        if ($this->shouldRunDeepSearch($key, $val)) {
33
-            return $this->getSearchFieldFunction($val, $key)($data);
34
-        }
35
-        else if (!isset($data->$key)) return false;
36
-        else if ($this->isInArraySearch($mode, $key, $val))
37
-            return in_array($data->$key, $val);
38
-        else
39
-            return $this->processFilter($mode, $val, $data->$key);
40
-    }
41
-
42
-    private function shouldRunDeepSearch($key, $val) {
43
-        return is_numeric($key) && is_array($val);
44
-    }
45
-
46
-    private function isInArraySearch($mode, $key, $val) {
47
-        return !(Maphper::FIND_BETWEEN & $mode) && !is_numeric($key) && is_array($val);
48
-    }
49
-
50
-    private function processFilter($mode, $expected, $actual) {
51
-        if (Maphper::FIND_NOT & $mode) return $expected != $actual;
52
-        else if ((Maphper::FIND_GREATER | Maphper::FIND_EXACT) === $mode) return $expected <= $actual;
53
-        else if ((Maphper::FIND_LESS | Maphper::FIND_EXACT) === $mode) return $expected >= $actual;
54
-        else if (Maphper::FIND_GREATER & $mode) return $expected < $actual;
55
-        else if (Maphper::FIND_LESS & $mode) return $expected > $actual;
56
-        else if (Maphper::FIND_BETWEEN & $mode) return $expected[0] <= $actual && $actual <= $expected[1];
57
-        else if (Maphper::FIND_NOCASE & $mode) return strtolower($expected) == strtolower($actual);
58
-        return $expected == $actual;
59
-    }
6
+	private $array;
7
+
8
+	public function __construct(array $array) {
9
+		$this->array = $array;
10
+	}
11
+
12
+	public function filter($fields) {
13
+		$filteredArray = array_filter($this->array, $this->getSearchFieldFunction($fields, \Maphper\Maphper::FIND_EXACT | \Maphper\Maphper::FIND_AND));
14
+		// Need to reset indexes
15
+		$filteredArray = array_values($filteredArray);
16
+		return $filteredArray;
17
+	}
18
+
19
+	private function getSearchFieldFunction($fields, $mode) {
20
+		return function ($data) use ($fields, $mode) {
21
+			foreach ($fields as $key => $val) {
22
+				$currentFieldResult = $this->getIfFieldMatches($key, $val, $data, $mode);
23
+
24
+				if (Maphper::FIND_OR & $mode && $currentFieldResult === true) return true;
25
+				else if (Maphper::FIND_OR ^ $mode && $currentFieldResult === false) return false;
26
+			}
27
+			return (bool)(Maphper::FIND_OR ^ $mode);
28
+		};
29
+	}
30
+
31
+	private function getIfFieldMatches($key, $val, $data, $mode) {
32
+		if ($this->shouldRunDeepSearch($key, $val)) {
33
+			return $this->getSearchFieldFunction($val, $key)($data);
34
+		}
35
+		else if (!isset($data->$key)) return false;
36
+		else if ($this->isInArraySearch($mode, $key, $val))
37
+			return in_array($data->$key, $val);
38
+		else
39
+			return $this->processFilter($mode, $val, $data->$key);
40
+	}
41
+
42
+	private function shouldRunDeepSearch($key, $val) {
43
+		return is_numeric($key) && is_array($val);
44
+	}
45
+
46
+	private function isInArraySearch($mode, $key, $val) {
47
+		return !(Maphper::FIND_BETWEEN & $mode) && !is_numeric($key) && is_array($val);
48
+	}
49
+
50
+	private function processFilter($mode, $expected, $actual) {
51
+		if (Maphper::FIND_NOT & $mode) return $expected != $actual;
52
+		else if ((Maphper::FIND_GREATER | Maphper::FIND_EXACT) === $mode) return $expected <= $actual;
53
+		else if ((Maphper::FIND_LESS | Maphper::FIND_EXACT) === $mode) return $expected >= $actual;
54
+		else if (Maphper::FIND_GREATER & $mode) return $expected < $actual;
55
+		else if (Maphper::FIND_LESS & $mode) return $expected > $actual;
56
+		else if (Maphper::FIND_BETWEEN & $mode) return $expected[0] <= $actual && $actual <= $expected[1];
57
+		else if (Maphper::FIND_NOCASE & $mode) return strtolower($expected) == strtolower($actual);
58
+		return $expected == $actual;
59
+	}
60 60
 }
Please login to merge, or discard this patch.
Maphper/Lib/Sql/NullConditional.php 1 patch
Indentation   +9 added lines, -9 removed lines patch added patch discarded remove patch
@@ -2,15 +2,15 @@
 block discarded – undo
2 2
 namespace Maphper\Lib\Sql;
3 3
 
4 4
 class NullConditional implements WhereConditional {
5
-    public function matches($key, $value, $mode) {
6
-        return $value === NULL;
7
-    }
5
+	public function matches($key, $value, $mode) {
6
+		return $value === NULL;
7
+	}
8 8
 
9
-    public function getSql($key, $value, $mode) {
10
-        $nullSql = $key . ' IS ';
11
-        if (\Maphper\Maphper::FIND_NOT & $mode) $nullSql .= 'NOT ';
12
-        $sql = [$nullSql . 'NULL'];
9
+	public function getSql($key, $value, $mode) {
10
+		$nullSql = $key . ' IS ';
11
+		if (\Maphper\Maphper::FIND_NOT & $mode) $nullSql .= 'NOT ';
12
+		$sql = [$nullSql . 'NULL'];
13 13
 
14
-        return ['args' => [], 'sql' => $sql];
15
-    }
14
+		return ['args' => [], 'sql' => $sql];
15
+	}
16 16
 }
Please login to merge, or discard this patch.
Maphper/Lib/Sql/WhereConditional.php 1 patch
Indentation   +2 added lines, -2 removed lines patch added patch discarded remove patch
@@ -2,6 +2,6 @@
 block discarded – undo
2 2
 namespace Maphper\Lib\Sql;
3 3
 
4 4
 interface WhereConditional {
5
-    public function matches($key, $value, $mode);
6
-    public function getSql($key, $value, $mode);
5
+	public function matches($key, $value, $mode);
6
+	public function getSql($key, $value, $mode);
7 7
 }
Please login to merge, or discard this patch.
Maphper/Lib/Sql/Between.php 1 patch
Indentation   +15 added lines, -15 removed lines patch added patch discarded remove patch
@@ -2,20 +2,20 @@
 block discarded – undo
2 2
 namespace Maphper\Lib\Sql;
3 3
 
4 4
 class Between implements WhereConditional {
5
-    public function matches($key, $value, $mode) {
6
-        return is_array($value) && \Maphper\Maphper::FIND_BETWEEN & $mode;
7
-    }
5
+	public function matches($key, $value, $mode) {
6
+		return is_array($value) && \Maphper\Maphper::FIND_BETWEEN & $mode;
7
+	}
8 8
 
9
-    public function getSql($key, $value, $mode) {
10
-        return [
11
-            'sql' => [
12
-                $key . '>= :' . $key . 'from',
13
-                $key . ' <= :' . $key . 'to'
14
-            ],
15
-            'args' => [
16
-                $key . 'from' => $value[0],
17
-                $key . 'to' => $value[1]
18
-            ]
19
-        ];
20
-    }
9
+	public function getSql($key, $value, $mode) {
10
+		return [
11
+			'sql' => [
12
+				$key . '>= :' . $key . 'from',
13
+				$key . ' <= :' . $key . 'to'
14
+			],
15
+			'args' => [
16
+				$key . 'from' => $value[0],
17
+				$key . 'to' => $value[1]
18
+			]
19
+		];
20
+	}
21 21
 }
Please login to merge, or discard this patch.
Maphper/Lib/Sql/Like.php 1 patch
Indentation   +15 added lines, -15 removed lines patch added patch discarded remove patch
@@ -3,21 +3,21 @@
 block discarded – undo
3 3
 use Maphper\Maphper;
4 4
 
5 5
 class Like implements WhereConditional {
6
-    public function matches($key, $value, $mode) {
7
-        return (Maphper::FIND_LIKE | Maphper::FIND_STARTS |
8
-                Maphper::FIND_ENDS | Maphper::FIND_NOCASE) & $mode;
9
-    }
6
+	public function matches($key, $value, $mode) {
7
+		return (Maphper::FIND_LIKE | Maphper::FIND_STARTS |
8
+				Maphper::FIND_ENDS | Maphper::FIND_NOCASE) & $mode;
9
+	}
10 10
 
11
-    public function getSql($key, $value, $mode) {
12
-        return [
13
-            'sql' => [$key . ' LIKE :' . $key],
14
-            'args' => [$key => $this->getValue($value, $mode)]
15
-        ];
16
-    }
11
+	public function getSql($key, $value, $mode) {
12
+		return [
13
+			'sql' => [$key . ' LIKE :' . $key],
14
+			'args' => [$key => $this->getValue($value, $mode)]
15
+		];
16
+	}
17 17
 
18
-    private function getValue($value, $mode) {
19
-        if ((Maphper::FIND_LIKE | Maphper::FIND_ENDS) & $mode) $value = '%' . $value;
20
-        if ((Maphper::FIND_LIKE | Maphper::FIND_STARTS) & $mode) $value .= '%';
21
-        return $value;
22
-    }
18
+	private function getValue($value, $mode) {
19
+		if ((Maphper::FIND_LIKE | Maphper::FIND_ENDS) & $mode) $value = '%' . $value;
20
+		if ((Maphper::FIND_LIKE | Maphper::FIND_STARTS) & $mode) $value .= '%';
21
+		return $value;
22
+	}
23 23
 }
Please login to merge, or discard this patch.
Maphper/Lib/Sql/GeneralOperator.php 1 patch
Indentation   +22 added lines, -22 removed lines patch added patch discarded remove patch
@@ -3,33 +3,33 @@
 block discarded – undo
3 3
 use Maphper\Maphper;
4 4
 
5 5
 class GeneralOperator implements WhereConditional {
6
-    public function matches($key, $value, $mode) {
7
-        return (Maphper::FIND_BIT ^ Maphper::FIND_GREATER ^ Maphper::FIND_LESS ^ Maphper::FIND_NOT & $mode)
8
-                || Maphper::FIND_EXACT & $mode;
9
-    }
6
+	public function matches($key, $value, $mode) {
7
+		return (Maphper::FIND_BIT ^ Maphper::FIND_GREATER ^ Maphper::FIND_LESS ^ Maphper::FIND_NOT & $mode)
8
+				|| Maphper::FIND_EXACT & $mode;
9
+	}
10 10
 
11
-    public function getSql($key, $value, $mode) {
12
-        return [
13
-            'sql' => [$key . ' ' . $this->getOperator($mode) . ' :' . $key],
14
-            'args' => [$key => $value]
15
-        ];
16
-    }
11
+	public function getSql($key, $value, $mode) {
12
+		return [
13
+			'sql' => [$key . ' ' . $this->getOperator($mode) . ' :' . $key],
14
+			'args' => [$key => $value]
15
+		];
16
+	}
17 17
 
18
-    private function getOperator($mode) {
19
-        if (\Maphper\Maphper::FIND_BIT & $mode) return '&';
20
-        else if (\Maphper\Maphper::FIND_NOT & $mode) return '!=';
18
+	private function getOperator($mode) {
19
+		if (\Maphper\Maphper::FIND_BIT & $mode) return '&';
20
+		else if (\Maphper\Maphper::FIND_NOT & $mode) return '!=';
21 21
 
22
-        return $this->getEqualsOperators($mode);
23
-    }
22
+		return $this->getEqualsOperators($mode);
23
+	}
24 24
 
25
-    private function getEqualsOperators($mode) {
26
-        $operator = "";
25
+	private function getEqualsOperators($mode) {
26
+		$operator = "";
27 27
 
28
-        if (\Maphper\Maphper::FIND_GREATER & $mode) $operator = '>';
29
-        else if (\Maphper\Maphper::FIND_LESS & $mode) $operator = '<';
28
+		if (\Maphper\Maphper::FIND_GREATER & $mode) $operator = '>';
29
+		else if (\Maphper\Maphper::FIND_LESS & $mode) $operator = '<';
30 30
 
31
-        if (\Maphper\Maphper::FIND_EXACT & $mode) $operator .= '=';
31
+		if (\Maphper\Maphper::FIND_EXACT & $mode) $operator .= '=';
32 32
 
33
-        return $operator;
34
-    }
33
+		return $operator;
34
+	}
35 35
 }
Please login to merge, or discard this patch.
Maphper/Lib/Sql/WhereBuilder.php 1 patch
Indentation   +62 added lines, -62 removed lines patch added patch discarded remove patch
@@ -2,87 +2,87 @@
 block discarded – undo
2 2
 namespace Maphper\Lib\Sql;
3 3
 
4 4
 class WhereBuilder {
5
-    private $conditionals = [];
6
-
7
-    public function __construct() {
8
-        $defaultConditionals = [
9
-            'Maphper\Lib\Sql\Between',
10
-            'Maphper\Lib\Sql\In',
11
-            'Maphper\Lib\Sql\NullConditional',
12
-            'Maphper\Lib\Sql\Like',
13
-            'Maphper\Lib\Sql\GeneralOperator'
14
-        ];
15
-
16
-        foreach ($defaultConditionals as $conditional) $this->addConditional(new $conditional);
17
-    }
5
+	private $conditionals = [];
6
+
7
+	public function __construct() {
8
+		$defaultConditionals = [
9
+			'Maphper\Lib\Sql\Between',
10
+			'Maphper\Lib\Sql\In',
11
+			'Maphper\Lib\Sql\NullConditional',
12
+			'Maphper\Lib\Sql\Like',
13
+			'Maphper\Lib\Sql\GeneralOperator'
14
+		];
15
+
16
+		foreach ($defaultConditionals as $conditional) $this->addConditional(new $conditional);
17
+	}
18 18
 
19
-    public function addConditional(WhereConditional $conditional) {
20
-        $this->conditionals[] = $conditional;
21
-    }
19
+	public function addConditional(WhereConditional $conditional) {
20
+		$this->conditionals[] = $conditional;
21
+	}
22 22
 
23 23
 	public function createSql($fields, $mode = \Maphper\Maphper::FIND_EXACT | \Maphper\Maphper::FIND_AND) {
24 24
 		$args = [];
25 25
 		$sql = [];
26 26
 
27
-        foreach ($fields as $key => $value) {
28
-            $value = $this->convertDates($value);
27
+		foreach ($fields as $key => $value) {
28
+			$value = $this->convertDates($value);
29 29
 
30
-            if (is_object($value)) continue;
30
+			if (is_object($value)) continue;
31 31
 			$result = $this->getResult($key, $value, $mode);
32
-            $result = $this->fixDuplicateArgs($args, $result);
33
-            $sql = array_merge($sql, (array)$result['sql']);
34
-            $args = array_merge($args, $result['args']);
35
-        }
32
+			$result = $this->fixDuplicateArgs($args, $result);
33
+			$sql = array_merge($sql, (array)$result['sql']);
34
+			$args = array_merge($args, $result['args']);
35
+		}
36 36
 
37 37
 		return ['args' => $args, 'sql' => $this->sqlArrayToString($sql, $mode)];
38 38
 	}
39 39
 
40
-    // Returns result with duplicate issues removed
41
-    private function fixDuplicateArgs($origArgs, $result) {
42
-        $duplicates = array_intersect_key($result['args'], $origArgs); // Holds all keys in results already in the args
43
-        if (count($duplicates) === 0) return $result;
40
+	// Returns result with duplicate issues removed
41
+	private function fixDuplicateArgs($origArgs, $result) {
42
+		$duplicates = array_intersect_key($result['args'], $origArgs); // Holds all keys in results already in the args
43
+		if (count($duplicates) === 0) return $result;
44 44
 
45
-        foreach ($duplicates as $argKey => $argVal) {
46
-            $valHash = substr(md5($argVal), 0, 5);
47
-            $newKey = $argKey . $valHash;
45
+		foreach ($duplicates as $argKey => $argVal) {
46
+			$valHash = substr(md5($argVal), 0, 5);
47
+			$newKey = $argKey . $valHash;
48 48
 
49
-            // Replace occurences of duplicate key with key + hash as arg
50
-            $result['sql'] = str_replace(':' . $argKey, ':' . $newKey, $result['sql']);
51
-            unset($result['args'][$argKey]);
52
-            $result['args'][$newKey] = $argVal;
53
-        }
49
+			// Replace occurences of duplicate key with key + hash as arg
50
+			$result['sql'] = str_replace(':' . $argKey, ':' . $newKey, $result['sql']);
51
+			unset($result['args'][$argKey]);
52
+			$result['args'][$newKey] = $argVal;
53
+		}
54 54
 
55
-        return $result;
56
-    }
55
+		return $result;
56
+	}
57 57
 
58
-    /*
58
+	/*
59 59
      * Either get sql from a conditional or call createSql again because the mode needs to be changed
60 60
      */
61
-    private function getResult($key, $value, $mode) {
62
-        if (is_numeric($key) && is_array($value)) return $this->createSql($value, $key);
63
-        return $this->getConditional($key, $value, $mode);
64
-    }
61
+	private function getResult($key, $value, $mode) {
62
+		if (is_numeric($key) && is_array($value)) return $this->createSql($value, $key);
63
+		return $this->getConditional($key, $value, $mode);
64
+	}
65 65
 
66
-    private function sqlArrayToString($sql, $mode) {
67
-        if (\Maphper\Maphper::FIND_OR & $mode) $query = implode(' OR  ', $sql);
66
+	private function sqlArrayToString($sql, $mode) {
67
+		if (\Maphper\Maphper::FIND_OR & $mode) $query = implode(' OR  ', $sql);
68 68
 		else $query = implode(' AND ', $sql);
69 69
 		if (!empty($query)) $query = '(' . $query . ')';
70
-        return $query;
71
-    }
72
-
73
-    private function getConditional($key, $value, $mode) {
74
-        foreach ($this->conditionals as $conditional) {
75
-            if ($conditional->matches($key, $value, $mode))
76
-                return $conditional->getSql($key, $value, $mode);
77
-        }
78
-        throw new \Exception("Invalid WHERE query");
79
-    }
80
-
81
-    private function convertDates($value) {
82
-        if ($value instanceof \DateTimeInterface) {
83
-            if ($value->format('H:i:s')  == '00:00:00') $value = $value->format('Y-m-d');
84
-            else $value = $value->format('Y-m-d H:i:s');
85
-        }
86
-        return $value;
87
-    }
70
+		return $query;
71
+	}
72
+
73
+	private function getConditional($key, $value, $mode) {
74
+		foreach ($this->conditionals as $conditional) {
75
+			if ($conditional->matches($key, $value, $mode))
76
+				return $conditional->getSql($key, $value, $mode);
77
+		}
78
+		throw new \Exception("Invalid WHERE query");
79
+	}
80
+
81
+	private function convertDates($value) {
82
+		if ($value instanceof \DateTimeInterface) {
83
+			if ($value->format('H:i:s')  == '00:00:00') $value = $value->format('Y-m-d');
84
+			else $value = $value->format('Y-m-d H:i:s');
85
+		}
86
+		return $value;
87
+	}
88 88
 }
Please login to merge, or discard this patch.