Completed
Push — develop ( fbffd5...4a9769 )
by Michael
02:27
created
src/AbstractActiveRecordSearch.php 2 patches
Indentation   +101 added lines, -101 removed lines patch added patch discarded remove patch
@@ -17,105 +17,105 @@
 block discarded – undo
17 17
  */
18 18
 abstract class AbstractActiveRecordSearch extends AbstractActiveRecord
19 19
 {
20
-	/**
21
-	 * {@inheritdoc}
22
-	 */
23
-	public function search($where = [], $orderBy = [], $limit = -1, $offset = 0)
24
-	{
25
-		try {
26
-			$pdoStatement = $this->getPdo()->prepare($this->getSearchQuery($where, $orderBy, $limit, $offset));
27
-			array_walk_recursive($where, function(&$value) use ($pdoStatement) {
28
-				static $index = 1;
29
-
30
-				$pdoStatement->bindParam($index++, $value);
31
-			});
32
-
33
-			$pdoStatement->execute();
34
-			$result = [];
35
-
36
-			while ($fetch = $pdoStatement->fetch()) {
37
-				$new = new static($this->getPdo());
38
-
39
-				$new->setId(intval($fetch['id']));
40
-				$new->setActiveRecordData($fetch);
41
-
42
-				$result[] = $new;
43
-			}
44
-
45
-			return $result;
46
-		} catch (\PDOException $e) {
47
-			throw new ActiveRecordException(sprintf('Can not search the record in the `%s` table.', $this->getActiveRecordName()), 0, $e);
48
-		}
49
-	}
50
-
51
-	/**
52
-	 * Returns the search query with the given where, order by, limit and offset clauses.
53
-	 *
54
-	 * @param array $where = []
55
-	 * @param array $orderBy = []
56
-	 * @param int $limit = -1
57
-	 * @param int $offset = 0
58
-	 * @return string the search query with the given where, order by, limit and offset clauses.
59
-	 */
60
-	private function getSearchQuery($where = [], $orderBy = [], $limit = -1, $offset = 0)
61
-	{
62
-		return sprintf(
63
-			'SELECT * FROM `%s` %s %s LIMIT %d OFFSET %d',
64
-			$this->getActiveRecordName(),
65
-			$this->getSearchQueryWhereClause($where),
66
-			$this->getSearchQueryOrderByClause($orderBy),
67
-			$limit,
68
-			$offset
69
-		);
70
-	}
71
-
72
-	/**
73
-	 * Returns the search query where clause.
74
-	 *
75
-	 * @param array $where
76
-	 * @return string the search query where clause.
77
-	 */
78
-	private function getSearchQueryWhereClause($where)
79
-	{
80
-		$columns = array_keys($this->getActiveRecordData());
81
-		$columns[] = 'id';
82
-		$result = [];
83
-
84
-		foreach ($where as $key => $value) {
85
-			if (!in_array($key, $columns)) {
86
-				throw new ActiveRecordException(sprintf('Search option key `%s` does not exists.', $key));
87
-			}
88
-
89
-			if (is_numeric($value)) {
90
-				$result[] = sprintf('`%s` = ?', $key);
91
-			} elseif (is_string($value)) {
92
-				$result[] = sprintf('`%s` LIKE ?', $key);
93
-			} elseif (is_null($value)) {
94
-				$result[] = sprintf('`%s` IS ?', $key);
95
-			} elseif (is_array($value) && !empty($value)) {
96
-				$result[] = sprintf('`%s` IN (%s)', $key, implode(',', array_fill(0, count($value), '?')));
97
-			} else {
98
-				throw new ActiveRecordException(sprintf('Search option value of key `%s` is not supported.', $key));
99
-			}
100
-		}
101
-
102
-		return empty($result) ? '' : 'WHERE ' . implode(' AND ', $result);
103
-	}
104
-
105
-	/**
106
-	 * Returns the search query order by clause.
107
-	 *
108
-	 * @param array $orderBy
109
-	 * @return string the search query order by clause.
110
-	 */
111
-	private function getSearchQueryOrderByClause($orderBy)
112
-	{
113
-		$result = [];
114
-
115
-		foreach ($orderBy as $key => $value) {
116
-			$result[] = sprintf('`%s` %s', $key, $value == 'DESC' ? 'DESC' : 'ASC');
117
-		}
118
-
119
-		return empty($result) ? '' : 'ORDER BY ' . implode(', ', $result);
120
-	}
20
+    /**
21
+     * {@inheritdoc}
22
+     */
23
+    public function search($where = [], $orderBy = [], $limit = -1, $offset = 0)
24
+    {
25
+        try {
26
+            $pdoStatement = $this->getPdo()->prepare($this->getSearchQuery($where, $orderBy, $limit, $offset));
27
+            array_walk_recursive($where, function(&$value) use ($pdoStatement) {
28
+                static $index = 1;
29
+
30
+                $pdoStatement->bindParam($index++, $value);
31
+            });
32
+
33
+            $pdoStatement->execute();
34
+            $result = [];
35
+
36
+            while ($fetch = $pdoStatement->fetch()) {
37
+                $new = new static($this->getPdo());
38
+
39
+                $new->setId(intval($fetch['id']));
40
+                $new->setActiveRecordData($fetch);
41
+
42
+                $result[] = $new;
43
+            }
44
+
45
+            return $result;
46
+        } catch (\PDOException $e) {
47
+            throw new ActiveRecordException(sprintf('Can not search the record in the `%s` table.', $this->getActiveRecordName()), 0, $e);
48
+        }
49
+    }
50
+
51
+    /**
52
+     * Returns the search query with the given where, order by, limit and offset clauses.
53
+     *
54
+     * @param array $where = []
55
+     * @param array $orderBy = []
56
+     * @param int $limit = -1
57
+     * @param int $offset = 0
58
+     * @return string the search query with the given where, order by, limit and offset clauses.
59
+     */
60
+    private function getSearchQuery($where = [], $orderBy = [], $limit = -1, $offset = 0)
61
+    {
62
+        return sprintf(
63
+            'SELECT * FROM `%s` %s %s LIMIT %d OFFSET %d',
64
+            $this->getActiveRecordName(),
65
+            $this->getSearchQueryWhereClause($where),
66
+            $this->getSearchQueryOrderByClause($orderBy),
67
+            $limit,
68
+            $offset
69
+        );
70
+    }
71
+
72
+    /**
73
+     * Returns the search query where clause.
74
+     *
75
+     * @param array $where
76
+     * @return string the search query where clause.
77
+     */
78
+    private function getSearchQueryWhereClause($where)
79
+    {
80
+        $columns = array_keys($this->getActiveRecordData());
81
+        $columns[] = 'id';
82
+        $result = [];
83
+
84
+        foreach ($where as $key => $value) {
85
+            if (!in_array($key, $columns)) {
86
+                throw new ActiveRecordException(sprintf('Search option key `%s` does not exists.', $key));
87
+            }
88
+
89
+            if (is_numeric($value)) {
90
+                $result[] = sprintf('`%s` = ?', $key);
91
+            } elseif (is_string($value)) {
92
+                $result[] = sprintf('`%s` LIKE ?', $key);
93
+            } elseif (is_null($value)) {
94
+                $result[] = sprintf('`%s` IS ?', $key);
95
+            } elseif (is_array($value) && !empty($value)) {
96
+                $result[] = sprintf('`%s` IN (%s)', $key, implode(',', array_fill(0, count($value), '?')));
97
+            } else {
98
+                throw new ActiveRecordException(sprintf('Search option value of key `%s` is not supported.', $key));
99
+            }
100
+        }
101
+
102
+        return empty($result) ? '' : 'WHERE ' . implode(' AND ', $result);
103
+    }
104
+
105
+    /**
106
+     * Returns the search query order by clause.
107
+     *
108
+     * @param array $orderBy
109
+     * @return string the search query order by clause.
110
+     */
111
+    private function getSearchQueryOrderByClause($orderBy)
112
+    {
113
+        $result = [];
114
+
115
+        foreach ($orderBy as $key => $value) {
116
+            $result[] = sprintf('`%s` %s', $key, $value == 'DESC' ? 'DESC' : 'ASC');
117
+        }
118
+
119
+        return empty($result) ? '' : 'ORDER BY ' . implode(', ', $result);
120
+    }
121 121
 }
Please login to merge, or discard this patch.
Spacing   +2 added lines, -2 removed lines patch added patch discarded remove patch
@@ -99,7 +99,7 @@  discard block
 block discarded – undo
99 99
 			}
100 100
 		}
101 101
 
102
-		return empty($result) ? '' : 'WHERE ' . implode(' AND ', $result);
102
+		return empty($result) ? '' : 'WHERE '.implode(' AND ', $result);
103 103
 	}
104 104
 
105 105
 	/**
@@ -116,6 +116,6 @@  discard block
 block discarded – undo
116 116
 			$result[] = sprintf('`%s` %s', $key, $value == 'DESC' ? 'DESC' : 'ASC');
117 117
 		}
118 118
 
119
-		return empty($result) ? '' : 'ORDER BY ' . implode(', ', $result);
119
+		return empty($result) ? '' : 'ORDER BY '.implode(', ', $result);
120 120
 	}
121 121
 }
Please login to merge, or discard this patch.
src/ActiveRecordSearchInterface.php 1 patch
Indentation   +11 added lines, -11 removed lines patch added patch discarded remove patch
@@ -17,15 +17,15 @@
 block discarded – undo
17 17
  */
18 18
 interface ActiveRecordSearchInterface extends ActiveRecordInterface
19 19
 {
20
-	/**
21
-	 * Returns the records with the given where, order by, limit and offset clauses.
22
-	 *
23
-	 * @param array $where = []
24
-	 * @param array $orderBy = []
25
-	 * @param int $limit = -1
26
-	 * @param int $offset = 0
27
-	 * @return static[] the records with the given where, order by, limit and offset clauses.
28
-	 * @throws ActiveRecordException on failure.
29
-	 */
30
-	public function search($where = [], $orderBy = [], $limit = -1, $offset = 0);
20
+    /**
21
+     * Returns the records with the given where, order by, limit and offset clauses.
22
+     *
23
+     * @param array $where = []
24
+     * @param array $orderBy = []
25
+     * @param int $limit = -1
26
+     * @param int $offset = 0
27
+     * @return static[] the records with the given where, order by, limit and offset clauses.
28
+     * @throws ActiveRecordException on failure.
29
+     */
30
+    public function search($where = [], $orderBy = [], $limit = -1, $offset = 0);
31 31
 }
Please login to merge, or discard this patch.