for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
declare(strict_types=1);
namespace RedRat\Presenthor\Bag;
use RedRat\Presenthor\Item\ItemInterface;
/**
* Item Bag Trait
*
* @package RedRat\Bag
*/
trait ItemBagTrait
{
* @var ItemInterface[]
private $items;
* Item Bag Trait constructor
public function __construct()
$this->items = [];
}
* {@inheritDoc}
public function addItem(ItemInterface $item, bool $acceptDuplicate = false): bool
if ($this->hasItem($item) && !$acceptDuplicate) {
return false;
$this->items[] = $item;
return true;
public function hasItem(ItemInterface $item): bool
return \in_array($item, $this->items);
public function removeItem(ItemInterface $item): bool
if (!$this->hasItem($item)) {
$itemKey = \array_search($item, $this->items);
unset($this->items[$itemKey]);
public function getItemsList(): array
return $this->items;
public function clearItems(): void
public function countItems(): int
return \count($this->items);
public function count(): int
return $this->countItems();
public function toArray(): array
$returnData = [];
foreach ($this->items as $item) {
$returnData[] = $item->toArray();
return $returnData;
public function toJson(): string
return \json_encode($this->toArray());