Skip to content

Commit

Permalink
Add improved support for static magic findWhere* methods
Browse files Browse the repository at this point in the history
  • Loading branch information
nicksagona committed Dec 7, 2023
1 parent e0dbc0f commit d8758f2
Show file tree
Hide file tree
Showing 2 changed files with 176 additions and 9 deletions.
124 changes: 115 additions & 9 deletions src/Record.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,20 @@
* @copyright Copyright (c) 2009-2024 NOLA Interactive, LLC. (http://www.nolainteractive.com)
* @license http://www.popphp.org/license New BSD License
* @version 6.0.0
* @method static findWhereEquals($column, $value, array $options = null, bool $asArray = false)
* @method static findWhereNotEquals($column, $value, array $options = null, bool $asArray = false)
* @method static findWhereGreaterThan($column, $value, array $options = null, bool $asArray = false)
* @method static findWhereGreaterThanOrEqual($column, $value, array $options = null, bool $asArray = false)
* @method static findWhereLessThan($column, $value, array $options = null, bool $asArray = false)
* @method static findWhereLessThanOrEqual($column, $value, array $options = null, bool $asArray = false)
* @method static findWhereLike($column, $value, array $options = null, bool $asArray = false)
* @method static findWhereNotLike($column, $value, array $options = null, bool $asArray = false)
* @method static findWhereIn($column, $values, array $options = null, bool $asArray = false)
* @method static findWhereNotIn($column, $values, array $options = null, bool $asArray = false)
* @method static findWhereBetween($column, $values, array $options = null, bool $asArray = false)
* @method static findWhereNotBetween($column, $values, array $options = null, bool $asArray = false)
* @method static findWhereNull($column, array $options = null, bool $asArray = false)
* @method static findWhereNotNull($column, array $options = null, bool $asArray = false)
*/
class Record extends Record\AbstractRecord
{
Expand Down Expand Up @@ -935,27 +949,119 @@ public function delete(array $columns = null, bool $commit = true): void
/**
* Call static method for 'findWhere'
*
* $users = Users::findWhereUsername('testuser');
* $users = Users::findWhereUsername($value);
*
* $users = Users::findWhereEquals($column, $value);
* $users = Users::findWhereNotEquals($column, $value);
* $users = Users::findWhereGreaterThan($column, $value);
* $users = Users::findWhereGreaterThanOrEqual($column, $value);
* $users = Users::findWhereLessThan($column, $value);
* $users = Users::findWhereLessThanOrEqual($column, $value);
*
* $users = Users::findWhereLike($column, $value);
* $users = Users::findWhereNotLike($column, $value);
*
* $users = Users::findWhereIn($column, $values);
* $users = Users::findWhereNotIn($column, $values);
*
* $users = Users::findWhereBetween($column, $values);
* $users = Users::findWhereNotBetween($column, $values);
*
* $users = Users::findWhereNull($column);
* $users = Users::findWhereNotNull($column);
*
* @param string $name
* @param array $arguments
* @return Collection|array|null
*/
public static function __callStatic(string $name, array $arguments): Collection|array|null
{
$record = null;
$columns = null;
$options = null;
$asArray = false;
$conditions = [
'Equals', 'NotEquals', 'GreaterThan', 'GreaterThanOrEqual', 'LessThan', 'LessThanOrEqual',
'Like', 'NotLike', 'In', 'NotIn', 'Between', 'NotBetween', 'Null', 'NotNull'
];

if (str_starts_with($name, 'findWhere')) {
if (in_array(substr($name, 9), $conditions)) {
$condition = substr($name, 9);
$column = $arguments[0];

if (str_contains($condition, 'Null')) {
$value = null;
$options = $arguments[1] ?? null;
$asArray = $arguments[2] ?? false;
} else {
$value = $arguments[1];
$options = $arguments[2] ?? null;
$asArray = $arguments[3] ?? false;
}

if (substr($name, 0, 9) == 'findWhere') {
$column = Sql\Parser\Table::parse(substr($name, 9));
$arg1 = $arguments[0] ?? null;
$arg2 = $arguments[1] ?? null;
switch ($condition) {
case 'Equals':
case 'In':
case 'Between':
case 'Null':
$columns = [$column => $value];
break;
case 'NotEquals':
$columns = [$column . '!=' => $value];
break;
case 'GreaterThan':
$columns = [$column . '>' => $value];
break;
case 'GreaterThanOrEqual':
$columns = [$column . '>=' => $value];
break;
case 'LessThan':
$columns = [$column . '<' => $value];
break;
case 'LessThanOrEqual':
$columns = [$column . '<=' => $value];
break;
case 'Like':
if (str_starts_with($value, '%')) {
$column = '%' . $column;
$value = substr($value, 1);
}
if (str_ends_with($value, '%')) {
$column .= '%';
$value = substr($value, 0, -1);
}
$columns = [$column => $value];
break;
case 'NotLike':
if (str_starts_with($value, '%')) {
$column = '-%' . $column;
$value = substr($value, 1);
}
if (str_ends_with($value, '%')) {
$column .= '%-';
$value = substr($value, 0, -1);
}
$columns = [$column => $value];
break;
case 'NotIn':
case 'NotBetween':
case 'NotNull':
$columns = [$column . '-' => $value];
break;
}
} else {
$column = Sql\Parser\Table::parse(substr($name, 9));
$value = $arguments[0] ?? null;
$options = $arguments[1] ?? null;
$asArray = $arguments[2] ?? false;

if ($arg1 !== null) {
$record = static::findBy([$column => $arg1], $arg2);
if ($value !== null) {
$columns = [$column => $value];
}
}
}

return $record;
return ($columns !== null) ? static::findBy($columns, $options, $asArray) : null;
}

}
61 changes: 61 additions & 0 deletions tests/RecordTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -925,6 +925,67 @@ public function testFindWhere()
$newUsers2 = Users::findWhereUsername('testuser27');
$this->assertEquals(0, $newUsers2->count());

$this->db->disconnect();
}

public function testFindWhereConditions()
{
$user = new Users([
'username' => 'testuser24',
'password' => 'password24',
'email' => '[email protected]',
'logins' => 1
]);
$user->save();

$users = Users::findWhereGreaterThan('logins', 0);
$this->assertGreaterThanOrEqual(1, $users->count());

$users = Users::findWhereGreaterThanOrEqual('logins', 1);
$this->assertGreaterThanOrEqual(1, $users->count());

$users = Users::findWhereLessThan('logins', 2);
$this->assertGreaterThanOrEqual(1, $users->count());

$users = Users::findWhereLessThanOrEqual('logins', 1);
$this->assertGreaterThanOrEqual(1, $users->count());

$users = Users::findWhereEquals('logins', 1);
$this->assertGreaterThanOrEqual(1, $users->count());

$users = Users::findWhereNotEquals('logins', -1);
$this->assertGreaterThanOrEqual(1, $users->count());

$users = Users::findWhereIn('logins', [1]);
$this->assertGreaterThanOrEqual(1, $users->count());

$users = Users::findWhereNotIn('logins', [10000000]);
$this->assertGreaterThanOrEqual(1, $users->count());

$users = Users::findWhereNull('logins');
$this->assertEquals(0, $users->count());

$users = Users::findWhereNotNull('logins');
$this->assertGreaterThanOrEqual(1, $users->count());

$users = Users::findWhereBetween('logins', '(0, 10)');
$this->assertGreaterThanOrEqual(1, $users->count());

$users = Users::findWhereNotBetween('logins', '(1000000, 1000010)');
$this->assertGreaterThanOrEqual(1, $users->count());

$users = Users::findWhereLike('username', 'testuser%');
$this->assertGreaterThanOrEqual(1, $users->count());

$users = Users::findWhereNotLike('username', 'baduser%');
$this->assertGreaterThanOrEqual(1, $users->count());

$users = Users::findWhereLike('username', '%testuser24');
$this->assertGreaterThanOrEqual(1, $users->count());

$users = Users::findWhereNotLike('username', '%baduser');
$this->assertGreaterThanOrEqual(1, $users->count());

$schema = $this->db->createSchema();
$schema->dropIfExists('users');
$schema->execute();
Expand Down

0 comments on commit d8758f2

Please sign in to comment.