1<?php
2
3namespace Sabre\CardDAV\Backend;
4
5class Mock extends AbstractBackend {
6
7    public $addressBooks;
8    public $cards;
9
10    function __construct($addressBooks = null, $cards = null) {
11
12        $this->addressBooks = $addressBooks;
13        $this->cards = $cards;
14
15        if (is_null($this->addressBooks)) {
16            $this->addressBooks = array(
17                array(
18                    'id' => 'foo',
19                    'uri' => 'book1',
20                    'principaluri' => 'principals/user1',
21                    '{DAV:}displayname' => 'd-name',
22                ),
23            );
24
25            $card2 = fopen('php://memory','r+');
26            fwrite($card2,"BEGIN:VCARD\nVERSION:3.0\nUID:45678\nEND:VCARD");
27            rewind($card2);
28            $this->cards = array(
29                'foo' => array(
30                    'card1' => "BEGIN:VCARD\nVERSION:3.0\nUID:12345\nEND:VCARD",
31                    'card2' => $card2,
32                ),
33            );
34        }
35
36    }
37
38
39    function getAddressBooksForUser($principalUri) {
40
41        $books = array();
42        foreach($this->addressBooks as $book) {
43            if ($book['principaluri'] === $principalUri) {
44                $books[] = $book;
45            }
46        }
47        return $books;
48
49    }
50
51    /**
52     * Updates properties for an address book.
53     *
54     * The list of mutations is stored in a Sabre\DAV\PropPatch object.
55     * To do the actual updates, you must tell this object which properties
56     * you're going to process with the handle() method.
57     *
58     * Calling the handle method is like telling the PropPatch object "I
59     * promise I can handle updating this property".
60     *
61     * Read the PropPatch documenation for more info and examples.
62     *
63     * @param string $addressBookId
64     * @param \Sabre\DAV\PropPatch $propPatch
65     * @return void
66     */
67    public function updateAddressBook($addressBookId, \Sabre\DAV\PropPatch $propPatch) {
68
69        foreach($this->addressBooks as &$book) {
70            if ($book['id'] !== $addressBookId)
71                continue;
72
73            $propPatch->handleRemaining(function($mutations) use (&$book) {
74                foreach($mutations as $key=>$value) {
75                    $book[$key] = $value;
76                }
77                return true;
78            });
79
80        }
81
82    }
83
84    function createAddressBook($principalUri, $url, array $properties) {
85
86        $this->addressBooks[] = array_merge($properties, array(
87            'id' => $url,
88            'uri' => $url,
89            'principaluri' => $principalUri,
90        ));
91
92    }
93
94    function deleteAddressBook($addressBookId) {
95
96        foreach($this->addressBooks as $key=>$value) {
97            if ($value['id'] === $addressBookId)
98                unset($this->addressBooks[$key]);
99        }
100        unset($this->cards[$addressBookId]);
101
102    }
103
104    function getCards($addressBookId) {
105
106        $cards = array();
107        foreach($this->cards[$addressBookId] as $uri=>$data) {
108            $cards[] = array(
109                'uri' => $uri,
110                'carddata' => $data,
111            );
112        }
113        return $cards;
114
115    }
116
117    function getCard($addressBookId, $cardUri) {
118
119        if (!isset($this->cards[$addressBookId][$cardUri])) {
120            return false;
121        }
122
123        return array(
124            'uri' => $cardUri,
125            'carddata' => $this->cards[$addressBookId][$cardUri],
126        );
127
128    }
129
130    function createCard($addressBookId, $cardUri, $cardData) {
131
132        $this->cards[$addressBookId][$cardUri] = $cardData;
133
134    }
135
136    function updateCard($addressBookId, $cardUri, $cardData) {
137
138        $this->cards[$addressBookId][$cardUri] = $cardData;
139
140    }
141
142    function deleteCard($addressBookId, $cardUri) {
143
144        unset($this->cards[$addressBookId][$cardUri]);
145
146    }
147
148}
149