-
Notifications
You must be signed in to change notification settings - Fork 49
Add start_index and end_index #169
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -605,6 +605,14 @@ public function renderCollection(Collection $halCollection) | |
$payload['page'] = ($payload['page_count'] > 0) | ||
? $halCollection->getPage() | ||
: 0; | ||
$payload['start_index'] = $payload['page'] > 0 | ||
? ($payload['page_size'] * ($payload['page'] - 1)?:1) | ||
: 0; | ||
$payload['end_index'] = $payload['page'] > 0 | ||
? ($payload['page'] < $payload['page_count'] | ||
? (($payload['page_size'] * $payload['page']) - 1) | ||
: $payload['total_items']) | ||
: 0; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is pretty convoluted and hard to follow. Maybe write a dedicated method for calculating the value? That way you can return early when specific conditions are met. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you think it should be better to get values from Paginator instead of Hal plugin? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IIRC, we use the values in the zf-hal configuration to seed the paginator. That said, the total number of items and number of pages is generally from the paginator, so getting those from that would likely be the best idea. |
||
} elseif (is_array($collection) || $collection instanceof Countable) { | ||
$payload['total_items'] = isset($payload['total_items']) ? $payload['total_items'] : count($collection); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1010,12 +1010,16 @@ public function testRenderingPaginatorCollectionRendersPaginationAttributes() | |
'page_size', | ||
'total_items', | ||
'page', | ||
'start_index', | ||
'end_index', | ||
]; | ||
$this->assertEquals($expected, array_keys($rendered)); | ||
$this->assertEquals(100, $rendered['total_items']); | ||
$this->assertEquals(3, $rendered['page']); | ||
$this->assertEquals(10, $rendered['page_count']); | ||
$this->assertEquals(10, $rendered['page_size']); | ||
$this->assertEquals(20, $rendered['start_index']); | ||
$this->assertEquals(29, $rendered['end_index']); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. While this correctly ensures that the newly introduced attributes are present, we also need tests covering what happens in terms of which items are in the collection when those attributes are present in the initial request. Ideally, all of these, including the above assertions, should be in a separate tests to ensure that existing behavior remains unchanged, while the new behavior is also present. I'd have tests demonstrating:
|
||
return $rendered; | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add spaces around the
?:
operator, please.