Skip to content

Instantly share code, notes, and snippets.

@kurozumi
Last active October 23, 2020 03:45
Show Gist options
  • Save kurozumi/02dca0ea9c074f8351a661b76bd466dc to your computer and use it in GitHub Desktop.
Save kurozumi/02dca0ea9c074f8351a661b76bd466dc to your computer and use it in GitHub Desktop.
<?php
/**
* This file is part of ProductHistory
*
* Copyright(c) Akira Kurozumi <info@a-zumi.net>
*
* https://a-zumi.net
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Customize\Service\ProductHistory;
use Doctrine\Common\Collections\ArrayCollection;
use Eccube\Entity\Product;
/**
* Class ProductCollection
* @package Customize\Service\CheckedProduct
*/
class ProductCollection extends ArrayCollection
{
/**
* @var int
*/
private $limit;
/**
* ProductCollection constructor.
* @param array $cookie
* @param int $limit
*/
public function __construct(array $cookie = [], int $limit = 5)
{
$this->limit = $limit;
parent::__construct($cookie);
}
/**
* @param Product $product
* @return bool|true
*/
public function addProduct(Product $product): bool
{
if ($this->count() > $this->limit) {
$this->removeElement($this->last());
}
if($this->contains($product->getId())) {
return false;
}
return parent::add($product->getId());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment