Skip to content

Commit 48d7fd9

Browse files
author
Edwin Daniels
committed
setup refactor, to utilize basic Eloquent query builder-esque methods. when time allows will move completely over to Eloquents library, for now just mocked similar API
1 parent 735ad47 commit 48d7fd9

File tree

5 files changed

+558
-91
lines changed

5 files changed

+558
-91
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
.DS_store
12
vendor/
23
config/*.php
34
.idea/

src/Salesforce/Database/Model.php

+32-91
Original file line numberDiff line numberDiff line change
@@ -59,109 +59,53 @@ public static function hydrateFactory($data)
5959
return $instance;
6060
}
6161

62+
6263
/**
6364
* @param $field
6465
* @param $value
6566
*
66-
* @todo migrate to a query builder object
67-
* @return mixed
67+
* @return Collection
6868
*/
69-
protected static function q_castQueryValue($field, $value)
69+
public static function findBy()
7070
{
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();
7676
}
7777

78+
7879
/**
79-
* @param $selectFields
80-
* @param $objectName
81-
* @param array $searchCriteria
80+
* Handle dynamic static method calls into the method.
8281
*
83-
* @todo migrate to a query builder object
84-
* @return string
82+
* @param string $method
83+
* @param array $parameters
84+
* @return mixed
8585
*/
86-
protected static function q_buildSelectQuery($selectFields, $objectName, $searchCriteria = [])
86+
public static function __callStatic($method, $parameters)
8787
{
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);
10689
}
10790

10891
/**
109-
* @param $search
92+
* Convert the model to its string representation.
11093
*
111-
* @todo migrate to a query builder object
112-
* @return array
94+
* @return string
11395
*/
114-
protected static function q_fetchResults($search)
96+
public function __toString()
11597
{
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();
14199
}
142100

143101
/**
144-
* @param $field
145-
* @param $value
146-
*
147-
* @return Collection
102+
* @return QueryBuilder
148103
*/
149-
public static function findBy()
104+
public function newQuery()
150105
{
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);
165109
}
166110

167111
/**
@@ -172,17 +116,10 @@ public static function findBy()
172116
*/
173117
public static function findOneBy()
174118
{
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])) {
180121

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];
186123
}
187124

188125
return null;
@@ -382,7 +319,11 @@ public function __call($name, $arguments)
382319
return $this;
383320
}
384321

385-
return null;
322+
if (in_array($name, ['increment', 'decrement'])) {
323+
return $this->$name(...$arguments);
324+
}
325+
326+
return $this->newQuery()->$name(...$arguments);
386327
}
387328

388329
/**

0 commit comments

Comments
 (0)