Horde_Feed_Entry_Atom::save PHP Method

save() public method

Save is used to either create new entries or to save changes to existing ones. If we have a link rel="edit", we are changing an existing entry. In this case we re-serialize the entry and PUT it to the edit URI, checking for a 200 OK result. For posting new entries, you must specify the $postUri parameter to save() to tell the object where to post itself. We use $postUri and POST the serialized entry there, checking for a 201 Created response. If the insert is successful, we then parse the response from the POST to get any values that the server has generated: an id, an updated time, and its new link rel="edit".
public save ( string $postUri = null, array $headers = [] )
$postUri string Location to POST for creating new entries.
$headers array Additional headers to transmit while posting.
    public function save($postUri = null, $headers = array())
    {
        $headers = array_merge(array('Content-Type' => 'application/atom+xml'), $headers);
        if ($this->id()) {
            // If id is set, look for link rel="edit" in the
            // entry object and PUT.
            $editUri = $this->link('edit');
            if (!$editUri) {
                throw new Horde_Feed_Exception('Cannot edit entry; no link rel="edit" is present.');
            }
            $response = $this->_httpClient->put($editUri, $this->saveXml(), $headers);
            if ($response->code !== 200) {
                throw new Horde_Feed_Exception('Expected response code 200, got ' . $response->code);
            }
        } else {
            if ($postUri === null) {
                throw new Horde_Feed_Exception('PostURI must be specified to save new entries.');
            }
            $response = $this->_httpClient->post($postUri, $this->saveXml(), $headers);
            if ($response->code !== 201) {
                throw new Horde_Feed_Exception('Expected response code 201, got ' . $response->code);
            }
        }
        // Update internal properties using the response body.
        $body = $response->getBody();
        $newEntry = new DOMDocument();
        $parsed = @$newEntry->loadXML($body);
        if (!$parsed) {
            throw new Horde_Feed_Exception('DOMDocument cannot parse XML: ', error_get_last());
        }
        $newEntry = $newEntry->getElementsByTagName($this->_entryElementName)->item(0);
        if (!$newEntry) {
            throw new Horde_Feed_Exception('No root <feed> element found in server response:' . "\n\n" . $body);
        }
        if ($this->_element->parentNode) {
            $oldElement = $this->_element;
            $this->_element = $oldElement->ownerDocument->importNode($newEntry, true);
            $oldElement->parentNode->replaceChild($this->_element, $oldElement);
        } else {
            $this->_element = $newEntry;
        }
        $this->_expireCachedChildren();
    }

Usage Example

Exemplo n.º 1
0
 public function testEdit()
 {
     $mock = new Horde_Http_Request_Mock();
     $mock->setResponse(new Horde_Http_Response_Mock('', fopen(__DIR__ . '/fixtures/AtomPublishingTest-updated-entry.xml', 'r'), array('HTTP/1.1 200')));
     $httpClient = new Horde_Http_Client(array('request' => $mock));
     // The base feed URI is the same as the POST URI, so just supply the
     // Horde_Feed_Entry_Atom object with that.
     $contents = file_get_contents(__DIR__ . '/fixtures/AtomPublishingTest-before-update.xml');
     $entry = new Horde_Feed_Entry_Atom($contents, $httpClient);
     // Initial state.
     $this->assertEquals('2005-05-23T16:26:00-08:00', $entry->updated(), 'Initial state of updated timestamp does not match');
     $this->assertEquals('http://example.com/Feed/1/1/', $entry->link('edit'), 'Initial state of edit link does not match');
     // Just change the entry's properties directly.
     $entry->content = '1.2';
     // Then save the changes.
     $entry->save();
     // New state.
     $this->assertEquals('1.2', $entry->content(), 'Content change did not stick');
     $this->assertEquals('2005-05-23T16:27:00-08:00', $entry->updated(), 'New updated link is not correct');
     $this->assertEquals('http://example.com/Feed/1/2/', $entry->link('edit'), 'New edit link is not correct');
 }
All Usage Examples Of Horde_Feed_Entry_Atom::save
Horde_Feed_Entry_Atom