@@ -59,109 +59,53 @@ public static function hydrateFactory($data)
59
59
return $ instance ;
60
60
}
61
61
62
+
62
63
/**
63
64
* @param $field
64
65
* @param $value
65
66
*
66
- * @todo migrate to a query builder object
67
- * @return mixed
67
+ * @return Collection
68
68
*/
69
- protected static function q_castQueryValue ( $ field , $ value )
69
+ public static function findBy ( )
70
70
{
71
- if ( is_bool ( $ value )) {
72
- return $ value ? ' true ' : ' false ' ;
73
- } else {
74
- return " ' " . addslashes ( $ value ) . " ' " ;
75
- }
71
+ $ instance = new static ;
72
+ $ args = func_get_args () ? func_get_args () : [[]] ;
73
+ $ query = call_user_func_array ([ $ instance , ' where ' ], $ args );
74
+
75
+ return $ query -> get ();
76
76
}
77
77
78
+
78
79
/**
79
- * @param $selectFields
80
- * @param $objectName
81
- * @param array $searchCriteria
80
+ * Handle dynamic static method calls into the method.
82
81
*
83
- * @todo migrate to a query builder object
84
- * @return string
82
+ * @param string $method
83
+ * @param array $parameters
84
+ * @return mixed
85
85
*/
86
- protected static function q_buildSelectQuery ( $ selectFields , $ objectName , $ searchCriteria = [] )
86
+ public static function __callStatic ( $ method , $ parameters )
87
87
{
88
-
89
- $ query = "SELECT %s FROM %s " ;
90
- $ ands = [];
91
-
92
- foreach ($ searchCriteria as $ search ) {
93
- foreach ($ search as $ field => $ val ) {
94
- $ ands [] = $ field . ' = ' . self ::q_castQueryValue ($ field , $ val );
95
- }
96
- }
97
-
98
- if (count ($ ands )) {
99
- $ query .= " WHERE " . implode (' AND ' , $ ands );
100
- }
101
-
102
- return sprintf ($ query ,
103
- implode (', ' , $ selectFields ),
104
- $ objectName
105
- );
88
+ return (new static )->$ method (...$ parameters );
106
89
}
107
90
108
91
/**
109
- * @param $search
92
+ * Convert the model to its string representation.
110
93
*
111
- * @todo migrate to a query builder object
112
- * @return array
94
+ * @return string
113
95
*/
114
- protected static function q_fetchResults ( $ search )
96
+ public function __toString ( )
115
97
{
116
- // get our fields
117
- $ fields = array_keys (static ::getSchema ());
118
-
119
- $ q = self ::q_buildSelectQuery ($ fields , static ::resolveObjectName (), $ search );
120
- // if searching IsArchived or IsDeleted, fetch from queryAll https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/dome_queryall.htm
121
- $ queryAll = false ;
122
- foreach ($ search as $ filter ) {
123
- foreach ($ filter as $ field => $ value ) {
124
- switch ($ field ) {
125
- case 'IsArchived ' :
126
- case 'IsDeleted ' :
127
- $ queryAll = true ;
128
- break 3 ;
129
- }
130
- }
131
- }
132
-
133
- $ connection = self ::$ connection ;
134
- if ($ queryAll ) {
135
- $ results = $ connection ->queryAll ($ q );
136
- } else {
137
- $ results = $ connection ->query ($ q );
138
- }
139
-
140
- return $ results ;
98
+ return $ this ->toJson ();
141
99
}
142
100
143
101
/**
144
- * @param $field
145
- * @param $value
146
- *
147
- * @return Collection
102
+ * @return QueryBuilder
148
103
*/
149
- public static function findBy ()
104
+ public function newQuery ()
150
105
{
151
- $ search = func_get_args ();
152
- if (func_num_args () == 2 ) {
153
- $ args = func_get_args ();
154
- $ search = [[$ args [0 ] => $ args [1 ]]];
155
- }
156
-
157
- $ results = self ::q_fetchResults ($ search );
158
-
159
- $ collection = [];
160
- foreach ($ results ['records ' ] as $ item ) {
161
- $ collection [] = self ::hydrateFactory ($ item );
162
- }
163
-
164
- return new Collection ($ collection );
106
+ return (new QueryBuilder (self ::$ connection ))
107
+ ->from (static ::resolveObjectName ())
108
+ ->select (array_keys (static ::getSchema ()))->setModel ($ this );
165
109
}
166
110
167
111
/**
@@ -172,17 +116,10 @@ public static function findBy()
172
116
*/
173
117
public static function findOneBy ()
174
118
{
175
- $ search = func_get_args ();
176
- if (func_num_args () == 2 ) {
177
- $ args = func_get_args ();
178
- $ search = [[$ args [0 ] => $ args [1 ]]];
179
- }
119
+ $ results = self ::findBy (...func_get_args ());
120
+ if (isset ($ results [0 ])) {
180
121
181
- $ results = self ::q_fetchResults ($ search );
182
-
183
- if (isset ($ results ['records ' ][0 ])) {
184
-
185
- return self ::hydrateFactory ($ results ['records ' ][0 ]);
122
+ return $ results [0 ];
186
123
}
187
124
188
125
return null ;
@@ -382,7 +319,11 @@ public function __call($name, $arguments)
382
319
return $ this ;
383
320
}
384
321
385
- return null ;
322
+ if (in_array ($ name , ['increment ' , 'decrement ' ])) {
323
+ return $ this ->$ name (...$ arguments );
324
+ }
325
+
326
+ return $ this ->newQuery ()->$ name (...$ arguments );
386
327
}
387
328
388
329
/**
0 commit comments