1 : <?php
2 :
3 : /**
4 : * Abstraction of a Twilio resource.
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_Resource
13 : implements Services_Twilio_DataProxy
14 : {
15 : protected $name;
16 : protected $proxy;
17 : protected $subresources;
18 :
19 : public function __construct(Services_Twilio_DataProxy $proxy)
20 : {
21 27 : $this->subresources = array();
22 27 : $this->proxy = $proxy;
23 27 : $this->name = get_class($this);
24 27 : $this->init();
25 27 : }
26 :
27 : protected function init()
28 : {
29 : // Left empty for derived classes to implement
30 27 : }
31 :
32 : public function retrieveData($path, array $params = array())
33 : {
34 12 : return $this->proxy->retrieveData($path, $params);
35 : }
36 :
37 : public function deleteData($path, array $params = array())
38 : {
39 1 : return $this->proxy->deleteData($path, $params);
40 : }
41 :
42 : public function createData($path, array $params = array())
43 : {
44 10 : return $this->proxy->createData($path, $params);
45 : }
46 :
47 : public function getSubresources($name = null)
48 : {
49 25 : if (isset($name)) {
50 25 : return isset($this->subresources[$name])
51 25 : ? $this->subresources[$name]
52 25 : : null;
53 : }
54 0 : return $this->subresources;
55 : }
56 :
57 : public function addSubresource($name, Services_Twilio_Resource $res)
58 : {
59 27 : $this->subresources[$name] = $res;
60 27 : }
61 :
62 : protected function setupSubresources()
63 : {
64 27 : foreach (func_get_args() as $name) {
65 27 : $constantized = ucfirst(Services_Twilio_Resource::camelize($name));
66 27 : $type = "Services_Twilio_Rest_" . $constantized;
67 27 : $this->addSubresource($name, new $type($this));
68 27 : }
69 27 : }
70 :
71 : public static function decamelize($word)
72 : {
73 27 : return preg_replace(
74 27 : '/(^|[a-z])([A-Z])/e',
75 27 : 'strtolower(strlen("\\1") ? "\\1_\\2" : "\\2")',
76 : $word
77 27 : );
78 : }
79 :
80 : public static function camelize($word)
81 : {
82 27 : return preg_replace('/(^|_)([a-z])/e', 'strtoupper("\\2")', $word);
83 : }
84 : }
85 :
|