-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Collection improvements (#94) * Added common AbstractCollection * CR Fixes * Migrating ResultCollection * Improvements in chain searching * CellCollection introduced * Added missing PHPDocs * Added PHPDoc and documentation * Composer improvements * Documentation typo fix * PSR-2 code style fixes. Scrolling Searching Context * Lowest version of ruflin/elastica upgraded to 2.1 * Added example QueryBuilderSearchingContext definition * Added dummy example of KnpPaginatorAdapter configuration and usage * Added warning about changing SearchingContext class
- Loading branch information
1 parent
d14d29d
commit 0e0537e
Showing
51 changed files
with
779 additions
and
353 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
<?php | ||
|
||
namespace KGzocha\Searcher; | ||
|
||
/** | ||
* @author Krzysztof Gzocha <krzysztof@propertyfinder.ae> | ||
*/ | ||
abstract class AbstractCollection implements \Countable, \IteratorAggregate | ||
{ | ||
/** | ||
* @var array | ||
*/ | ||
private $items; | ||
|
||
/** | ||
* @param \Traversable|array $items | ||
*/ | ||
public function __construct($items = []) | ||
{ | ||
$this->items = []; | ||
$this->isTraversable($items); | ||
$this->checkItems($items); | ||
|
||
$this->items = $items; | ||
} | ||
|
||
/** | ||
* @param object $item | ||
* | ||
* @return bool | ||
*/ | ||
abstract protected function isItemValid($item); | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getIterator() | ||
{ | ||
return new \ArrayIterator($this->items); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function count() | ||
{ | ||
return count($this->items); | ||
} | ||
|
||
/** | ||
* @param mixed $item | ||
* | ||
* @return $this | ||
*/ | ||
protected function addItem($item) | ||
{ | ||
$this->items[] = $item; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @param string $name | ||
* @param mixed $item | ||
* | ||
* @return $this | ||
*/ | ||
protected function addNamedItem($name, $item) | ||
{ | ||
$this->items[$name] = $item; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
protected function getItems() | ||
{ | ||
return $this->items; | ||
} | ||
|
||
/** | ||
* @param string $name | ||
* | ||
* @return mixed|null | ||
*/ | ||
protected function getNamedItem($name) | ||
{ | ||
if (array_key_exists($name, $this->items)) { | ||
return $this->items[$name]; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
/** | ||
* @param mixed $items | ||
* | ||
* @throws \InvalidArgumentException | ||
*/ | ||
private function isTraversable($items) | ||
{ | ||
if (is_array($items)) { | ||
return; | ||
} | ||
|
||
if ($items instanceof \Traversable) { | ||
return; | ||
} | ||
|
||
throw new \InvalidArgumentException(sprintf( | ||
'Argument passed to collection %s needs to be an array or traversable object', | ||
get_class($this) | ||
)); | ||
} | ||
|
||
/** | ||
* @param array|\Traversable $items | ||
* | ||
* @throws \InvalidArgumentException | ||
*/ | ||
private function checkItems($items) | ||
{ | ||
foreach ($items as $item) { | ||
if ($this->isItemValid($item)) { | ||
continue; | ||
} | ||
|
||
throw new \InvalidArgumentException(sprintf( | ||
'At least one item in collection "%s" is invalid.', | ||
get_class($this) | ||
)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.