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