Beispiel #1
0
 /**
  * Hydrate a resource value
  *
  * @param array $valueObject
  * @param Value $value
  */
 protected function hydrateResource(array $valueObject, Value $value)
 {
     $value->setType(Value::TYPE_RESOURCE);
     $value->setValue(null);
     // set default
     $value->setLang(null);
     // set default
     $value->setUriLabel(null);
     // set default
     $valueResource = $this->adapter->getEntityManager()->find('Omeka\\Entity\\Resource', $valueObject['value_resource_id']);
     if (null === $valueResource) {
         throw new Exception\NotFoundException(sprintf($this->adapter->getTranslator()->translate('Resource not found with id %s.'), $valueObject['value_resource_id']));
     }
     if ($valueResource instanceof Media) {
         $translator = $this->adapter->getTranslator();
         $exception = new Exception\ValidationException();
         $exception->getErrorStore()->addError('value', $translator->translate('A value resource cannot be Media.'));
         throw $exception;
     }
     $value->setValueResource($valueResource);
 }
 /**
  * Hydrate an entity.
  *
  * Encapsulates hydration, authorization, pre-validation API events, and
  * validation procedures into one method.
  *
  * @throws Exception\ValidationException
  * @param Request $request
  * @param EntityInterface $entity
  * @param ErrorStore $errorStore
  */
 protected function hydrateEntity(Request $request, EntityInterface $entity, ErrorStore $errorStore)
 {
     $operation = $request->getOperation();
     // Before everything, check whether the current user has access to this
     // entity in its original state.
     $this->authorize($entity, $operation);
     // Trigger the operation's api.validate.data.pre event.
     $event = new Event(Event::API_VALIDATE_DATA_PRE, $this, ['services' => $this->getServiceLocator(), 'entity' => $entity, 'request' => $request, 'errorStore' => $errorStore]);
     $this->getEventManager()->trigger($event);
     // Validate the request.
     $this->validateRequest($request, $errorStore);
     if ($errorStore->hasErrors()) {
         $validationException = new Exception\ValidationException();
         $validationException->setErrorStore($errorStore);
         throw $validationException;
     }
     $this->hydrate($request, $entity, $errorStore);
     // Trigger the operation's api.validate.entity.pre event.
     $event = new Event(Event::API_VALIDATE_ENTITY_PRE, $this, ['services' => $this->getServiceLocator(), 'entity' => $entity, 'request' => $request, 'errorStore' => $errorStore]);
     $this->getEventManager()->trigger($event);
     // Validate the entity.
     $this->validateEntity($entity, $errorStore);
     if ($errorStore->hasErrors()) {
         if (Request::UPDATE == $operation) {
             // Refresh the entity from the database, overriding any local
             // changes that have not yet been persisted
             $this->getEntityManager()->refresh($entity);
         }
         $validationException = new Exception\ValidationException();
         $validationException->setErrorStore($errorStore);
         throw $validationException;
     }
 }