Skip to content
This repository has been archived by the owner on Jan 21, 2020. It is now read-only.

Add start_index and end_index #169

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Conversation

e-belair
Copy link

So you can show : Item [start_index] to [end_index] on [total_items]
or if you prefer : Item 20 to 29 on 100

@coveralls
Copy link

Coverage Status

Coverage increased (+0.1%) to 75.4% when pulling 04989a0 on manuscle:master into 6f84c05 on zfcampus:master.

];
$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']);
Copy link
Member

Choose a reason for hiding this comment

The 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:

  • what happens when both items are present in the request
  • what happens when only the start_index is present in the request (for instance, how does this interact with pagination?)
  • what happens when only the end_index is present in the request (is this valid? if so, how does it interact with pagination?)

@@ -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)
Copy link
Member

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.

? ($payload['page'] < $payload['page_count']
? (($payload['page_size'] * $payload['page']) - 1)
: $payload['total_items'])
: 0;
Copy link
Member

Choose a reason for hiding this comment

The 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.

Copy link
Author

@e-belair e-belair Jul 31, 2018

Choose a reason for hiding this comment

The 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?

Copy link
Member

Choose a reason for hiding this comment

The 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.

@Wilt
Copy link
Contributor

Wilt commented Sep 6, 2018

Should such logic not be a front end implementation?
I mean you have all the ingredients to calculate these "start_index" and "end_index" values on the front end side. I consider this interface specific (front-end) and IMO it is not the responsibility of the server to provide these values.

@weierophinney
Copy link
Member

This repository has been closed and moved to laminas-api-tools/api-tools-hal; a new issue has been opened at laminas-api-tools/api-tools-hal#4.

@weierophinney
Copy link
Member

This repository has been moved to laminas-api-tools/api-tools-hal. If you feel that this patch is still relevant, please re-open against that repository, and reference this issue. To re-open, we suggest the following workflow:

  • Squash all commits in your branch (git rebase -i origin/{branch})
  • Make a note of all changed files (`git diff --name-only origin/{branch}...HEAD
  • Run the laminas/laminas-migration tool on the code.
  • Clone laminas-api-tools/api-tools-hal to another directory.
  • Copy the files from the second bullet point to the clone of laminas-api-tools/api-tools-hal.
  • In your clone of laminas-api-tools/api-tools-hal, commit the files, push to your fork, and open the new PR.
    We will be providing tooling via laminas/laminas-migration soon to help automate the process.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants