diff --git a/src/Dropbox/Dropbox.php b/src/Dropbox/Dropbox.php index b017c38..b84775d 100644 --- a/src/Dropbox/Dropbox.php +++ b/src/Dropbox/Dropbox.php @@ -1274,4 +1274,155 @@ public function getSpaceUsage() //Return the decoded body return $body; } + + /** + * Get a File Request + * + * @param string $id File Request ID of the file request to get details for + * + * @link https://www.dropbox.com/developers/documentation/http/documentation#file_requests_get + * + * @return \Dropbox\Models\FileRequest + */ + public function getFileRequest($id) + { + //Set the id + $params['id'] = $id; + + //Get File Request + $response = $this->postToAPI('/file_requests/get', $params); + + //Make and Return the Model + return $this->makeModelFromResponse($response); + } + + /** + * Get the list of File Requests + * + * @link https://www.dropbox.com/developers/documentation/http/documentation#file_requests_list + * + * @return \Dropbox\Models\FileRequestCollection + */ + public function listFileRequests() + { + + //Get File Request List + $response = $this->postToAPI('/file_requests/list'); + + //Make and Return the Model + return $this->makeModelFromResponse($response); + } + + /** + * Create a File Request + * + * @param string $title Title of the File Request. + * @param string $destination The path of the folder in the Dropbox where uploaded files will be sent. + * @param string $deadline The deadline for this file request. (format="%Y-%m-%dT%H:%M:%SZ") + * @param string $allow_late_uploads If set, allow uploads after the deadline has passed. ['one_day','two_days','seven_days','thirty_days','always'] + * @param boolean $open Whether or not the file request should be open. + * + * @link https://www.dropbox.com/developers/documentation/http/documentation#file_requests_create + * + * @return \Dropbox\Models\FileRequest + */ + public function createFileRequest($title, $destination, $deadline = '', $allow_late_uploads = '', $open = true) + { + //Set the title and destination + $params['title'] = $title; + $params['destination'] = $destination; + $params['open'] = $open; + + //Add deadline if specified + if (!empty($deadline)) { + //Invalid Format + if (!preg_match('/(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})Z/', $deadline)) { + throw new DropboxClientException("Invalid format. Must be '%Y-%m-%dT%H:%M:%SZ'."); + } + $deadline_data = ['deadline' => $deadline]; + + //Add allow late uploads if specified + if (!empty($allow_late_uploads)) { + //Invalid Format + if (!in_array($allow_late_uploads, ['one_day','two_days','seven_days','thirty_days','always'])) { + throw new DropboxClientException("Invalid format. Must be any of 'one_day', 'two_days', 'seven_days', 'thirty_days' or 'always'."); + } + $deadline_data['allow_late_uploads'] = $allow_late_uploads; + } + + $params['deadline'] = new FileRequestDeadline($deadline_data); + } + + //Create File Request + $response = $this->postToAPI('/file_requests/create', $params); + + //Make and Return the Model + return $this->makeModelFromResponse($response); + } + + /** + * Update a File Request + * + * @param string $id File Request ID of the file request to update. + * @param string $title New title of the File Request. + * @param string $destination New path of the folder in the Dropbox where uploaded files will be sent. + * @param string $deadline New deadline for this file request. Empty string to clear. (format="%Y-%m-%dT%H:%M:%SZ") + * @param string $allow_late_uploads If set, allow uploads after the deadline has passed. ['one_day','two_days','seven_days','thirty_days','always'] + * @param boolean $open Whether or not the file request should be open. + * + * @link https://www.dropbox.com/developers/documentation/http/documentation#file_requests_update + * + * @return \Dropbox\Models\FileRequest + */ + public function updateFileRequest($id, $title = null, $destination = null, $deadline = null, $allow_late_uploads = '', $open = null) + { + //Set the id + $params['id'] = $id; + + //Set the new title + if (!is_null($title)) { + $params['title'] = $title; + } + + //Set the new destination + if (!is_null($destination)) { + $params['destination'] = $destination; + } + + //Set the new deadline, empty string to clear + if (!is_null($deadline)) { + if (empty($deadline)) { + $params['deadline'] = ['.tag' => 'update']; + } else { + //Invalid Format + if (!preg_match('/(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})Z/', $deadline)) { + throw new DropboxClientException("Invalid format. Must be '%Y-%m-%dT%H:%M:%SZ'."); + } + $deadline_data = ['.tag' => 'update', 'deadline' => $deadline]; + + //Add allow late uploads if specified + if (!empty($allow_late_uploads)) { + //Invalid Format + if (!in_array($allow_late_uploads, ['one_day','two_days','seven_days','thirty_days','always'])) { + throw new DropboxClientException("Invalid format. Must be any of 'one_day', 'two_days', 'seven_days', 'thirty_days' or 'always'."); + } + $deadline_data['allow_late_uploads'] = $allow_late_uploads; + } + + $params['deadline'] = $deadline_data; + } + } else { + $params['deadline'] = ['.tag' => 'no_update']; + } + + //Set the new open + if (!is_null($open)) { + $params['open'] = $open; + } + + $response = $this->postToAPI('/file_requests/update', $params); + + //Make and Return the Model + return $this->makeModelFromResponse($response); + } } diff --git a/src/Dropbox/Models/FileRequest.php b/src/Dropbox/Models/FileRequest.php new file mode 100644 index 0000000..b29097c --- /dev/null +++ b/src/Dropbox/Models/FileRequest.php @@ -0,0 +1,163 @@ +id = $this->getDataProperty('id'); + $this->url = $this->getDataProperty('url'); + $this->title = $this->getDataProperty('title'); + $this->destination = $this->getDataProperty('destination'); + $this->created = $this->getDataProperty('created'); + $this->is_open = $this->getDataProperty('is_open'); + $this->file_count = $this->getDataProperty('file_count'); + $this->deadline = $this->getDataProperty('deadline'); + if (is_array($this->deadline)) { + $this->deadline = new FileRequestDeadline($this->deadline); + } + } + + /** + * Get the 'id' property of the file request model. + * + * @return string + */ + public function getId() + { + return $this->id; + } + + /** + * Get the 'url' property of the file request model. + * + * @return string + */ + public function getUrl() + { + return $this->url; + } + + /** + * Get the 'title' property of the file request model. + * + * @return string + */ + public function getTitle() + { + return $this->title; + } + + /** + * Get the 'destination' property of the file request model. + * + * @return string + */ + public function getDestination() + { + return $this->destination; + } + + /** + * Get the 'created' property of the file request model. + * + * @return DateTime + */ + public function getCreated() + { + return $this->created; + } + + /** + * Get the 'is_open' property of the file request model. + * + * @return bool + */ + public function isOpen() + { + return $this->is_open; + } + + /** + * Get the 'file_count' property of the file request model. + * + * @return int + */ + public function getFileCount() + { + return $this->file_count; + } + + /** + * Get the 'deadline' property of the file request model. + * + * @return \Kunnu\Dropbox\Models\FileRequestDeadline + */ + public function getDeadline() + { + return $this->deadline; + } +} diff --git a/src/Dropbox/Models/FileRequestCollection.php b/src/Dropbox/Models/FileRequestCollection.php new file mode 100644 index 0000000..a80750b --- /dev/null +++ b/src/Dropbox/Models/FileRequestCollection.php @@ -0,0 +1,55 @@ +data = $data; + + $items = isset($data[$this->getCollectionItemsKey()]) ? $data[$this->getCollectionItemsKey()] : []; + $this->processItems($items); + } + + /** + * Process items and cast them + * to their respective Models + * + * @param array $items Unprocessed Items + * + * @return void + */ + protected function processItems(array $items) + { + $processedItems = []; + + foreach ($items as $entry) { + $processedItems[] = ModelFactory::make($entry); + } + + $this->items = new ModelCollection($processedItems); + } +} diff --git a/src/Dropbox/Models/FileRequestDeadline.php b/src/Dropbox/Models/FileRequestDeadline.php new file mode 100644 index 0000000..175cad7 --- /dev/null +++ b/src/Dropbox/Models/FileRequestDeadline.php @@ -0,0 +1,55 @@ +deadline = $this->getDataProperty('deadline'); + $this->allow_late_uploads = $this->getDataProperty('allow_late_uploads'); + if (is_array($this->allow_late_uploads)) { + $this->allow_late_uploads = $this->allow_late_uploads['.tag']; + } + } + + /** + * Get the 'deadline' property of the file request deadline model. + * + * @return string + */ + public function getDeadline() + { + return $this->deadline; + } + + /** + * Get the 'allow_late_uploads' property of the file request deadline model. + * + * @return string + */ + public function getAllowLateUploads() + { + return $this->allow_late_uploads; + } +} diff --git a/src/Dropbox/Models/ModelFactory.php b/src/Dropbox/Models/ModelFactory.php index 3c085cd..1e2991a 100644 --- a/src/Dropbox/Models/ModelFactory.php +++ b/src/Dropbox/Models/ModelFactory.php @@ -43,6 +43,16 @@ public static function make(array $data = array()) return new SearchResults($data); } + //File Requests List + if (static::isFileRequestList($data)) { + return new FileRequestCollection($data); + } + + //File Request + if (static::isFileRequest($data['id'])) { + return new FileRequest($data); + } + //Deleted File/Folder if (static::isDeletedFileOrFolder($data)) { return new DeletedMetadata($data); @@ -112,6 +122,26 @@ protected static function isSearchResult(array $data) return isset($data['matches']); } + /** + * @param array $data + * + * @return bool + */ + protected static function isFileRequestList(array $data) + { + return isset($data['file_requests']); + } + + /** + * @param array $data + * + * @return bool + */ + protected static function isFileRequest(array $data) + { + return isset($data['id']) && isset($data['is_open']); + } + /** * @param array $data *