Skip to content

Commit

Permalink
Merge pull request #3 from fulll/feat-php8.1
Browse files Browse the repository at this point in the history
  • Loading branch information
crozet-magenta authored Jun 20, 2024
2 parents 48db025 + 29c734e commit b748864
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 182 deletions.
4 changes: 3 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
language: php

dist: "focal"

php:
- 7.4
- 8.0
- 8.1

env:
global:
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@
"homepage": "https://github.com/stephpy/timeline/contributors"
}],
"require": {
"php": ">=7.4",
"php": ">=8.0",
"symfony/options-resolver": "*",
"doctrine/common": "~2.2|~3.0"
},
"require-dev": {
"atoum/atoum": "~4.0",
"pimple/pimple": "*",
"knplabs/knp-components": "1.*",
"knplabs/knp-components": "4.*",
"symfony/event-dispatcher": "*"
},
"suggest": {
Expand Down
75 changes: 18 additions & 57 deletions src/Driver/Redis/Pager/Pager.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,12 @@

class Pager extends AbstractPager implements PagerInterface, \IteratorAggregate, \Countable, \ArrayAccess
{
/**
* @var integer
*/
protected $page;

/**
* {@inheritdoc}
*/
public function paginate($target, $page = 1, $limit = 10, $options = array())
protected int $page;
private array $items;
private int $nbResults;
private int $lastPage;

public function paginate($target, int $page = 1, int $limit = 10, array $options = []): static
{
if (!$target instanceof PagerToken) {
throw new \Exception('Not supported, must give a PagerToken');
Expand All @@ -33,67 +30,42 @@ public function paginate($target, $page = 1, $limit = 10, $options = array())
return $this;
}

/**
* {@inheritdoc}
*/
public function getLastPage()
public function getLastPage(): int
{
return $this->lastPage;
}

/**
* {@inheritdoc}
*/
public function getPage()
public function getPage(): int
{
return $this->page;
}

/**
* {@inheritdoc}
*/
public function haveToPaginate()
public function haveToPaginate(): bool
{
return $this->getLastPage() > 1;
}

/**
* {@inheritdoc}
*/
public function getNbResults()
public function getNbResults(): int
{
return $this->nbResults;
}

/**
* @param array $items items
*/
public function setItems(array $items)
public function setItems(array $items): void
{
$this->items = $items;
}

/**
* @return \ArrayIterator
*/
public function getIterator()
public function getIterator(): \Traversable
{
return new \ArrayIterator($this->items);
}

/**
* @return integer
*/
public function count()
public function count(): int
{
return count($this->items);
}

/**
* @param mixed $offset
* @param mixed $value
*/
public function offsetSet($offset, $value)
public function offsetSet(mixed $offset, mixed $value): void
{
if (is_null($offset)) {
$this->items[] = $value;
Expand All @@ -102,29 +74,18 @@ public function offsetSet($offset, $value)
}
}

/**
* @param mixed $offset
* @return boolean
*/
public function offsetExists($offset)
public function offsetExists(mixed $offset): bool
{
return isset($this->items[$offset]);
}

/**
* @param mixed $offset
*/
public function offsetUnset($offset)
public function offsetUnset(mixed $offset): void
{
unset($this->items[$offset]);
}

/**
* @param mixed $offset
* @return mixed
*/
public function offsetGet($offset)
public function offsetGet(mixed $offset): mixed
{
return isset($this->items[$offset]) ? $this->items[$offset] : null;
return $this->items[$offset] ?? null;
}
}
37 changes: 10 additions & 27 deletions src/ResultBuilder/Pager/AbstractPager.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,29 @@

namespace Spy\Timeline\ResultBuilder\Pager;

use Knp\Component\Pager\Pagination\AbstractPagination;

abstract class AbstractPager implements \ArrayAccess
{
/**
* @var array
*/
protected $pager;
protected ?AbstractPagination $pager = null;

/**
* {@inheritdoc}
*/
public function offsetExists($offset): bool
{
return array_key_exists($offset, $this->pager);
return $this->pager->offsetExists($offset);
}

/**
* {@inheritdoc}
*/
public function offsetGet($offset)
public function offsetGet(mixed $offset): mixed
{
return $this->pager[$offset];
return $this->pager->offsetGet($offset);
}

/**
* {@inheritdoc}
*/
public function offsetSet($offset, $value)
public function offsetSet(mixed $offset, mixed $value): void
{
if (null === $offset) {
$this->pager[] = $value;
} else {
$this->pager[$offset] = $value;
}
$this->pager->offsetSet($offset, $value);
}

/**
* {@inheritdoc}
*/
public function offsetUnset($offset)
public function offsetUnset(mixed $offset): void
{
unset($this->pager[$offset]);
$this->pager->offsetUnset($offset);
}
}
68 changes: 14 additions & 54 deletions src/ResultBuilder/Pager/KnpPager.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,19 @@

namespace Spy\Timeline\ResultBuilder\Pager;

use Knp\Component\Pager\Paginator;
use Knp\Component\Pager\PaginatorInterface;

class KnpPager extends AbstractPager implements PagerInterface, \IteratorAggregate, \Countable
{
/**
* @var Paginator
*/
protected $paginator;
protected int $page;
protected array $data;

/**
* @var integer
*/
protected $page;

/**
* @var array
*/
protected $data;

/**
* @param Paginator|null $paginator paginator
*/
public function __construct(Paginator $paginator = null)
{
$this->paginator = $paginator;
public function __construct(
protected ?PaginatorInterface $paginator = null
) {
}

/**
* {@inheritdoc}
*/
public function paginate($target, $page = 1, $limit = 10)
public function paginate(mixed $target, int $page = 1, int $limit = 10): static
{
if (null === $this->paginator) {
throw new \LogicException(sprintf('Knp\Component\Pager\Paginator not injected in constructor of %s', __CLASS__));
Expand All @@ -45,43 +27,27 @@ public function paginate($target, $page = 1, $limit = 10)
return $this;
}

/**
* {@inheritdoc}
*/
public function getPage()
public function getPage(): int
{
return $this->page;
}

/**
* {@inheritdoc}
*/
public function getLastPage()
public function getLastPage(): int
{
return $this->data['last'];
}

/**
* {@inheritdoc}
*/
public function haveToPaginate()
public function haveToPaginate(): bool
{
return $this->getLastPage() > 1;
}

/**
* {@inheritdoc}
*/
public function getNbResults()
public function getNbResults(): int
{
return $this->data['totalCount'];
}

/**
* @param array $items items
* @throws \Exception
*/
public function setItems(array $items)
public function setItems(array $items): void
{
if (!$this->pager) {
throw new \Exception('Paginate before set items');
Expand All @@ -90,18 +56,12 @@ public function setItems(array $items)
$this->pager->setItems($items);
}

/**
* @return \ArrayIterator
*/
public function getIterator()
public function getIterator(): \Traversable
{
return $this->pager;
}

/**
* @return integer
*/
public function count()
public function count(): int
{
return $this->data['currentItemCount'];
}
Expand Down
34 changes: 6 additions & 28 deletions src/ResultBuilder/Pager/PagerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,15 @@

interface PagerInterface
{
/**
* @param mixed $target target
* @param int $page page
* @param int $limit limit
*
* @return mixed
*/
public function paginate($target, $page = 1, $limit = 10);
public function paginate(mixed $target, int $page = 1, int $limit = 10);

/**
* @return integer
*/
public function getPage();
public function getPage(): int;

/**
* @return integer
*/
public function getLastPage();
public function getLastPage(): int;

/**
* @return boolean
*/
public function haveToPaginate();
public function haveToPaginate(): bool;

/**
* @return integer
*/
public function getNbResults();
public function getNbResults(): int;

/**
* @param array $items items
*/
public function setItems(array $items);
public function setItems(array $items): void;
}
5 changes: 1 addition & 4 deletions src/Spread/Entry/EntryCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,7 @@ public function setActionManager(ActionManagerInterface $actionManager)
$this->actionManager = $actionManager;
}

/**
* @return \ArrayIterator
*/
public function getIterator()
public function getIterator(): \Traversable
{
return $this->coll;
}
Expand Down
Loading

0 comments on commit b748864

Please sign in to comment.