Skip to content

Comments

Document what count() is supposed to return#1468

Merged
dunglas merged 2 commits intoapi-platform:2.1from
greg0ire:document_countable
Nov 2, 2017
Merged

Document what count() is supposed to return#1468
dunglas merged 2 commits intoapi-platform:2.1from
greg0ire:document_countable

Conversation

@greg0ire
Copy link
Contributor

Q A
Bug fix? no
New feature? no
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets none
License MIT
Doc PR none

count() could return the number of pages, or the total number of items, or the number of items on the current page. Looking at the doctrine ORM implementation, it looks like it should be the number of items on the current page, so let's document that, maybe it is relied on somewhere?

I'm contributing this on 2.1, but on master the implementation was moved in an abstract class. Here is how it looks:

public function count(): int
{
return iterator_count($this->getIterator());
}


/**
* Paginator Interface.
* Paginator Interface. The \Countable implementation should return the number
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can even remove Paginator Interface.. This comments brings nothing.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dunglas done

Grégoire Paris added 2 commits October 31, 2017 15:49
count() could return the number of pages, or the total number of items,
or the number of items on the current page.
Looking at the doctrine ORM implementation, it looks like it should be
the number of items on the current page, so let's document that, maybe
it is relied on somewhere?
@greg0ire
Copy link
Contributor Author

greg0ire commented Oct 31, 2017

Does anyone know when this method is supposed to be called by the way? If it is unused, I think it should be removed. I tried throwing an exception in it, and I have yet to see any page break.

@dunglas
Copy link
Member

dunglas commented Oct 31, 2017

@greg0ire
Copy link
Contributor Author

greg0ire commented Oct 31, 2017

@dunglas I think you found a bug, because the result is assigned to $data['hydra:totalItems'], but really is the number of items on the current page (at least that what it seems to be based on my tests).

EDIT: Oh sorry I didn't read the whole condition, this is not a bug, it makes some sense.

$data['hydra:totalItems'] = $paginated ? $object->getTotalItems() : count($object);

@greg0ire
Copy link
Contributor Author

@dunglas but if the object is not $paginated, it does not implement the interface... so... try again :P ?

@greg0ire greg0ire mentioned this pull request Oct 31, 2017
@dunglas dunglas merged commit 32f65b7 into api-platform:2.1 Nov 2, 2017
@dunglas
Copy link
Member

dunglas commented Nov 2, 2017

Thanks @greg0ire

@greg0ire greg0ire deleted the document_countable branch November 2, 2017 16:18
hoangnd25 pushed a commit to hoangnd25/core that referenced this pull request Feb 23, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants