public function testClear() { $this->image->setLocal($this->file); $this->image->setRemote('http://example.com/foo'); $this->image->clear(); $this->assertEmpty($this->image->getLocal()); $this->assertEmpty($this->image->getRemote()); }
/** * @param Image $entity * @param string $url * @param bool $override */ public function imageField(Image $entity, $url = '', $override = false) { if ($url) { $entity->setRemote($url); } // upload remote file if (!$entity->getLocal() instanceof UploadedFile && $entity->getRemote()) { if (!($path = parse_url($entity->getRemote(), PHP_URL_PATH))) { throw new \InvalidArgumentException('It is invalid URL: ' . $entity->getRemote()); } $entity->setFilename(pathinfo($path, PATHINFO_BASENAME)); $target = $this->getTargetDirForImageField($entity, $override); if (!$this->image($entity->getRemote(), $target, $override)) { throw new \RuntimeException('Failed download image'); } // set remote as local for validate $entity->setLocal(new UploadedFile($target, pathinfo($entity->getFilename(), PATHINFO_BASENAME), getimagesize($target)['mime'], filesize($target), UPLOAD_ERR_OK)); // validate entity $errors = $this->validator->validate($entity); if ($errors->has(0)) { unlink($target); throw new \InvalidArgumentException($errors->get(0)->getMessage()); } $entity->clear(); } // upload local file if ($entity->getLocal() instanceof UploadedFile) { $entity->setFilename($entity->getLocal()->getClientOriginalName()); $info = pathinfo($this->getTargetDirForImageField($entity, $override)); // upload from original name $entity->getLocal()->move($info['dirname'], $info['basename']); $entity->clear(); } }