| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Clarify; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | use Clarify\Exceptions\InvalidEnumTypeException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Clarify\Exceptions\InvalidJSONException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Clarify\Exceptions\InvalidIntegerArgumentException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * Class Bundle | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * @package Clarify | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * @property mixed  $metadata   This is the metadata subresource of the bundle. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * @property mixed  $tracks     This is the tracks subresource of the bundle. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | class Bundle implements \Iterator | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     protected $client = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     protected $position = 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     public $detail = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     public $location = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     public function __construct($key, $client = null, $user_agent = '') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |         $this->client = (is_null($client)) ? new \Clarify\Client($key, $client, $user_agent) : $client; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      * @param string $name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      * @param string $media_url | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * @param string $metadata | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * @param string $notify_url | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * @param string $audio_channel | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      * @throws Exceptions\InvalidJSONException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      * @throws Exceptions\InvalidEnumTypeException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     public function create($name = '', $media_url = '', $metadata = '', $notify_url = '', $audio_channel = '') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         $params = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         $params['name'] = $name; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         $params['media_url'] = $media_url; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         $params['metadata'] = $metadata; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         $params['notify_url'] = $notify_url; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         $params['audio_channel'] = $audio_channel; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         $ob = json_decode($metadata); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         if ($metadata != '' && $ob === null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |             throw new InvalidJSONException(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         $audio_channel = isset($params['audio_channel']) ? $params['audio_channel'] : ''; | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 52 |  | View Code Duplication |         if (!in_array($audio_channel, array('left', 'right', 'split', ''))) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |             throw new InvalidEnumTypeException(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         $result = $this->client->post('bundles', $params); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         $this->detail = $this->client->detail; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         $this->location = $this->client->response->getHeader('Location')[0]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         return $result; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      * @param $id | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      * @param string $name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |      * @param string $notify_url | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      * @param int $version | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |      * @return mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |      * @throws InvalidIntegerArgumentException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     public function update($id, $name = '', $notify_url = '', $version  = 1) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         $params = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         $params['name'] = $name; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         $params['notify_url'] = $notify_url; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         $params['version'] = $version; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         if (!is_numeric($params['version'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |             throw new InvalidIntegerArgumentException(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         return $this->client->put($id, $params); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     public function delete($id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         return $this->client->delete($id); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     public function load($id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         return $this->client->get($id); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  |     public function index($limit = 10, $embed = '') | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 95 |  |  |         $params = array('limit' => $limit, 'embed' => $embed); | 
            
                                                                        
                            
            
                                    
            
            
                | 96 |  |  |         $this->detail = $this->client->get('bundles', $params); | 
            
                                                                        
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 98 |  |  |         return $this->detail; | 
            
                                                                        
                            
            
                                    
            
            
                | 99 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |      * The nice thing about search is that once you've loaded some results, you can use the normal pagination functions | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |      *   without having to do anything special because search results are just another collection. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |      * @param $query | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |      * @param int $limit                    How many search results to return at a time | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |      * @param string $embed                 Should the full item be embedded in the results or just a reference? | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |      * @param string $query_fields | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |      * @param string $filter | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |      * @param string $language | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |      * @return array|bool|float|int|string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |     public function search($query, $limit = 10, $embed = '', $query_fields = '', $filter = '', $language = 'en') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |         $params = array('query' => $query, 'limit' => $limit, 'embed' => $embed, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |                         'query_fields' => $query_fields, 'filter' => $filter, 'language' => $language); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |         $this->detail = $this->client->get('search', $params); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |         return $this->detail; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |     public function hasMorePages() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |         return isset($this->detail['_links']['next']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |     public function getNextPage() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |         return $this->getPage('next'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |     public function getPreviousPage() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |         return $this->getPage('prev'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |     protected function getPage($direction = 'next') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |         if (isset($this->detail['_links'][$direction])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |             $next_uri = $this->detail['_links'][$direction]['href']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |             $_params = parse_url($next_uri); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |             parse_str($_params['query'], $params); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |             $this->detail = $this->client->get($next_uri, $params); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |             $this->detail = json_encode(array()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |         return $this->detail; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |     public function getResponse() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |         return $this->client->response; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |     public function getStatusCode() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |         return $this->client->statusCode; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |     public function rewind() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |         $this->position = 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |     public function current() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |         $item_id = $this->detail['_links']['items'][$this->position]['href']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |         return $item_id; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |     public function key() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |         return $this->position; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |     public function next() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |         $this->position++; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |     public function valid() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  |         return isset($this->detail['_links']['items'][$this->position]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  |     public function count() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 |  |  |         return $this->detail['total']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 |  |  |      * @param $name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 |  |  |      * @return Metadata|Tracks|Insights | 
            
                                                                                                            
                            
            
                                    
            
            
                | 196 |  |  |      * @throws Exceptions\InvalidResourceException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 197 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 198 |  |  |     public function __get($name) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 199 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 200 |  |  |         $classname = ucwords($name); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 201 |  |  |         $fullclass = "Clarify\\" . $classname; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 202 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 203 |  |  |         if (class_exists($fullclass)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 204 |  |  |             return new $fullclass($this->client); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 205 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 206 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 207 |  |  |         throw new \Clarify\Exceptions\InvalidResourceException('That subresource was not found'); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 208 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 209 |  |  | } |