Skip to content
This repository has been archived by the owner on May 7, 2021. It is now read-only.

Commit

Permalink
Abstract common functionality as protected methods in an abstract cla…
Browse files Browse the repository at this point in the history
…ss (#6)
  • Loading branch information
pavlakis authored Nov 16, 2020
1 parent 3e2ce83 commit 09a4207
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .php_cs.cache
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"php":"7.4.8","version":"2.16.4:v2.16.4#1023c3458137ab052f6ff1e09621a721bfdeca13","indent":" ","lineEnding":"\n","rules":{"blank_line_after_namespace":true,"braces":true,"class_definition":true,"constant_case":true,"elseif":true,"function_declaration":true,"indentation_type":true,"line_ending":true,"lowercase_keywords":true,"method_argument_space":{"on_multiline":"ensure_fully_multiline"},"no_break_comment":true,"no_closing_tag":true,"no_spaces_after_function_name":true,"no_spaces_inside_parenthesis":true,"no_trailing_whitespace":true,"no_trailing_whitespace_in_comment":true,"single_blank_line_at_eof":true,"single_class_element_per_statement":{"elements":["property"]},"single_import_per_statement":true,"single_line_after_imports":true,"switch_case_semicolon_to_colon":true,"switch_case_space":true,"visibility_required":true,"encoding":true,"full_opening_tag":true,"ordered_imports":{"sort_algorithm":"length"}},"hashes":{"src\/Repository\/AbstractPaginatorRepository.php":1476324894,"src\/Repository\/PaginationQueryManagerInterface.php":2483929998,"src\/Helper\/NativeSqlHelper.php":1346947594,"src\/Particle\/ParticleFilterFactory.php":3398098011,"tests\/phpunit\/Rule\/UpperCaseCharacterRuleTest.php":3187295278,"tests\/phpunit\/Rule\/MinimumLengthRuleTest.php":1503783452,"tests\/phpunit\/Rule\/AllRulesTest.php":1356751865,"tests\/phpunit\/Rule\/SpecialCharacterRuleTest.php":1220784808,"tests\/phpunit\/Rule\/DigitRuleTest.php":2593523549,"tests\/phpunit\/Format\/HumanReadablePolicyTest.php":2918699308,"tests\/phpunit\/Policy\/JsonPolicyTest.php":1409930937,"src\/Repository\/Filter.php":2062942533,"src\/Paginator.php":1549210670,"src\/Repository\/PaginatorConfigInterface.php":2959026211,"tests\/phpunit\/Repository\/FilterTest.php":3979093524,"tests\/phpunit\/GridPaginatorTest.php":2964267577,"src\/GridPaginator.php":630471002}}
{"php":"7.4.8","version":"2.16.4:v2.16.4#1023c3458137ab052f6ff1e09621a721bfdeca13","indent":" ","lineEnding":"\n","rules":{"blank_line_after_namespace":true,"braces":true,"class_definition":true,"constant_case":true,"elseif":true,"function_declaration":true,"indentation_type":true,"line_ending":true,"lowercase_keywords":true,"method_argument_space":{"on_multiline":"ensure_fully_multiline"},"no_break_comment":true,"no_closing_tag":true,"no_spaces_after_function_name":true,"no_spaces_inside_parenthesis":true,"no_trailing_whitespace":true,"no_trailing_whitespace_in_comment":true,"single_blank_line_at_eof":true,"single_class_element_per_statement":{"elements":["property"]},"single_import_per_statement":true,"single_line_after_imports":true,"switch_case_semicolon_to_colon":true,"switch_case_space":true,"visibility_required":true,"encoding":true,"full_opening_tag":true,"ordered_imports":{"sort_algorithm":"length"}},"hashes":{"src\/Repository\/AbstractPaginatorRepository.php":2036465871,"src\/Repository\/PaginationQueryManagerInterface.php":3740590114,"src\/Helper\/NativeSqlHelper.php":1346947594,"src\/Particle\/ParticleFilterFactory.php":3398098011,"tests\/phpunit\/Rule\/UpperCaseCharacterRuleTest.php":3187295278,"tests\/phpunit\/Rule\/MinimumLengthRuleTest.php":1503783452,"tests\/phpunit\/Rule\/AllRulesTest.php":1356751865,"tests\/phpunit\/Rule\/SpecialCharacterRuleTest.php":1220784808,"tests\/phpunit\/Rule\/DigitRuleTest.php":2593523549,"tests\/phpunit\/Format\/HumanReadablePolicyTest.php":2918699308,"tests\/phpunit\/Policy\/JsonPolicyTest.php":1409930937,"src\/Repository\/Filter.php":2062942533,"src\/Paginator.php":1549210670,"src\/Repository\/PaginatorConfigInterface.php":2959026211,"tests\/phpunit\/Repository\/FilterTest.php":3979093524,"tests\/phpunit\/GridPaginatorTest.php":2964267577,"src\/GridPaginator.php":630471002,"src\/Repository\/AbstractPaginationQueryManager.php":2155519270,"src\/Meta\/GridMetadata.php":1226617945,"src\/Meta\/MetadataInterface.php":4218364040}}
2 changes: 1 addition & 1 deletion .phpunit.result.cache
Original file line number Diff line number Diff line change
@@ -1 +1 @@
C:37:"PHPUnit\Runner\DefaultTestResultCache":1797:{a:2:{s:7:"defects";a:3:{s:7:"Warning";i:6;s:82:"StaySafe\Paginator\DataGrid\Test\Repository\FilterTest::can_add_multiple_mind_maps";i:4;s:115:"StaySafe\Paginator\DataGrid\Test\GridPaginatorTest::sanitize_grid_metadata_returns_empty_array_for_invalid_datatype";i:3;}s:5:"times";a:14:{s:7:"Warning";d:0.004;s:85:"StaySafe\Paginator\DataGrid\Test\Repository\FilterTest::invalid_type_throws_exception";d:0.003;s:82:"StaySafe\Paginator\DataGrid\Test\Repository\FilterTest::can_add_multiple_mind_maps";d:0;s:105:"StaySafe\Paginator\DataGrid\Test\Repository\FilterTest::can_add_bind_map_with_valid_type with data set #0";d:0.004;s:105:"StaySafe\Paginator\DataGrid\Test\Repository\FilterTest::can_add_bind_map_with_valid_type with data set #1";d:0.003;s:105:"StaySafe\Paginator\DataGrid\Test\Repository\FilterTest::can_add_bind_map_with_valid_type with data set #2";d:0.002;s:105:"StaySafe\Paginator\DataGrid\Test\Repository\FilterTest::can_add_bind_map_with_valid_type with data set #3";d:0.002;s:82:"StaySafe\Paginator\DataGrid\Test\Repository\FilterTest::can_add_multiple_bind_maps";d:0.002;s:115:"StaySafe\Paginator\DataGrid\Test\GridPaginatorTest::sanitize_grid_metadata_returns_empty_array_for_invalid_datatype";d:0.005;s:104:"StaySafe\Paginator\DataGrid\Test\GridPaginatorTest::sanitize_empty_grid_metadata_returns_valid_construct";d:0.009;s:98:"StaySafe\Paginator\DataGrid\Test\GridPaginatorTest::decode_submitted_search_query with data set #0";d:0.003;s:98:"StaySafe\Paginator\DataGrid\Test\GridPaginatorTest::decode_submitted_search_query with data set #1";d:0.002;s:98:"StaySafe\Paginator\DataGrid\Test\GridPaginatorTest::decode_submitted_search_query with data set #2";d:0.002;s:98:"StaySafe\Paginator\DataGrid\Test\GridPaginatorTest::decode_submitted_search_query with data set #3";d:0.002;}}}
C:37:"PHPUnit\Runner\DefaultTestResultCache":1777:{a:2:{s:7:"defects";a:3:{s:7:"Warning";i:6;s:82:"StaySafe\Paginator\DataGrid\Test\Repository\FilterTest::can_add_multiple_mind_maps";i:4;s:115:"StaySafe\Paginator\DataGrid\Test\GridPaginatorTest::sanitize_grid_metadata_returns_empty_array_for_invalid_datatype";i:3;}s:5:"times";a:14:{s:7:"Warning";d:0.004;s:85:"StaySafe\Paginator\DataGrid\Test\Repository\FilterTest::invalid_type_throws_exception";d:0.001;s:82:"StaySafe\Paginator\DataGrid\Test\Repository\FilterTest::can_add_multiple_mind_maps";d:0;s:105:"StaySafe\Paginator\DataGrid\Test\Repository\FilterTest::can_add_bind_map_with_valid_type with data set #0";d:0.002;s:105:"StaySafe\Paginator\DataGrid\Test\Repository\FilterTest::can_add_bind_map_with_valid_type with data set #1";d:0;s:105:"StaySafe\Paginator\DataGrid\Test\Repository\FilterTest::can_add_bind_map_with_valid_type with data set #2";d:0;s:105:"StaySafe\Paginator\DataGrid\Test\Repository\FilterTest::can_add_bind_map_with_valid_type with data set #3";d:0;s:82:"StaySafe\Paginator\DataGrid\Test\Repository\FilterTest::can_add_multiple_bind_maps";d:0;s:115:"StaySafe\Paginator\DataGrid\Test\GridPaginatorTest::sanitize_grid_metadata_returns_empty_array_for_invalid_datatype";d:0.005;s:104:"StaySafe\Paginator\DataGrid\Test\GridPaginatorTest::sanitize_empty_grid_metadata_returns_valid_construct";d:0.005;s:98:"StaySafe\Paginator\DataGrid\Test\GridPaginatorTest::decode_submitted_search_query with data set #0";d:0.001;s:98:"StaySafe\Paginator\DataGrid\Test\GridPaginatorTest::decode_submitted_search_query with data set #1";d:0.001;s:98:"StaySafe\Paginator\DataGrid\Test\GridPaginatorTest::decode_submitted_search_query with data set #2";d:0;s:98:"StaySafe\Paginator\DataGrid\Test\GridPaginatorTest::decode_submitted_search_query with data set #3";d:0.001;}}}
47 changes: 47 additions & 0 deletions src/Repository/AbstractPaginationQueryManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);

namespace StaySafe\Paginator\DataGrid\Repository;

abstract class AbstractPaginationQueryManager implements PaginationQueryManagerInterface
{
private array $columns = [];

/**
* @param string $name
* @param mixed $value
*
* @return $this
*/
public function addColumn(string $name, $value): self
{
$this->columns[$name] = $value;

return $this;
}

/**
* @param string $name
*
* @return bool
*/
public function hasColumn(string $name): bool
{
return array_key_exists($name, $this->columns);
}

/**
* @param string $name
*
* @return mixed|null
*/
public function getColumn(string $name)
{
if ($this->hasColumn($name)) {
return $this->columns[$name];
}

return null;
}
}
20 changes: 13 additions & 7 deletions src/Repository/AbstractPaginatorRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,16 @@ protected function getPaginationCount(string $countSQL, Filter $filters): int
$statement->execute();
$countResult = $statement->fetch();

return (int) $countResult['total'] ?: 0;
return ($statement->rowCount() > 0) ? (int) $countResult['total'] : 0;
}

/**
* @param string $itemsSQL
* @param Filter $filters
* @param array $sort
* @param int $page
* @param int $limit
* @param string $itemsSQL
* @param Filter $filters
* @param array $sort
* @param int $page
* @param int $limit
* @param string|null $groupBy
*
* @return array
*
Expand All @@ -61,11 +62,16 @@ protected function getPaginationItemsResult(
Filter $filters,
array $sort,
int $page,
int $limit
int $limit,
?string $groupBy = null
): array {
// filters
$itemsSQL .= $filters->getFilterString();

if (null !== $groupBy) {
$itemsSQL .= $groupBy;
}

// Sort
$itemsSQL .= $this->nativeSqlHelper->getSortString($sort);

Expand Down
22 changes: 22 additions & 0 deletions src/Repository/PaginationQueryManagerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,26 @@
interface PaginationQueryManagerInterface
{
public function getPaginatedData(int $limit, int $page, array $sort, array $filterableFields, array $filter = []): array;

/**
* @param string $name
* @param mixed $value
*
* @return $this
*/
public function addColumn(string $name, $value): self;

/**
* @param string $name
*
* @return bool
*/
public function hasColumn(string $name): bool;

/**
* @param string $name
*
* @return mixed|null
*/
public function getColumn(string $name);
}

0 comments on commit 09a4207

Please sign in to comment.