/** * Load image using by the filename, and assign a product to it. * * @param string $name The filename for the image * @param Product $product The product to assign to the image * @param array $options The product options to assign to the image * @param string $type The type to set against the image * @throws Exception\AssignmentException Throws exception if file cannot be found * @throws Exception\AssignmentException Throws exception if file is not an image * * @return Image Returns a new Image instance with the product assigned */ public function assignByName($name, Product $product, array $options = [], $type = 'default') { $file = $this->_fileLoader->getByFilename($name); if (is_array($file)) { $file = array_shift($file); } if (false === $file) { throw new Exception\AssignmentException('Could not find image by name of `' . $name . '`'); } if ($file->typeID !== File\Type::IMAGE) { throw new Exception\AssignmentException('File with name `' . $name . '` is not an image file'); } return $this->_setProductToImage($product, $file, $options, $type); }
/** * Load all image files and create an array of choices * * @return array */ private function _getImageChoices() { $images = (array) $this->_fileLoader->getByType(File\Type::IMAGE); $choices = []; foreach ($images as $image) { $choices[$image->id] = $image->name; } asort($choices); return $choices; }
/** * Load file with ID matching that submitted, and assign it to the bundle. * * @param Bundle $bundle * @param array $data * @throws Exception\BundleBuildException Throws exception if no file exists with submitted file ID * @throws Exception\BundleBuildException Throws exception if the file loaded is not an image */ private function _addImage(Bundle $bundle, array $data) { if (!empty($data[Form\BundleForm::IMAGE])) { $id = $data[Form\BundleForm::IMAGE]; $image = $this->_fileLoader->getByID($id); if (!$image) { throw new Exception\BundleBuildException('Could not load file with ID `' . $id . '`'); } if (!$image instanceof File\File || $image->typeID !== File\Type::IMAGE) { throw new Exception\BundleBuildException('File with ID `' . $id . '` is not a valid image'); } $bundle->setImage($image); } }
/** * Lazy load image assigned to bundle using the FileLoader * * @param BundleProxy $bundle * * @return array|\Message\Mothership\FileManager\File\File */ public function getImage(BundleProxy $bundle) { return $this->_fileLoader->getByID($bundle->getImageID()); }