Sabre\CardDAV\Backend\PDO::createCard PHP Method

createCard() public method

The addressbook id will be passed as the first argument. This is the same id as it is returned from the getAddressBooksForUser method. The cardUri is a base uri, and doesn't include the full path. The cardData argument is the vcard body, and is passed as a string. It is possible to return an ETag from this method. This ETag is for the newly created resource, and must be enclosed with double quotes (that is, the string itself must contain the double quotes). You should only return the ETag if you store the carddata as-is. If a subsequent GET request on the same card does not have the same body, byte-by-byte and you did return an ETag here, clients tend to get confused. If you don't return an ETag, you can just return null.
public createCard ( mixed $addressBookId, string $cardUri, string $cardData ) : string | null
$addressBookId mixed
$cardUri string
$cardData string
return string | null
    function createCard($addressBookId, $cardUri, $cardData)
    {
        $stmt = $this->pdo->prepare('INSERT INTO ' . $this->cardsTableName . ' (carddata, uri, lastmodified, addressbookid, size, etag) VALUES (?, ?, ?, ?, ?, ?)');
        $etag = md5($cardData);
        $stmt->execute([$cardData, $cardUri, time(), $addressBookId, strlen($cardData), $etag]);
        $this->addChange($addressBookId, $cardUri, 1);
        return '"' . $etag . '"';
    }

Usage Example

Example #1
0
 /**
  * @depends testCreateCard
  */
 function testGetMultiple()
 {
     $result = $this->backend->createCard(1, 'card2', 'data2');
     $result = $this->backend->createCard(1, 'card3', 'data3');
     $check = [['id' => 1, 'uri' => 'card1', 'carddata' => 'card1', 'lastmodified' => 0], ['id' => 2, 'uri' => 'card2', 'carddata' => 'data2', 'lastmodified' => time()], ['id' => 3, 'uri' => 'card3', 'carddata' => 'data3', 'lastmodified' => time()]];
     $result = $this->backend->getMultipleCards(1, ['card1', 'card2', 'card3']);
     foreach ($check as $index => $node) {
         foreach ($node as $k => $v) {
             $expected = $v;
             $actual = $result[$index][$k];
             switch ($k) {
                 case 'lastmodified':
                     $this->assertInternalType('int', $actual);
                     break;
                 case 'carddata':
                     if (is_resource($actual)) {
                         $actual = stream_get_contents($actual);
                     }
                     // No break intended.
                 // No break intended.
                 default:
                     $this->assertEquals($expected, $actual);
                     break;
             }
         }
     }
 }
All Usage Examples Of Sabre\CardDAV\Backend\PDO::createCard