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