public function addAttachment(Attachment $attachment) { /** @var Object $this */ $uri = sprintf('%s/%s/Attachments/%s', $this::getResourceURI(), $this->getGUID(), $attachment->getFileName()); $url = new URL($this->_application, $uri); $request = new Request($this->_application, $url, Request::METHOD_POST); $request->setBody($attachment->getContent(), $attachment->getMimeType()); $request->send(); $response = $request->getResponse(); if (false !== ($element = current($response->getElements()))) { $attachment->fromStringArray($element); } return $this; }
/** * Function to save properties directly which do not update via a POST * * This is called automatically from the save method for things like adding contacts to ContactGroups * * @param Object $object * @throws Exception */ private function savePropertiesDirectly(Object $object) { foreach ($object::getProperties() as $property_name => $meta) { if ($meta[Object::KEY_SAVE_DIRECTLY] && $object->isPropertyDirty($property_name)) { //Then actually save $property_objects = $object->{$property_name}; $property_type = get_class(current($property_objects)); $url = new URL($this, sprintf('%s/%s/%s', $object::getResourceURI(), $object->getGUID(), $property_type::getResourceURI())); $request = new Request($this, $url, Request::METHOD_PUT); $property_array = array(); foreach ($property_objects as $property_object) { $property_array[] = $property_object->toStringArray(); } $root_node_name = Helpers::pluralize($property_type::getRootNodeName()); $request->setBody(Helpers::arrayToXML(array($root_node_name => $property_array))); $request->send(); $response = $request->getResponse(); foreach ($response->getElements() as $element_index => $element) { if ($response->getErrorsForElement($element_index) === null) { $property_objects[$element_index]->fromStringArray($element); $property_objects[$element_index]->setClean(); } } //Set it clean so the following save might have nothing to do. $object->setClean($property_name); } } }