public function testAddRemoveAttachment() { $fh = fopen(__DIR__ . '/_files/logo.jpg', 'r'); $user = $this->dm->find('Doctrine\\Tests\\Models\\CMS\\CmsUser', 'user_with_attachment'); $user->attachments['logo.jpg'] = Attachment::createFromBinaryData($fh, 'image/jpeg'); $this->dm->flush(); $this->dm->clear(); // dont re-use identity map $user = $this->dm->find('Doctrine\\Tests\\Models\\CMS\\CmsUser', 'user_with_attachment'); unset($user->attachments['foo.txt']); $this->dm->flush(); $this->dm->clear(); // dont re-use identity map $user = $this->dm->find('Doctrine\\Tests\\Models\\CMS\\CmsUser', 'user_with_attachment'); $this->assertArrayHasKey('logo.jpg', $user->attachments); $this->assertArrayNotHasKey('foo.txt', $user->attachments); }
public function testCreateFromBinaryFileHandle() { $fh = fopen(__DIR__ . "/_files/foo.txt", "r"); $attachment = Attachment::createFromBinaryData($fh); $this->assertEquals('Hello i am a string!', $attachment->getRawData()); }
/** * Convert an object from $source to an entity or a value object. * * @param mixed $source * @param string $targetType * @param array $convertedChildProperties * @param \TYPO3\Flow\Property\PropertyMappingConfigurationInterface $configuration * @return object the target type * @throws \TYPO3\Flow\Property\Exception\InvalidTargetException * @throws \InvalidArgumentException */ public function convertFrom($source, $targetType, array $convertedChildProperties = array(), \TYPO3\Flow\Property\PropertyMappingConfigurationInterface $configuration = NULL) { if (is_array($source)) { $object = $this->handleArrayData($source, $targetType, $convertedChildProperties, $configuration); } elseif (is_string($source)) { if ($source === '') { return NULL; } $object = $this->fetchObjectFromPersistence($source, $targetType); } else { throw new \InvalidArgumentException('Only strings and arrays are accepted.', 1305630314); } foreach ($convertedChildProperties as $propertyName => $propertyValue) { if ($this->reflectionService->isPropertyAnnotatedWith($targetType, $propertyName, 'Doctrine\\ODM\\CouchDB\\Mapping\\Annotations\\Attachments')) { $attachments = array(); foreach ($propertyValue as $version => $value) { $safeFileName = preg_replace('/[^a-zA-Z-_0-9\\.]*/', '', $value['name']); $attachments[$safeFileName] = \Doctrine\CouchDB\Attachment::createFromBinaryData(\TYPO3\Flow\Utility\Files::getFileContents($value['tmp_name'])); } $propertyValue = $attachments; } $result = \TYPO3\Flow\Reflection\ObjectAccess::setProperty($object, $propertyName, $propertyValue); if ($result === FALSE) { $exceptionMessage = sprintf('Property "%s" having a value of type "%s" could not be set in target object of type "%s". Make sure that the property is accessible properly, for example via an appropriate setter method.', $propertyName, is_object($propertyValue) ? get_class($propertyValue) : gettype($propertyValue), $targetType); throw new \TYPO3\Flow\Property\Exception\InvalidTargetException($exceptionMessage, 1297935345); } } return $object; }
public function setOriginalData($data, $type) { $attachment = Attachment::createFromBinaryData($data, $type); return $this->setAttachment('original', $attachment); }