1 : <?php
2 :
3 : /**
4 : * Abstraction of a list resource from the Twilio API.
5 : *
6 : * @category Services
7 : * @package Services_Twilio
8 : * @author Neuman Vong <neuman@twilio.com>
9 : * @license http://creativecommons.org/licenses/MIT/ MIT
10 : * @link http://pear.php.net/package/Services_Twilio
11 : */
12 : abstract class Services_Twilio_ListResource
13 : extends Services_Twilio_Resource
14 : implements IteratorAggregate
15 : {
16 : private $_page;
17 :
18 : /**
19 : * Gets a resource from this list.
20 : *
21 : * @param string $sid The resource SID
22 : * @return Services_Twilio_InstanceResource The resource
23 : */
24 : public function get($sid)
25 : {
26 27 : $schema = $this->getSchema();
27 27 : $type = $schema['instance'];
28 27 : return new $type(is_object($sid)
29 27 : ? new Services_Twilio_CachingDataProxy(
30 12 : isset($sid->sid) ? $sid->sid : NULL, $this, $sid
31 27 : ) : new Services_Twilio_CachingDataProxy($sid, $this));
32 : }
33 :
34 : /**
35 : * Deletes a resource from this list.
36 : *
37 : * @param string $sid The resource SID
38 : * @return null
39 : */
40 : public function delete($sid, array $params = array())
41 : {
42 1 : $schema = $this->getSchema();
43 1 : $basename = $schema['basename'];
44 1 : $this->proxy->deleteData("$basename/$sid", $params);
45 1 : }
46 :
47 : /**
48 : * Create a resource on the list and then return its representation as an
49 : * InstanceResource.
50 : *
51 : * @param array $params The parameters with which to create the resource
52 : *
53 : * @return Services_Twilio_InstanceResource The created resource
54 : */
55 : protected function _create(array $params)
56 : {
57 6 : $schema = $this->getSchema();
58 6 : $basename = $schema['basename'];
59 6 : return $this->get($this->proxy->createData($basename, $params));
60 : }
61 :
62 : /**
63 : * Create a resource on the list and then return its representation as an
64 : * InstanceResource.
65 : *
66 : * @param array $params The parameters with which to create the resource
67 : *
68 : * @return Services_Twilio_InstanceResource The created resource
69 : */
70 : public function retrieveData($sid, array $params = array())
71 : {
72 16 : $schema = $this->getSchema();
73 16 : $basename = $schema['basename'];
74 16 : return $this->proxy->retrieveData("$basename/$sid", $params);
75 : }
76 :
77 : /**
78 : * Create a resource on the list and then return its representation as an
79 : * InstanceResource.
80 : *
81 : * @param array $params The parameters with which to create the resource
82 : *
83 : * @return Services_Twilio_InstanceResource The created resource
84 : */
85 : public function createData($sid, array $params = array())
86 : {
87 11 : $schema = $this->getSchema();
88 11 : $basename = $schema['basename'];
89 11 : return $this->proxy->createData("$basename/$sid", $params);
90 : }
91 :
92 : /**
93 : * Returns a page of InstanceResources from this list.
94 : *
95 : * @param int $page The start page
96 : * @param int $size Number of items per page
97 : * @param array $size Optional filters
98 : *
99 : * @return Services_Twilio_Page A page
100 : */
101 : public function getPage($page = 0, $size = 50, array $filters = array())
102 : {
103 8 : $schema = $this->getSchema();
104 8 : $page = $this->proxy->retrieveData($schema['basename'], array(
105 8 : 'Page' => $page,
106 8 : 'PageSize' => $size,
107 8 : ) + $filters);
108 :
109 8 : $page->{$schema['list']} = array_map(
110 8 : array($this, 'get'),
111 8 : $page->{$schema['list']}
112 8 : );
113 :
114 8 : return new Services_Twilio_Page($page, $schema['list']);
115 : }
116 :
117 : /**
118 : * Returns meta data about this list resource type.
119 : *
120 : * @return array Meta data
121 : */
122 : public function getSchema()
123 : {
124 27 : $name = get_class($this);
125 27 : $parts = explode('_', $name);
126 27 : $basename = end($parts);
127 : return array(
128 27 : 'name' => $name,
129 27 : 'basename' => $basename,
130 27 : 'instance' => substr($name, 0, -1),
131 27 : 'list' => self::decamelize($basename),
132 27 : );
133 : }
134 :
135 : /**
136 : * Returns an iterable list of InstanceResources
137 : *
138 : * @param int $page The start page
139 : * @param int $size Number of items per page
140 : * @param array $size Optional filters
141 : *
142 : * The filter array can accept full datetimes when StartTime or DateCreated
143 : * are used. Inequalities should be within the key portion of the array and
144 : * multiple filter parameters can be combined for more specific searches.
145 : *
146 : * eg.
147 : * array('DateCreated>' => '2011-07-05 08:00:00', 'DateCreated<' => '2011-08-01')
148 : * or
149 : * array('StartTime<' => '2011-07-05 08:00:00')
150 : *
151 : * @return Services_Twilio_AutoPagingIterator An iterator
152 : */
153 : public function getIterator($page = 0, $size = 50, array $filters = array())
154 : {
155 2 : return new Services_Twilio_AutoPagingIterator(
156 2 : array($this, 'getPageGenerator'),
157 2 : create_function('$page, $size, $filters',
158 2 : 'return array($page + 1, $size, $filters);'),
159 2 : array($page, $size, $filters)
160 2 : );
161 : }
162 :
163 : public function getPageGenerator($page, $size, array $filters = array()) {
164 2 : return $this->getPage($page, $size, $filters)->getItems();
165 : }
166 : }
|