Skip to content

Commit

Permalink
refactor(builder): refactor from zephir.
Browse files Browse the repository at this point in the history
  • Loading branch information
noone-silent committed Jul 30, 2024
1 parent 9b4f68b commit cfd234e
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 62 deletions.
72 changes: 25 additions & 47 deletions src/Mvc/Model/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

namespace Phalcon\Mvc\Model\Query;

use Phalcon\Di\Di;
use Phalcon\Db\Column;
use Phalcon\Di\DiInterface;
use Phalcon\Mvc\Model\Exception;
Expand Down Expand Up @@ -190,7 +189,7 @@ public function __construct(array | string | null $params = null, DiInterface |
* Assign bind types
*/
if (isset($params["bind"])) {
$this->bindParams = $params['bind'];
$this->bindParams = $params['bind'] ?? [];
}

if (isset($params["bindTypes"])) {
Expand All @@ -200,94 +199,73 @@ public function __construct(array | string | null $params = null, DiInterface |
/**
* Assign SELECT DISTINCT / SELECT ALL clause
*/
if (isset($params["distinct"])) {
$this->distinct = $params['distinct'];
}
$this->distinct = $params['distinct'] ?? null;

/**
* Assign FROM clause
*/
if (isset($params["models"])) {
$this->models = $params['models'];
}
$this->models = $params['models'] ?? null;

/**
* Assign COLUMNS clause
*/
if (isset($params["columns"])) {
$this->columns = $params['columns'];
}
$this->columns = $params['columns'] ?? null;

/**
* Assign JOIN clause
*/
if (isset($params["joins"])) {
$this->joins = $params['joins'];
}
$this->joins = $params['joins'] ?? [];

/**
* Assign GROUP clause
*/
if (isset($params["group"])) {
$this->groupBy($params['group']);
}
$this->groupBy($params['group'] ?? []);

/**
* Assign HAVING clause
*/
if (isset($params["having"])) {
$this->having = $params['having'];
}
$this->having = $params['having'] ?? null;

/**
* Assign ORDER clause
*/
if (isset($params["order"])) {
$this->order = $params['order'];
}
$this->order = $params['order'] ?? [];

/**
* Assign LIMIT clause
*/
if (isset($params["limit"])) {
if (is_array($params['limit'])) {
if (isset($params['limit'][0]) && is_int($params['limit'][0])) {
$this->limit = $params['limit'][0];
}
if (isset($params['limit'][1]) && is_int($params['limit'][1])) {
$this->offset = $params['limit'][1];
}
if (isset($params['limit']['number']) && is_int($params['limit']['number'])) {
$this->limit = $params['limit']['number'];
}
if (isset($params['limit']['offset']) && is_int($params['limit']['offset'])) {
$this->offset = $params['limit']['offset'];
}
} else {
$this->limit = $params['limit'];
$this->limit = $params['limit'] ?? 0;
if (isset($params["limit"]) && is_array($params['limit'])) {
if (isset($params['limit'][0]) && is_numeric($params['limit'][0])) {
$this->limit = (int)$params['limit'][0];
}
if (isset($params['limit'][1]) && is_numeric($params['limit'][1])) {
$this->offset = (int)$params['limit'][1];
}
if (isset($params['limit']['number']) && is_numeric($params['limit']['number'])) {
$this->limit = (int)$params['limit']['number'];
}
if (isset($params['limit']['offset']) && is_numeric($params['limit']['offset'])) {
$this->offset = (int)$params['limit']['offset'];
}
}

/**
* Assign OFFSET clause
*/
if (isset($params["offset"])) {
$this->offset = $params['offset'];
$this->offset = $params['offset'] ?? 0;
}

/**
* Assign FOR UPDATE clause
*/
if (isset($params["for_update"])) {
$this->forUpdate = $params['for_update'];
}
$this->forUpdate = $params['for_update'] ?? false;

/**
* Assign SHARED LOCK clause
*/
if (isset($params["shared_lock"])) {
$this->sharedLock = $params['shared_lock'];
}
$this->sharedLock = $params['shared_lock'] ?? false;

/**
* Update the dependency injector if any
Expand Down Expand Up @@ -658,7 +636,7 @@ final public function getPhql(): string
{
$container = $this->container;
if (!is_object($container)) {
$container = Di::getDefault();
$container = $this->getDI();
if ($container === null) {
throw new Exception('Di container required');
}
Expand Down
3 changes: 1 addition & 2 deletions src/Paginator/Adapter/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,9 @@ public function paginate(): RepositoryInterface
$next = $totalPages;
}

$previous = 1;
if ($pageNumber > 1) {
$previous = $pageNumber - 1;
} else {
$previous = 1;
}

return $this->getRepository(
Expand Down
8 changes: 3 additions & 5 deletions src/Paginator/Adapter/NativeArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,17 +76,15 @@ public function paginate(): RepositoryInterface
$show
);

//Fix next
// Fix next
$next = $totalPages;
if ($pageNumber < $totalPages) {
$next = $pageNumber + 1;
} else {
$next = $totalPages;
}

$previous = 1;
if ($pageNumber > 1) {
$previous = $pageNumber - 1;
} else {
$previous = 1;
}

return $this->getRepository(
Expand Down
5 changes: 2 additions & 3 deletions tests/database/Mvc/Model/Criteria/FromInputCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

use DatabaseTester;
use Phalcon\Mvc\Model\Criteria;
use Phalcon\Storage\Exception;
use Phalcon\Tests\Fixtures\Traits\DiTrait;
use Phalcon\Tests\Models\Invoices;

Expand Down Expand Up @@ -73,8 +72,8 @@ public function mvcModelCriteriaFromInputMysql(DatabaseTester $I): void
. 'AND [inv_cst_id] = :inv_cst_id: '
. 'AND [inv_status_flag] = :inv_status_flag: '
. 'AND [inv_title] LIKE :inv_title: '
. 'AND [inv_total] = :inv_total: '
. 'AND [inv_created_at] = :inv_created_at:';
. 'AND [inv_total] LIKE :inv_total: '
. 'AND [inv_created_at] LIKE :inv_created_at:';
} else {
$expected = 'SELECT [Phalcon\Tests\Models\Invoices].* '
. 'FROM [Phalcon\Tests\Models\Invoices] '
Expand Down
4 changes: 0 additions & 4 deletions tests/database/Mvc/Model/Criteria/JoinCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
namespace Phalcon\Tests\Database\Mvc\Model\Criteria;

use Codeception\Attribute\Group;
use Codeception\Util\Debug;
use DatabaseTester;
use Phalcon\Mvc\Model\Criteria;
use Phalcon\Mvc\Model\Query\Builder;
Expand Down Expand Up @@ -119,9 +118,6 @@ public function mvcModelCriteriaJoinManyToManyMultipleSchema(DatabaseTester $I)
$expected = 'private';
$I->assertStringContainsString($expected, $request['sql']);

Debug::debug($I->getDriver());
Debug::debug($request['sql']);

$I->assertInstanceOf(Simple::class, $query->execute());
}
}
2 changes: 1 addition & 1 deletion tests/database/Mvc/Model/Query/Builder/OrderByCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function mvcModelQueryBuilderOrderBy(DatabaseTester $I)
{
$I->wantToTest('Mvc\Model\Query\Builder :: orderBy()');

$builder = new Builder();
$builder = new Builder(null, $this->container);
$phql = $builder
->columns('inv_id, inv_title')
->addFrom(Invoices::class)
Expand Down

0 comments on commit cfd234e

Please sign in to comment.