From 2641166540956249438d47de98f36293c4739a5a Mon Sep 17 00:00:00 2001 From: Ho-Ling Poon Date: Mon, 26 Feb 2018 14:11:58 -0500 Subject: [PATCH 01/26] Adding row count to ModelSet. --- src/ModelSet.php | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/src/ModelSet.php b/src/ModelSet.php index 754c80a..ef266a8 100644 --- a/src/ModelSet.php +++ b/src/ModelSet.php @@ -39,6 +39,16 @@ class ModelSet extends Model implements ReadInterface */ public $limit = 25; + /** + * @var bool + */ + public $includeCount = false; + + /** + * @var int + */ + public $rowCount = 0; + /** * @var bool */ @@ -175,4 +185,36 @@ public function setNoDefaultSorting($noDefaultSorting) { $this->noDefaultSorting = (bool) $noDefaultSorting; } + + /** + * @return bool + */ + public function isIncludeCount() + { + return $this->includeCount; + } + + /** + * @param bool $includeCount + */ + public function setIncludeCount($includeCount) + { + $this->includeCount = $includeCount; + } + + /** + * @return int + */ + public function getRowCount() + { + return $this->rowCount; + } + + /** + * @param int $rowCount + */ + public function setRowCount($rowCount) + { + $this->rowCount = $rowCount; + } } From 879f7945dc43f0c69fce4f87dad5842f6b63edee Mon Sep 17 00:00:00 2001 From: Ho-Ling Poon Date: Mon, 26 Feb 2018 15:44:28 -0500 Subject: [PATCH 02/26] Adding rowCount to DBReadTrait. --- src/Model/ModelTrait/DBReadTrait.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/Model/ModelTrait/DBReadTrait.php b/src/Model/ModelTrait/DBReadTrait.php index 5136c89..82dfb2c 100644 --- a/src/Model/ModelTrait/DBReadTrait.php +++ b/src/Model/ModelTrait/DBReadTrait.php @@ -289,6 +289,9 @@ protected function setSet($ignoreNoRecord = false) } if ($selectStatement->rowCount()) { + if ($this->isIncludeCount()) { + $this->setRowCount($selectStatement->rowCount()); + } $className = get_class($this->getBaseModel()); foreach ($selectStatement->fetchAll() as $result) { From 9dc00310c8d8491e29374e3d6af8133f969d6fae Mon Sep 17 00:00:00 2001 From: Ho-Ling Poon Date: Mon, 26 Feb 2018 17:11:40 -0500 Subject: [PATCH 03/26] Adding include_count detection in filters. --- src/Filter.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/Filter.php b/src/Filter.php index 27eb6e2..4a27764 100644 --- a/src/Filter.php +++ b/src/Filter.php @@ -100,6 +100,14 @@ public function isRangeFilter() return strpos($this->getFilterValue(), '[') !== false && strpos($this->getFilterValue(), ']') !== false; } + /** + * @return bool + */ + public function isIncludeCount() + { + return $this->getFilterColumn() == 'include_count'; + } + /** * @param bool $jsonColumn */ From 90d50433cd95fa2c3305241a321b26e759d60569 Mon Sep 17 00:00:00 2001 From: Ho-Ling Poon Date: Tue, 27 Feb 2018 11:00:32 -0500 Subject: [PATCH 04/26] Testing for total count to show up on data. --- src/Controller.php | 2 ++ src/ModelSet.php | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Controller.php b/src/Controller.php index b354707..a9139c1 100644 --- a/src/Controller.php +++ b/src/Controller.php @@ -249,6 +249,8 @@ protected function getDefaultReadResponse( $model->setLimit($this->getRequest()->getParam('limit')); + $model->setIncludeCount($this->getRequest()->getParam('include_count')); + if ($filter) { $model->addFilter($filter); } diff --git a/src/ModelSet.php b/src/ModelSet.php index ef266a8..ae0b3de 100644 --- a/src/ModelSet.php +++ b/src/ModelSet.php @@ -197,7 +197,7 @@ public function isIncludeCount() /** * @param bool $includeCount */ - public function setIncludeCount($includeCount) + public function setIncludeCount($includeCount = false) { $this->includeCount = $includeCount; } From e39e1ebefe7f8a93db5d9b4b8136b430d8f049f1 Mon Sep 17 00:00:00 2001 From: Ho-Ling Poon Date: Wed, 28 Feb 2018 17:14:41 -0500 Subject: [PATCH 05/26] Preliminary version of showing total count with date range. --- src/Controller.php | 6 ++- src/Model.php | 1 - src/Model/ModelTrait/DBReadTrait.php | 11 ++++- src/Model/Response/SuccessResponse.php | 24 ++++++++- src/ModelSet.php | 29 +++++------ src/TotalCount.php | 67 ++++++++++++++++++++++++++ 6 files changed, 115 insertions(+), 23 deletions(-) create mode 100644 src/TotalCount.php diff --git a/src/Controller.php b/src/Controller.php index a9139c1..b03b3e2 100644 --- a/src/Controller.php +++ b/src/Controller.php @@ -249,7 +249,9 @@ protected function getDefaultReadResponse( $model->setLimit($this->getRequest()->getParam('limit')); - $model->setIncludeCount($this->getRequest()->getParam('include_count')); + $model->setTotalCount(new TotalCount($this->getRequest()->getParam('include_count'))); + + $model->setIncludeTotalCount($this->getRequest()->getParam('include_count')); if ($filter) { $model->addFilter($filter); @@ -263,7 +265,7 @@ protected function getDefaultReadResponse( $model->read(); - $response->initializeResponse($model->getData()); + $response->initializeResponse($model->getData(), $model->getTotalCount()); } else { if ($filter) { if ($filter->getId()) { diff --git a/src/Model.php b/src/Model.php index 6335c9a..b9c081e 100644 --- a/src/Model.php +++ b/src/Model.php @@ -69,7 +69,6 @@ public function jsonSerialize() $jsonArray[$this->getJsonObjectName($objectName)] = $this->getJsonObjectValue($objectValue); } } - return $jsonArray; } diff --git a/src/Model/ModelTrait/DBReadTrait.php b/src/Model/ModelTrait/DBReadTrait.php index 82dfb2c..c54f264 100644 --- a/src/Model/ModelTrait/DBReadTrait.php +++ b/src/Model/ModelTrait/DBReadTrait.php @@ -2,6 +2,7 @@ namespace NYPL\Starter\Model\ModelTrait; use NYPL\Starter\APIException; +use NYPL\Starter\APILogger; use NYPL\Starter\DB; use NYPL\Starter\Filter; use NYPL\Starter\Filter\OrFilter; @@ -289,8 +290,14 @@ protected function setSet($ignoreNoRecord = false) } if ($selectStatement->rowCount()) { - if ($this->isIncludeCount()) { - $this->setRowCount($selectStatement->rowCount()); + if ($this->isIncludeTotalCount()) { + $saveSelectStatement = DB::getDatabase()->select() + ->from($baseModel->translateDbName($baseModel->getTableName())); + if ($this->getFilters()) { + $this->applyFilters($this->getFilters(), $saveSelectStatement); + } + $saveSelectStatement = $saveSelectStatement->execute(); + $this->setTotalCount($saveSelectStatement->rowCount()); } $className = get_class($this->getBaseModel()); diff --git a/src/Model/Response/SuccessResponse.php b/src/Model/Response/SuccessResponse.php index 83e4bc6..c51af55 100644 --- a/src/Model/Response/SuccessResponse.php +++ b/src/Model/Response/SuccessResponse.php @@ -16,6 +16,11 @@ abstract class SuccessResponse extends Response */ public $count = 0; + /** + * @var int + */ + public $totalCount = 0; + /** * @var int */ @@ -37,12 +42,13 @@ public function __construct($model = null, $code = 200) /** * @param Model|Model[] $model */ - public function initializeResponse($model) + public function initializeResponse($model, $totalCount = 0) { $this->setData($model); if (is_array($model)) { $this->setCount(count($model)); + $this->setTotalCount($totalCount); } else { $this->setCount(1); } @@ -95,4 +101,20 @@ public function setCount($count) { $this->count = $count; } + + /** + * @return int + */ + public function getTotalCount() + { + return $this->totalCount; + } + + /** + * @param int $totalCount + */ + public function setTotalCount($totalCount) + { + $this->totalCount = $totalCount; + } } diff --git a/src/ModelSet.php b/src/ModelSet.php index ae0b3de..0e46f6c 100644 --- a/src/ModelSet.php +++ b/src/ModelSet.php @@ -40,14 +40,9 @@ class ModelSet extends Model implements ReadInterface public $limit = 25; /** - * @var bool - */ - public $includeCount = false; - - /** - * @var int + * @var TotalCount */ - public $rowCount = 0; + public $totalCount; /** * @var bool @@ -189,32 +184,32 @@ public function setNoDefaultSorting($noDefaultSorting) /** * @return bool */ - public function isIncludeCount() + public function isIncludeTotalCount() { - return $this->includeCount; + return $this->includeTotalCount; } /** - * @param bool $includeCount + * @param bool $includeTotalCount */ - public function setIncludeCount($includeCount = false) + public function setIncludeTotalCount($includeTotalCount) { - $this->includeCount = $includeCount; + $this->includeTotalCount = $includeTotalCount; } /** * @return int */ - public function getRowCount() + public function getTotalCount() { - return $this->rowCount; + return $this->totalCount; } /** - * @param int $rowCount + * @param int $totalCount */ - public function setRowCount($rowCount) + public function setTotalCount($totalCount) { - $this->rowCount = $rowCount; + $this->totalCount = $totalCount; } } diff --git a/src/TotalCount.php b/src/TotalCount.php new file mode 100644 index 0000000..5ac1595 --- /dev/null +++ b/src/TotalCount.php @@ -0,0 +1,67 @@ +setIncludeCount($includeCount); + } + + if ($count) { + $this->setCount($count); + } + + } + + /** + * @return bool + */ + public function isIncludeCount() + { + return $this->includeCount; + } + + /** + * @param bool $includeCount + */ + public function setIncludeCount($includeCount) + { + $this->includeCount = $includeCount; + } + + /** + * @return int + */ + public function getCount() + { + return $this->count; + } + + /** + * @param int $count + */ + public function setCount($count) + { + $this->count = $count; + } +} From 54ea7bf1a91c7d687315edcd4812c6c6631ab4b2 Mon Sep 17 00:00:00 2001 From: Ho-Ling Poon Date: Thu, 1 Mar 2018 11:15:52 -0500 Subject: [PATCH 06/26] Refactoring TotalCount as its own PHP class. --- src/Controller.php | 2 -- src/Model/ModelTrait/DBReadTrait.php | 4 +++- src/Model/Response/SuccessResponse.php | 12 +++++++----- src/ModelSet.php | 20 ++++++-------------- 4 files changed, 16 insertions(+), 22 deletions(-) diff --git a/src/Controller.php b/src/Controller.php index b03b3e2..9de420c 100644 --- a/src/Controller.php +++ b/src/Controller.php @@ -251,8 +251,6 @@ protected function getDefaultReadResponse( $model->setTotalCount(new TotalCount($this->getRequest()->getParam('include_count'))); - $model->setIncludeTotalCount($this->getRequest()->getParam('include_count')); - if ($filter) { $model->addFilter($filter); } diff --git a/src/Model/ModelTrait/DBReadTrait.php b/src/Model/ModelTrait/DBReadTrait.php index c54f264..b7d2c42 100644 --- a/src/Model/ModelTrait/DBReadTrait.php +++ b/src/Model/ModelTrait/DBReadTrait.php @@ -9,6 +9,7 @@ use NYPL\Starter\Model; use NYPL\Starter\ModelSet; use NYPL\Starter\OrderBy; +use NYPL\Starter\TotalCount; use Slim\PDO\Statement\SelectStatement; use Slim\PDO\Statement\StatementContainer; @@ -297,8 +298,9 @@ protected function setSet($ignoreNoRecord = false) $this->applyFilters($this->getFilters(), $saveSelectStatement); } $saveSelectStatement = $saveSelectStatement->execute(); - $this->setTotalCount($saveSelectStatement->rowCount()); + $this->setTotalCount(new TotalCount($this->isIncludeTotalCount(),$saveSelectStatement->rowCount())); } + $className = get_class($this->getBaseModel()); foreach ($selectStatement->fetchAll() as $result) { diff --git a/src/Model/Response/SuccessResponse.php b/src/Model/Response/SuccessResponse.php index c51af55..8bc9d66 100644 --- a/src/Model/Response/SuccessResponse.php +++ b/src/Model/Response/SuccessResponse.php @@ -3,6 +3,7 @@ use NYPL\Starter\Model; use NYPL\Starter\Model\Response; +use NYPL\Starter\TotalCount; abstract class SuccessResponse extends Response { @@ -17,9 +18,9 @@ abstract class SuccessResponse extends Response public $count = 0; /** - * @var int + * @var TotalCount */ - public $totalCount = 0; + public $totalCount; /** * @var int @@ -41,8 +42,9 @@ public function __construct($model = null, $code = 200) /** * @param Model|Model[] $model + * @param TotalCount $totalCount */ - public function initializeResponse($model, $totalCount = 0) + public function initializeResponse($model, $totalCount = null) { $this->setData($model); @@ -103,7 +105,7 @@ public function setCount($count) } /** - * @return int + * @return TotalCount */ public function getTotalCount() { @@ -111,7 +113,7 @@ public function getTotalCount() } /** - * @param int $totalCount + * @param TotalCount $totalCount */ public function setTotalCount($totalCount) { diff --git a/src/ModelSet.php b/src/ModelSet.php index 0e46f6c..ad07f99 100644 --- a/src/ModelSet.php +++ b/src/ModelSet.php @@ -40,9 +40,9 @@ class ModelSet extends Model implements ReadInterface public $limit = 25; /** - * @var TotalCount + * @var null|TotalCount */ - public $totalCount; + public $totalCount = null; /** * @var bool @@ -186,19 +186,11 @@ public function setNoDefaultSorting($noDefaultSorting) */ public function isIncludeTotalCount() { - return $this->includeTotalCount; + return $this->getTotalCount()->isIncludeCount(); } /** - * @param bool $includeTotalCount - */ - public function setIncludeTotalCount($includeTotalCount) - { - $this->includeTotalCount = $includeTotalCount; - } - - /** - * @return int + * @return null|TotalCount */ public function getTotalCount() { @@ -206,9 +198,9 @@ public function getTotalCount() } /** - * @param int $totalCount + * @param null|TotalCount $totalCount */ - public function setTotalCount($totalCount) + public function setTotalCount($totalCount = null) { $this->totalCount = $totalCount; } From 478096b2ed1a9c04c3d472040e0c63709244d1ca Mon Sep 17 00:00:00 2001 From: Ho-Ling Poon Date: Thu, 1 Mar 2018 11:42:14 -0500 Subject: [PATCH 07/26] Adding conditionals to reflect only showing total count by query parameters. --- src/Controller.php | 11 ++++++++--- src/Filter.php | 8 -------- src/Model/ModelTrait/DBReadTrait.php | 4 ++-- src/ModelSet.php | 2 +- 4 files changed, 11 insertions(+), 14 deletions(-) diff --git a/src/Controller.php b/src/Controller.php index 9de420c..6820d67 100644 --- a/src/Controller.php +++ b/src/Controller.php @@ -249,7 +249,9 @@ protected function getDefaultReadResponse( $model->setLimit($this->getRequest()->getParam('limit')); - $model->setTotalCount(new TotalCount($this->getRequest()->getParam('include_count'))); + if ($this->getRequest()->getParam('include_count') == 'true') { + $model->setTotalCount(new TotalCount($this->getRequest()->getParam('include_count'))); + } if ($filter) { $model->addFilter($filter); @@ -262,8 +264,11 @@ protected function getDefaultReadResponse( } $model->read(); - - $response->initializeResponse($model->getData(), $model->getTotalCount()); + if ($this->getRequest()->getParam('include_count') == 'true') { + $response->initializeResponse($model->getData(), $model->getTotalCount()); + } else { + $response->initializeResponse($model->getData()); + } } else { if ($filter) { if ($filter->getId()) { diff --git a/src/Filter.php b/src/Filter.php index 4a27764..27eb6e2 100644 --- a/src/Filter.php +++ b/src/Filter.php @@ -100,14 +100,6 @@ public function isRangeFilter() return strpos($this->getFilterValue(), '[') !== false && strpos($this->getFilterValue(), ']') !== false; } - /** - * @return bool - */ - public function isIncludeCount() - { - return $this->getFilterColumn() == 'include_count'; - } - /** * @param bool $jsonColumn */ diff --git a/src/Model/ModelTrait/DBReadTrait.php b/src/Model/ModelTrait/DBReadTrait.php index b7d2c42..31177fb 100644 --- a/src/Model/ModelTrait/DBReadTrait.php +++ b/src/Model/ModelTrait/DBReadTrait.php @@ -291,7 +291,7 @@ protected function setSet($ignoreNoRecord = false) } if ($selectStatement->rowCount()) { - if ($this->isIncludeTotalCount()) { + if ($this->isIncludeTotalCount() == true) { $saveSelectStatement = DB::getDatabase()->select() ->from($baseModel->translateDbName($baseModel->getTableName())); if ($this->getFilters()) { @@ -300,7 +300,7 @@ protected function setSet($ignoreNoRecord = false) $saveSelectStatement = $saveSelectStatement->execute(); $this->setTotalCount(new TotalCount($this->isIncludeTotalCount(),$saveSelectStatement->rowCount())); } - + $className = get_class($this->getBaseModel()); foreach ($selectStatement->fetchAll() as $result) { diff --git a/src/ModelSet.php b/src/ModelSet.php index ad07f99..d933880 100644 --- a/src/ModelSet.php +++ b/src/ModelSet.php @@ -186,7 +186,7 @@ public function setNoDefaultSorting($noDefaultSorting) */ public function isIncludeTotalCount() { - return $this->getTotalCount()->isIncludeCount(); + return $this->getTotalCount() ? $this->getTotalCount()->isIncludeCount() : false; } /** From 123870841756f11f870555080ed9b5e67b20c705 Mon Sep 17 00:00:00 2001 From: Ho-Ling Poon Date: Thu, 1 Mar 2018 13:15:37 -0500 Subject: [PATCH 08/26] Fixed formatting. --- src/Model.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Model.php b/src/Model.php index b9c081e..6335c9a 100644 --- a/src/Model.php +++ b/src/Model.php @@ -69,6 +69,7 @@ public function jsonSerialize() $jsonArray[$this->getJsonObjectName($objectName)] = $this->getJsonObjectValue($objectValue); } } + return $jsonArray; } From 62793f03f0c9466365244bc02bcee46d10b51076 Mon Sep 17 00:00:00 2001 From: Ho-Ling Poon Date: Thu, 1 Mar 2018 13:58:48 -0500 Subject: [PATCH 09/26] Showing totalCount only as a single number or null on API response. --- src/Model/Response/SuccessResponse.php | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/Model/Response/SuccessResponse.php b/src/Model/Response/SuccessResponse.php index 8bc9d66..4c8b9b6 100644 --- a/src/Model/Response/SuccessResponse.php +++ b/src/Model/Response/SuccessResponse.php @@ -18,7 +18,7 @@ abstract class SuccessResponse extends Response public $count = 0; /** - * @var TotalCount + * @var int */ public $totalCount; @@ -50,7 +50,9 @@ public function initializeResponse($model, $totalCount = null) if (is_array($model)) { $this->setCount(count($model)); - $this->setTotalCount($totalCount); + if ($totalCount) { + $this->setTotalCount($totalCount->getCount()); + } } else { $this->setCount(1); } @@ -105,7 +107,7 @@ public function setCount($count) } /** - * @return TotalCount + * @return int */ public function getTotalCount() { @@ -113,7 +115,7 @@ public function getTotalCount() } /** - * @param TotalCount $totalCount + * @param int $totalCount */ public function setTotalCount($totalCount) { From c6f6651f1d3e6bdc095e983a13d95c0d91c0ff16 Mon Sep 17 00:00:00 2001 From: Ho-Ling Poon Date: Thu, 1 Mar 2018 14:50:11 -0500 Subject: [PATCH 10/26] First refactoring of obtainTotalCount. --- src/Model/ModelTrait/DBReadTrait.php | 29 +++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/src/Model/ModelTrait/DBReadTrait.php b/src/Model/ModelTrait/DBReadTrait.php index 31177fb..cb06967 100644 --- a/src/Model/ModelTrait/DBReadTrait.php +++ b/src/Model/ModelTrait/DBReadTrait.php @@ -292,13 +292,7 @@ protected function setSet($ignoreNoRecord = false) if ($selectStatement->rowCount()) { if ($this->isIncludeTotalCount() == true) { - $saveSelectStatement = DB::getDatabase()->select() - ->from($baseModel->translateDbName($baseModel->getTableName())); - if ($this->getFilters()) { - $this->applyFilters($this->getFilters(), $saveSelectStatement); - } - $saveSelectStatement = $saveSelectStatement->execute(); - $this->setTotalCount(new TotalCount($this->isIncludeTotalCount(),$saveSelectStatement->rowCount())); + $this->obtainTotalCount(); } $className = get_class($this->getBaseModel()); @@ -348,6 +342,27 @@ protected function addOrderBy(SelectStatement $selectStatement) return true; } + /** + * @return bool + */ + protected function obtainTotalCount() + { + /** + * @var DBTrait $baseModel + */ + $baseModel = $this->getBaseModel(); + + $saveSelectStatement = DB::getDatabase()->select() + ->from($baseModel->translateDbName($baseModel->getTableName())); + if ($this->getFilters()) { + $this->applyFilters($this->getFilters(), $saveSelectStatement); + } + $saveSelectStatement = $saveSelectStatement->execute(); + $this->setTotalCount(new TotalCount($this->isIncludeTotalCount(),$saveSelectStatement->rowCount())); + + return true; + } + /** * @param bool $ignoreNoRecord * From b633d2066347058d3946c62a730adaea3d3bf03c Mon Sep 17 00:00:00 2001 From: Ho-Ling Poon Date: Thu, 1 Mar 2018 14:59:33 -0500 Subject: [PATCH 11/26] Renaming variable for easier reading and fitting local scope. --- src/Model/ModelTrait/DBReadTrait.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Model/ModelTrait/DBReadTrait.php b/src/Model/ModelTrait/DBReadTrait.php index cb06967..3f9192f 100644 --- a/src/Model/ModelTrait/DBReadTrait.php +++ b/src/Model/ModelTrait/DBReadTrait.php @@ -352,13 +352,13 @@ protected function obtainTotalCount() */ $baseModel = $this->getBaseModel(); - $saveSelectStatement = DB::getDatabase()->select() + $selectStatement = DB::getDatabase()->select() ->from($baseModel->translateDbName($baseModel->getTableName())); if ($this->getFilters()) { - $this->applyFilters($this->getFilters(), $saveSelectStatement); + $this->applyFilters($this->getFilters(), $selectStatement); } - $saveSelectStatement = $saveSelectStatement->execute(); - $this->setTotalCount(new TotalCount($this->isIncludeTotalCount(),$saveSelectStatement->rowCount())); + $selectStatement = $selectStatement->execute(); + $this->setTotalCount(new TotalCount($this->isIncludeTotalCount(),$selectStatement->rowCount())); return true; } From c21ee6883f0227759639ba5756fb5c464bfe09e9 Mon Sep 17 00:00:00 2001 From: Ho-Ling Poon Date: Thu, 1 Mar 2018 15:50:01 -0500 Subject: [PATCH 12/26] Removing PHPStorm autogenerated text. --- src/TotalCount.php | 7 ------- 1 file changed, 7 deletions(-) diff --git a/src/TotalCount.php b/src/TotalCount.php index 5ac1595..c647b22 100644 --- a/src/TotalCount.php +++ b/src/TotalCount.php @@ -1,14 +1,7 @@ Date: Thu, 1 Mar 2018 16:11:50 -0500 Subject: [PATCH 13/26] Removing APILogger not used in DBTrait. --- src/Model/ModelTrait/DBReadTrait.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Model/ModelTrait/DBReadTrait.php b/src/Model/ModelTrait/DBReadTrait.php index 3f9192f..f6a4189 100644 --- a/src/Model/ModelTrait/DBReadTrait.php +++ b/src/Model/ModelTrait/DBReadTrait.php @@ -2,7 +2,6 @@ namespace NYPL\Starter\Model\ModelTrait; use NYPL\Starter\APIException; -use NYPL\Starter\APILogger; use NYPL\Starter\DB; use NYPL\Starter\Filter; use NYPL\Starter\Filter\OrFilter; From 9bb45bf354a1c193963b8160c7a47d5be4c576fb Mon Sep 17 00:00:00 2001 From: Ho-Ling Poon Date: Fri, 2 Mar 2018 11:47:28 -0500 Subject: [PATCH 14/26] Removing TotalCount class. Moving total count related data back to ModelSet. Clearly assign boolean value to . --- src/Controller.php | 8 ++-- src/Model/ModelTrait/DBReadTrait.php | 5 ++- src/Model/Response/SuccessResponse.php | 7 +-- src/ModelSet.php | 25 ++++++++--- src/TotalCount.php | 60 -------------------------- 5 files changed, 31 insertions(+), 74 deletions(-) delete mode 100644 src/TotalCount.php diff --git a/src/Controller.php b/src/Controller.php index 6820d67..fa8061e 100644 --- a/src/Controller.php +++ b/src/Controller.php @@ -249,8 +249,10 @@ protected function getDefaultReadResponse( $model->setLimit($this->getRequest()->getParam('limit')); - if ($this->getRequest()->getParam('include_count') == 'true') { - $model->setTotalCount(new TotalCount($this->getRequest()->getParam('include_count'))); + $includeTotalCount = $this->getRequest()->getParam('includeTotalCount') === 'true' ? true : false ; + + if ($includeTotalCount === true) { + $model->setIncludeTotalCount($includeTotalCount); } if ($filter) { @@ -264,7 +266,7 @@ protected function getDefaultReadResponse( } $model->read(); - if ($this->getRequest()->getParam('include_count') == 'true') { + if ($includeTotalCount === true) { $response->initializeResponse($model->getData(), $model->getTotalCount()); } else { $response->initializeResponse($model->getData()); diff --git a/src/Model/ModelTrait/DBReadTrait.php b/src/Model/ModelTrait/DBReadTrait.php index f6a4189..cb0da42 100644 --- a/src/Model/ModelTrait/DBReadTrait.php +++ b/src/Model/ModelTrait/DBReadTrait.php @@ -2,6 +2,7 @@ namespace NYPL\Starter\Model\ModelTrait; use NYPL\Starter\APIException; +use NYPL\Starter\APILogger; use NYPL\Starter\DB; use NYPL\Starter\Filter; use NYPL\Starter\Filter\OrFilter; @@ -290,7 +291,7 @@ protected function setSet($ignoreNoRecord = false) } if ($selectStatement->rowCount()) { - if ($this->isIncludeTotalCount() == true) { + if ($this->isIncludeTotalCount() === true) { $this->obtainTotalCount(); } @@ -357,7 +358,7 @@ protected function obtainTotalCount() $this->applyFilters($this->getFilters(), $selectStatement); } $selectStatement = $selectStatement->execute(); - $this->setTotalCount(new TotalCount($this->isIncludeTotalCount(),$selectStatement->rowCount())); + $this->setTotalCount($selectStatement->rowCount()); return true; } diff --git a/src/Model/Response/SuccessResponse.php b/src/Model/Response/SuccessResponse.php index 4c8b9b6..cd9715e 100644 --- a/src/Model/Response/SuccessResponse.php +++ b/src/Model/Response/SuccessResponse.php @@ -3,6 +3,7 @@ use NYPL\Starter\Model; use NYPL\Starter\Model\Response; +use NYPL\Starter\ModelSet; use NYPL\Starter\TotalCount; abstract class SuccessResponse extends Response @@ -42,16 +43,16 @@ public function __construct($model = null, $code = 200) /** * @param Model|Model[] $model - * @param TotalCount $totalCount + * @param int $totalCount */ - public function initializeResponse($model, $totalCount = null) + public function initializeResponse($model, $totalCount = 0) { $this->setData($model); if (is_array($model)) { $this->setCount(count($model)); if ($totalCount) { - $this->setTotalCount($totalCount->getCount()); + $this->setTotalCount($totalCount); } } else { $this->setCount(1); diff --git a/src/ModelSet.php b/src/ModelSet.php index d933880..4f9f555 100644 --- a/src/ModelSet.php +++ b/src/ModelSet.php @@ -40,9 +40,14 @@ class ModelSet extends Model implements ReadInterface public $limit = 25; /** - * @var null|TotalCount + * @var int + */ + public $totalCount = 0; + + /** + * @var bool */ - public $totalCount = null; + public $includeTotalCount = false; /** * @var bool @@ -186,11 +191,19 @@ public function setNoDefaultSorting($noDefaultSorting) */ public function isIncludeTotalCount() { - return $this->getTotalCount() ? $this->getTotalCount()->isIncludeCount() : false; + return $this->includeTotalCount; } /** - * @return null|TotalCount + * @param bool $includeTotalCount + */ + public function setIncludeTotalCount($includeTotalCount) + { + $this->includeTotalCount = $includeTotalCount; + } + + /** + * @return int */ public function getTotalCount() { @@ -198,9 +211,9 @@ public function getTotalCount() } /** - * @param null|TotalCount $totalCount + * @param int $totalCount */ - public function setTotalCount($totalCount = null) + public function setTotalCount($totalCount = 0) { $this->totalCount = $totalCount; } diff --git a/src/TotalCount.php b/src/TotalCount.php deleted file mode 100644 index c647b22..0000000 --- a/src/TotalCount.php +++ /dev/null @@ -1,60 +0,0 @@ -setIncludeCount($includeCount); - } - - if ($count) { - $this->setCount($count); - } - - } - - /** - * @return bool - */ - public function isIncludeCount() - { - return $this->includeCount; - } - - /** - * @param bool $includeCount - */ - public function setIncludeCount($includeCount) - { - $this->includeCount = $includeCount; - } - - /** - * @return int - */ - public function getCount() - { - return $this->count; - } - - /** - * @param int $count - */ - public function setCount($count) - { - $this->count = $count; - } -} From 69958d090d3ad3f6b1ef017c1ee7ac2f59db86b5 Mon Sep 17 00:00:00 2001 From: Ho-Ling Poon Date: Fri, 2 Mar 2018 11:49:19 -0500 Subject: [PATCH 15/26] Removing mentions of TotalCount class. Saving ModelSet filter for next commit. --- src/Model/ModelTrait/DBReadTrait.php | 1 - src/Model/Response/SuccessResponse.php | 2 -- 2 files changed, 3 deletions(-) diff --git a/src/Model/ModelTrait/DBReadTrait.php b/src/Model/ModelTrait/DBReadTrait.php index cb0da42..46f704a 100644 --- a/src/Model/ModelTrait/DBReadTrait.php +++ b/src/Model/ModelTrait/DBReadTrait.php @@ -9,7 +9,6 @@ use NYPL\Starter\Model; use NYPL\Starter\ModelSet; use NYPL\Starter\OrderBy; -use NYPL\Starter\TotalCount; use Slim\PDO\Statement\SelectStatement; use Slim\PDO\Statement\StatementContainer; diff --git a/src/Model/Response/SuccessResponse.php b/src/Model/Response/SuccessResponse.php index cd9715e..7824a85 100644 --- a/src/Model/Response/SuccessResponse.php +++ b/src/Model/Response/SuccessResponse.php @@ -3,8 +3,6 @@ use NYPL\Starter\Model; use NYPL\Starter\Model\Response; -use NYPL\Starter\ModelSet; -use NYPL\Starter\TotalCount; abstract class SuccessResponse extends Response { From 44443f61013142c8c9253c011da907cc3b6afe31 Mon Sep 17 00:00:00 2001 From: Ho-Ling Poon Date: Fri, 2 Mar 2018 12:34:17 -0500 Subject: [PATCH 16/26] DBReadTrait::obtainTotalCount() refactored to use Slim\PDO\Statement\SelectStatement::count() aggregation feature, avoids returning full data set which can potentially crash API endpoint. --- src/Model/ModelTrait/DBReadTrait.php | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/Model/ModelTrait/DBReadTrait.php b/src/Model/ModelTrait/DBReadTrait.php index 46f704a..a7e4f6c 100644 --- a/src/Model/ModelTrait/DBReadTrait.php +++ b/src/Model/ModelTrait/DBReadTrait.php @@ -351,12 +351,15 @@ protected function obtainTotalCount() */ $baseModel = $this->getBaseModel(); - $selectStatement = DB::getDatabase()->select() - ->from($baseModel->translateDbName($baseModel->getTableName())); + $selectStatement = DB::getDatabase()->select(['id']) + ->from($baseModel->translateDbName($baseModel->getTableName())) + ->groupBy('id'); if ($this->getFilters()) { $this->applyFilters($this->getFilters(), $selectStatement); } - $selectStatement = $selectStatement->execute(); + + $selectStatement = $selectStatement->count('id')->execute(); + $this->setTotalCount($selectStatement->rowCount()); return true; From a961cb3123dc17f6e311500da7189498e1e73ed8 Mon Sep 17 00:00:00 2001 From: Ho-Ling Poon Date: Fri, 2 Mar 2018 13:08:38 -0500 Subject: [PATCH 17/26] Refactoring SuccessResponse to detect Model, Model[] and ModelSet. --- src/Controller.php | 6 +--- src/Model/Response/SuccessResponse.php | 47 +++++++++++++++++++++----- 2 files changed, 39 insertions(+), 14 deletions(-) diff --git a/src/Controller.php b/src/Controller.php index fa8061e..da14952 100644 --- a/src/Controller.php +++ b/src/Controller.php @@ -266,11 +266,7 @@ protected function getDefaultReadResponse( } $model->read(); - if ($includeTotalCount === true) { - $response->initializeResponse($model->getData(), $model->getTotalCount()); - } else { - $response->initializeResponse($model->getData()); - } + $response->initializeResponse($model); } else { if ($filter) { if ($filter->getId()) { diff --git a/src/Model/Response/SuccessResponse.php b/src/Model/Response/SuccessResponse.php index 7824a85..7cf0849 100644 --- a/src/Model/Response/SuccessResponse.php +++ b/src/Model/Response/SuccessResponse.php @@ -1,8 +1,10 @@ setData($model); if (is_array($model)) { - $this->setCount(count($model)); - if ($totalCount) { - $this->setTotalCount($totalCount); - } - } else { - $this->setCount(1); + $this->initializeArrayOfModels($model); + } else if ($model instanceof ModelSet) { + $this->initializeModelSet($model); + } else if ($model instanceof Model) { + $this->initializeModel($model); + } + } + + /** + * @param Model $model + */ + public function initializeModel($model) + { + APILogger::addDebug('Model'); + $this->setCount(1); + } + + /** + * @param Model[] $arrayOfModels + */ + public function initializeArrayOfModels($arrayOfModels) + { + APILogger::addDebug('Array Of Models'); + $this->setCount(count($arrayOfModels)); + } + + /** + * @param ModelSet $modelSet + */ + public function initializeModelSet($modelSet) + { + APILogger::addDebug('ModelSet'); + if ($modelSet->isIncludeTotalCount()) { + $this->setTotalCount($modelSet->getTotalCount()); } } From b0240dedfe8dbaf01e15ed7d15236c30923a13c8 Mon Sep 17 00:00:00 2001 From: Ho-Ling Poon Date: Fri, 2 Mar 2018 13:10:34 -0500 Subject: [PATCH 18/26] Removing Debug messages. --- src/Model/Response/SuccessResponse.php | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/Model/Response/SuccessResponse.php b/src/Model/Response/SuccessResponse.php index 7cf0849..be19e1c 100644 --- a/src/Model/Response/SuccessResponse.php +++ b/src/Model/Response/SuccessResponse.php @@ -62,7 +62,6 @@ public function initializeResponse($model) */ public function initializeModel($model) { - APILogger::addDebug('Model'); $this->setCount(1); } @@ -71,7 +70,6 @@ public function initializeModel($model) */ public function initializeArrayOfModels($arrayOfModels) { - APILogger::addDebug('Array Of Models'); $this->setCount(count($arrayOfModels)); } @@ -80,7 +78,6 @@ public function initializeArrayOfModels($arrayOfModels) */ public function initializeModelSet($modelSet) { - APILogger::addDebug('ModelSet'); if ($modelSet->isIncludeTotalCount()) { $this->setTotalCount($modelSet->getTotalCount()); } From 239099d0e06eea7af907d49d773d9527f3a047af Mon Sep 17 00:00:00 2001 From: Ho-Ling Poon Date: Fri, 2 Mar 2018 13:17:11 -0500 Subject: [PATCH 19/26] Only exposing data from ModelSet. --- src/Model/Response/SuccessResponse.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Model/Response/SuccessResponse.php b/src/Model/Response/SuccessResponse.php index be19e1c..dd5b2b4 100644 --- a/src/Model/Response/SuccessResponse.php +++ b/src/Model/Response/SuccessResponse.php @@ -46,13 +46,14 @@ public function __construct($model = null, $code = 200) */ public function initializeResponse($model) { - $this->setData($model); - if (is_array($model)) { + $this->setData($model); $this->initializeArrayOfModels($model); } else if ($model instanceof ModelSet) { + $this->setData($model->getData()); $this->initializeModelSet($model); } else if ($model instanceof Model) { + $this->setData($model); $this->initializeModel($model); } } From c8b6ce7e728cd032d1105afff297e44c20185466 Mon Sep 17 00:00:00 2001 From: Ho-Ling Poon Date: Fri, 2 Mar 2018 13:24:07 -0500 Subject: [PATCH 20/26] Refactor setData() to type specific initialization. --- src/Model/Response/SuccessResponse.php | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/Model/Response/SuccessResponse.php b/src/Model/Response/SuccessResponse.php index dd5b2b4..ea781d8 100644 --- a/src/Model/Response/SuccessResponse.php +++ b/src/Model/Response/SuccessResponse.php @@ -1,7 +1,6 @@ setData($model); $this->initializeArrayOfModels($model); } else if ($model instanceof ModelSet) { - $this->setData($model->getData()); $this->initializeModelSet($model); } else if ($model instanceof Model) { - $this->setData($model); $this->initializeModel($model); } } @@ -63,6 +59,7 @@ public function initializeResponse($model) */ public function initializeModel($model) { + $this->setData($model); $this->setCount(1); } @@ -71,6 +68,7 @@ public function initializeModel($model) */ public function initializeArrayOfModels($arrayOfModels) { + $this->setData($arrayOfModels); $this->setCount(count($arrayOfModels)); } @@ -79,6 +77,7 @@ public function initializeArrayOfModels($arrayOfModels) */ public function initializeModelSet($modelSet) { + $this->setData($modelSet->getData()); if ($modelSet->isIncludeTotalCount()) { $this->setTotalCount($modelSet->getTotalCount()); } From 92596a25683bd89819fd7d42a390f0f9c02bd7f3 Mon Sep 17 00:00:00 2001 From: Ho-Ling Poon Date: Fri, 2 Mar 2018 13:28:35 -0500 Subject: [PATCH 21/26] Show current size of ModelSet in count. --- src/Model/Response/SuccessResponse.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Model/Response/SuccessResponse.php b/src/Model/Response/SuccessResponse.php index ea781d8..4d59557 100644 --- a/src/Model/Response/SuccessResponse.php +++ b/src/Model/Response/SuccessResponse.php @@ -78,6 +78,7 @@ public function initializeArrayOfModels($arrayOfModels) public function initializeModelSet($modelSet) { $this->setData($modelSet->getData()); + $this->setCount(count($modelSet->getData())); if ($modelSet->isIncludeTotalCount()) { $this->setTotalCount($modelSet->getTotalCount()); } From fd3e7a48ec9e38628cdb6d04b869fa4489a5ceb3 Mon Sep 17 00:00:00 2001 From: Ho-Ling Poon Date: Fri, 2 Mar 2018 13:32:32 -0500 Subject: [PATCH 22/26] Adding detection of ModelSet in SuccessResponse's constructor. --- src/Model/Response/SuccessResponse.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Model/Response/SuccessResponse.php b/src/Model/Response/SuccessResponse.php index 4d59557..3411c52 100644 --- a/src/Model/Response/SuccessResponse.php +++ b/src/Model/Response/SuccessResponse.php @@ -28,7 +28,7 @@ abstract class SuccessResponse extends Response public $statusCode; /** - * @param Model|Model[] $model + * @param Model|Model[]|ModelSet $model * @param int $code */ public function __construct($model = null, $code = 200) From 0bc7451d29f158d83a7e50587c4937b2d3a0f814 Mon Sep 17 00:00:00 2001 From: Ho-Ling Poon Date: Mon, 5 Mar 2018 11:45:18 -0500 Subject: [PATCH 23/26] Remove redundant checking of true value for . --- src/Controller.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Controller.php b/src/Controller.php index da14952..93c2981 100644 --- a/src/Controller.php +++ b/src/Controller.php @@ -251,7 +251,7 @@ protected function getDefaultReadResponse( $includeTotalCount = $this->getRequest()->getParam('includeTotalCount') === 'true' ? true : false ; - if ($includeTotalCount === true) { + if ($includeTotalCount) { $model->setIncludeTotalCount($includeTotalCount); } From 6f591925ccf7dcc45e183e4f4afde7e991414c74 Mon Sep 17 00:00:00 2001 From: Ho-Ling Poon Date: Mon, 5 Mar 2018 11:46:31 -0500 Subject: [PATCH 24/26] Database lookup only returns count in obtainTotalCount(). --- src/Model/ModelTrait/DBReadTrait.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Model/ModelTrait/DBReadTrait.php b/src/Model/ModelTrait/DBReadTrait.php index a7e4f6c..34cd7e6 100644 --- a/src/Model/ModelTrait/DBReadTrait.php +++ b/src/Model/ModelTrait/DBReadTrait.php @@ -351,16 +351,16 @@ protected function obtainTotalCount() */ $baseModel = $this->getBaseModel(); - $selectStatement = DB::getDatabase()->select(['id']) - ->from($baseModel->translateDbName($baseModel->getTableName())) - ->groupBy('id'); + $selectStatement = DB::getDatabase()->select(['COUNT(*)']) + ->from($baseModel->translateDbName($baseModel->getTableName())); + if ($this->getFilters()) { $this->applyFilters($this->getFilters(), $selectStatement); } - $selectStatement = $selectStatement->count('id')->execute(); + $selectStatement = $selectStatement->count()->execute(); - $this->setTotalCount($selectStatement->rowCount()); + $this->setTotalCount($selectStatement->fetchColumn(0)); return true; } From 6eb5fee39801d8ad5b9f65f1255b888d9bdff687 Mon Sep 17 00:00:00 2001 From: Ho-Ling Poon Date: Mon, 5 Mar 2018 11:52:33 -0500 Subject: [PATCH 25/26] Refactoring nested if-else structure, using early return from function instead. Checking for ModelSet first to avoid returning baseModel in response. --- src/Model/Response/SuccessResponse.php | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/Model/Response/SuccessResponse.php b/src/Model/Response/SuccessResponse.php index 3411c52..ff8fe63 100644 --- a/src/Model/Response/SuccessResponse.php +++ b/src/Model/Response/SuccessResponse.php @@ -42,15 +42,25 @@ public function __construct($model = null, $code = 200) /** * @param Model|Model[]|ModelSet $model + * @return bool */ public function initializeResponse($model) { - if (is_array($model)) { - $this->initializeArrayOfModels($model); - } else if ($model instanceof ModelSet) { + // ModelSet needs to be checked first to avoid + // returning baseModel data in response + if ($model instanceof ModelSet) { $this->initializeModelSet($model); - } else if ($model instanceof Model) { + return true; + } + + if ($model instanceof Model) { $this->initializeModel($model); + return true; + } + + if (is_array($model)) { + $this->initializeArrayOfModels($model); + return true; } } From 4d82517c7c831387b98338827afa8c42193b6518 Mon Sep 17 00:00:00 2001 From: Ho-Ling Poon Date: Mon, 5 Mar 2018 11:53:15 -0500 Subject: [PATCH 26/26] Typecasting to ensure safe assignments. --- src/ModelSet.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ModelSet.php b/src/ModelSet.php index 4f9f555..c9087c3 100644 --- a/src/ModelSet.php +++ b/src/ModelSet.php @@ -199,7 +199,7 @@ public function isIncludeTotalCount() */ public function setIncludeTotalCount($includeTotalCount) { - $this->includeTotalCount = $includeTotalCount; + $this->includeTotalCount = (bool) $includeTotalCount; } /** @@ -215,6 +215,6 @@ public function getTotalCount() */ public function setTotalCount($totalCount = 0) { - $this->totalCount = $totalCount; + $this->totalCount = (int) $totalCount; } }