Exemplo n.º 1
0
 /**
  * Adds an artifact to the collection.
  *
  * @param \Gedcomx\Rs\Client\Util\DataSource               $artifact
  * @param \Gedcomx\Source\SourceDescription                $description
  * @param \Gedcomx\Rs\Client\GedcomxApplicationState       $state
  * @param \Gedcomx\Rs\Client\Options\StateTransitionOption $option,...
  *
  * @throws \Gedcomx\Rs\Client\Exception\GedcomxApplicationException
  * @return \Gedcomx\Rs\Client\SourceDescriptionState
  */
 public function addArtifact(DataSource $artifact, SourceDescription $description = null, GedcomxApplicationState $state = null, StateTransitionOption $option = null)
 {
     if ($state == null) {
         $state = $this;
     }
     $link = $state->getLink(Rel::ARTIFACTS);
     if ($link == null || $link->getHref() == null) {
         throw new GedcomxApplicationException(sprintf("Resource at %s doesn't support adding artifacts.", state . getUri()));
     }
     $postData = [];
     if ($artifact->isFile()) {
         $postData[] = ['name' => 'artifact', 'contents' => fopen($artifact->getFile(), 'r')];
         if ($artifact->getTitle()) {
             $postData[] = ['name' => 'title', 'contents' => $artifact->getTitle()];
         }
     } else {
         foreach ($artifact->getParameters() as $key => $value) {
             $postData[] = ['name' => $key, 'contents' => $value];
         }
     }
     if ($description != null) {
         if ($description->getTitles() != null) {
             foreach ($description->getTitles() as $value) {
                 $postData[] = ['name' => 'title', 'contents' => $value->getValue()];
             }
         }
         if ($description->getDescriptions() != null) {
             foreach ($description->getDescriptions() as $value) {
                 $postData[] = ['name' => 'description', 'contents' => $value->getValue()];
             }
         }
         if ($description->getCitations() != null) {
             foreach ($description->getCitations() as $citation) {
                 $postData[] = ['name' => 'citation', 'contents' => $value->getValue()];
             }
         }
     }
     $body = new \GuzzleHttp\Psr7\MultipartStream($postData);
     $headers = ['Content-Type' => 'multipart/form-data; boundary=' . $body->getBoundary()];
     $request = $state->createAuthenticatedGedcomxRequest('POST', $link->getHref(), $headers, null, $body);
     return $state->stateFactory->createState('SourceDescriptionState', $this->client, $request, $state->passOptionsTo('invoke', array($request), func_get_args()), $state->accessToken);
 }