1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
<?php
/* --------------------------------------------------------------
ProductImageContainer.php 2015-12-29
Gambio GmbH
http://www.gambio.de
Copyright (c) 2015 Gambio GmbH
Released under the GNU General Public License (Version 2)
[http://www.gnu.org/licenses/gpl-2.0.html]
--------------------------------------------------------------
*/
/**
* Class ProductImageContainer
*
* @category System
* @package Product
* @subpackage Entities
*/
class ProductImageContainer implements ProductImageContainerInterface
{
/**
* Primary image.
*
* @var ProductImage
*/
protected $primaryImage;
/**
* Additional image associative array.
*
* @var array
*/
protected $additionalImages;
/**
* ProductImageContainer constructor.
*/
public function __construct()
{
$primaryImageFile = MainFactory::create('FilenameStringType', '');
$this->primaryImage = MainFactory::create('ProductImage', $primaryImageFile);
$this->additionalImages = array();
}
/**
* Sets the primary image of the image container.
*
* @param ProductImageInterface $image Primary product image to set.
*
* @return ProductImageContainer Same instance for chained method calls.
*/
public function setPrimary(ProductImageInterface $image)
{
$this->primaryImage = $image;
return $this;
}
/**
* Returns the product primary image from container.
*
* @return ProductImageInterface The requested primary product image from the image container.
*/
public function getPrimary()
{
return $this->primaryImage;
}
/**
* Adds an additional image to the additional images array.
*
* @param ProductImageInterface $image Additional product image to set in the container.
*
* @throws InvalidArgumentException if the provided image type is not valid.
*
* @return ProductImageContainer Same instance for chained method calls.
*/
public function addAdditional(ProductImageInterface $image)
{
$key = $image->getFilename();
$this->additionalImages[$key] = $image;
return $this;
}
/**
* Returns the array of additional images as a collection.
*
* @return ProductImageCollection The requested additional images from container.
*/
public function getAdditionals()
{
$collection = MainFactory::create('ProductImageCollection', $this->additionalImages);
return $collection;
}
/**
* Replaces an additional product image in the container.
*
* @param FilenameStringType $imageFile Image file name.
* @param ProductImageInterface $image Image to place.
*
* @return ProductImageContainer Same instance for chained method calls.
*/
public function replaceAdditional(FilenameStringType $imageFile, ProductImageInterface $image)
{
$key = $imageFile->asString();
$this->additionalImages[$key] = $image;
return $this;
}
/**
* Deletes an image from the additional images array.
*
* @param FilenameStringType $imageFile Image filename.
*
* @return ProductImageContainer Same instance for chained method calls.
*/
public function delete(FilenameStringType $imageFile)
{
$key = $imageFile->asString();
if($this->primaryImage->getFilename() === $imageFile->asString())
{
$this->primaryImage = MainFactory::create('EmptyProductImage');
}
else
{
unset($this->additionalImages[$key]);
}
return $this;
}
}