Skip to content

Commit

Permalink
collection: added typehints (BC break!)
Browse files Browse the repository at this point in the history
  • Loading branch information
hrach committed Jan 14, 2017
1 parent 0842eea commit 4829fc0
Show file tree
Hide file tree
Showing 15 changed files with 75 additions and 78 deletions.
24 changes: 12 additions & 12 deletions src/Collection/ArrayCollection.php
Expand Up @@ -60,7 +60,7 @@ public function getBy(array $where)
}


public function findBy(array $where)
public function findBy(array $where): ICollection
{
$collection = clone $this;
foreach ($where as $column => $value) {
Expand All @@ -70,7 +70,7 @@ public function findBy(array $where)
}


public function orderBy($column, $direction = self::ASC)
public function orderBy($column, string $direction = self::ASC): ICollection
{
$collection = clone $this;
if (is_array($column)) {
Expand All @@ -84,15 +84,15 @@ public function orderBy($column, $direction = self::ASC)
}


public function resetOrderBy()
public function resetOrderBy(): ICollection
{
$collection = clone $this;
$collection->collectionSorter = [];
return $collection;
}


public function limitBy($limit, $offset = null)
public function limitBy(int $limit, int $offset = null): ICollection
{
$collection = clone $this;
$collection->collectionLimit = [$limit, $offset];
Expand Down Expand Up @@ -121,7 +121,7 @@ public function fetchAll()
}


public function fetchPairs($key = null, $value = null)
public function fetchPairs(string $key = null, string $value = null): array
{
return FetchPairsHelper::process($this->getIterator(), $key, $value);
}
Expand All @@ -147,7 +147,7 @@ public function getIterator()
}


public function getEntityIterator(IEntity $parent = null)
public function getEntityIterator(IEntity $parent = null): Iterator
{
if ($parent && $this->relationshipMapper) {
$collection = clone $this;
Expand All @@ -162,33 +162,33 @@ public function getEntityIterator(IEntity $parent = null)
}


public function count()
public function count(): int
{
return $this->getEntityCount($this->relationshipParent);
}


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


public function getEntityCount(IEntity $parent = null)
public function getEntityCount(IEntity $parent = null): int
{
return count($this->getEntityIterator($parent));
}


public function setRelationshipMapping(IRelationshipMapper $mapper = null, IEntity $parent = null)
public function setRelationshipMapping(IRelationshipMapper $mapper = null, IEntity $parent = null): ICollection
{
$this->relationshipMapper = $mapper;
$this->relationshipParent = $parent;
return $this;
}


public function getRelationshipMapper()
public function getRelationshipMapper(): IRelationshipMapper
{
return $this->relationshipMapper;
}
Expand All @@ -214,7 +214,7 @@ protected function processData()
}

if ($this->collectionLimit) {
$data = array_slice($data, $this->collectionLimit[1], $this->collectionLimit[0]);
$data = array_slice($data, $this->collectionLimit[1] ?: 0, $this->collectionLimit[0]);
}

$this->collectionFilter = [];
Expand Down
28 changes: 15 additions & 13 deletions src/Collection/EmptyCollection.php
Expand Up @@ -8,6 +8,8 @@

namespace Nextras\Orm\Collection;

use EmptyIterator;
use Iterator;
use Nextras\Orm\Entity\IEntity;
use Nextras\Orm\Mapper\IRelationshipMapper;

Expand All @@ -24,25 +26,25 @@ public function getBy(array $where)
}


public function findBy(array $where)
public function findBy(array $where): ICollection
{
return clone $this;
}


public function orderBy($column, $direction = self::ASC)
public function orderBy($column, string $direction = self::ASC): ICollection
{
return clone $this;
}


public function resetOrderBy()
public function resetOrderBy(): ICollection
{
return clone $this;
}


public function limitBy($limit, $offset = null)
public function limitBy(int $limit, int $offset = null): ICollection
{
return clone $this;
}
Expand All @@ -60,7 +62,7 @@ public function fetchAll()
}


public function fetchPairs($key = null, $value = null)
public function fetchPairs(string $key = null, string $value = null): array
{
return [];
}
Expand All @@ -75,42 +77,42 @@ public function toCollection($resetOrderBy = false)

public function getIterator()
{
return new \EmptyIterator();
return new EmptyIterator();
}


public function getEntityIterator(IEntity $parent = null)
public function getEntityIterator(IEntity $parent = null): Iterator
{
return new \EmptyIterator();
return new EmptyIterator();
}


public function getEntityCount(IEntity $parent = null)
public function getEntityCount(IEntity $parent = null): int
{
return 0;
}


public function setRelationshipMapping(IRelationshipMapper $mapper = null, IEntity $parent = null)
public function setRelationshipMapping(IRelationshipMapper $mapper = null, IEntity $parent = null): ICollection
{
$this->relationshipMapper = $mapper;
return $this;
}


public function getRelationshipMapper()
public function getRelationshipMapper(): IRelationshipMapper
{
return $this->relationshipMapper;
}


public function countStored()
public function countStored(): int
{
return 0;
}


public function count()
public function count(): int
{
return 0;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Collection/EntityContainer.php
Expand Up @@ -36,7 +36,7 @@ public function getEntity($key)
}


public function getPreloadValues($property)
public function getPreloadValues(string $property): array
{
if (isset($this->preloadCache[$property])) {
return $this->preloadCache[$property];
Expand Down
2 changes: 1 addition & 1 deletion src/Collection/EntityIterator.php
Expand Up @@ -98,7 +98,7 @@ public function count()
}


public function getPreloadValues($property)
public function getPreloadValues(string $property): array
{
if (isset($this->preloadCache[$property])) {
return $this->preloadCache[$property];
Expand Down
42 changes: 13 additions & 29 deletions src/Collection/ICollection.php
Expand Up @@ -9,10 +9,10 @@
namespace Nextras\Orm\Collection;

use Countable;
use Iterator;
use IteratorAggregate;
use Nextras\Orm\Entity\IEntity;
use Nextras\Orm\Mapper\IRelationshipMapper;
use Traversable;


interface ICollection extends IteratorAggregate, Countable
Expand All @@ -35,36 +35,29 @@ public function getBy(array $where);
/**
* Returns entity collection filtered by conditions.
* Returns new instance of collection.
* @param array $where
* @return ICollection
*/
public function findBy(array $where);
public function findBy(array $where): ICollection;


/**
* Selects columns to order by.
* Returns new instance of collection.
* @param string|array $column column name or array of column names
* @param string $direction sorting direction self::ASC or self::DESC
* @return ICollection
*/
public function orderBy($column, $direction = self::ASC);
public function orderBy($column, string $direction = self::ASC): ICollection;


/**
* Resets collection ordering.
* @return ICollection
*/
public function resetOrderBy();
public function resetOrderBy(): ICollection;


/**
* Limits number of rows.
* @param int $limit
* @param int $offset
* @return ICollection
*/
public function limitBy($limit, $offset = null);
public function limitBy(int $limit, int $offset = null): ICollection;


/**
Expand All @@ -83,25 +76,19 @@ public function fetchAll();

/**
* Fetches all records like $key => $value pairs.
* @param string $key associative key
* @param string $value value
* @return array
* @param string|null $key associative key
* @param string|null $value value
*/
public function fetchPairs($key = null, $value = null);
public function fetchPairs(string $key = null, string $value = null): array;


/**
* @param IEntity|null $parent
* @return Traversable
*/
public function getEntityIterator(IEntity $parent = null);
public function getEntityIterator(IEntity $parent = null): Iterator;


/**
* @param IEntity|null $parent
* @return int
*/
public function getEntityCount(IEntity $parent = null);
public function getEntityCount(IEntity $parent = null): int;


/**
Expand All @@ -110,22 +97,19 @@ public function getEntityCount(IEntity $parent = null);
* @ignore
* @param IRelationshipMapper|null $mapper
* @param IEntity|null $parent
* @return self
*/
public function setRelationshipMapping(IRelationshipMapper $mapper = null, IEntity $parent = null);
public function setRelationshipMapping(IRelationshipMapper $mapper = null, IEntity $parent = null): ICollection;


/**
* @internal
* @ignore
* @return IRelationshipMapper
*/
public function getRelationshipMapper();
public function getRelationshipMapper(): IRelationshipMapper;


/**
* Counts collection entities without fetching them from storage.
* @return int
*/
public function countStored();
public function countStored(): int;
}
2 changes: 1 addition & 1 deletion src/Collection/IEntityContainer.php
Expand Up @@ -13,7 +13,7 @@ interface IEntityContainer extends IEntityPreloadContainer
{
/**
* Returms entity by joining key.
* @param int $key
* @param int $key
*/
public function getEntity($key);
}
3 changes: 2 additions & 1 deletion src/Collection/IEntityIterator.php
Expand Up @@ -13,7 +13,8 @@ interface IEntityIterator extends IEntityPreloadContainer, \Iterator, \Countable
{
/**
* Sets index for inner hasMany collections.
* @param int|null $index
* @param int|null $index
* @return void
*/
public function setDataIndex($index);
}
4 changes: 1 addition & 3 deletions src/Collection/IEntityPreloadContainer.php
Expand Up @@ -13,8 +13,6 @@ interface IEntityPreloadContainer
{
/**
* Returns array of $property values for preloading.
* @param string $property
* @return array
*/
public function getPreloadValues($property);
public function getPreloadValues(string $property): array;
}

0 comments on commit 4829fc0

Please sign in to comment.