Skip to content

File Requests support #109

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
151 changes: 151 additions & 0 deletions src/Dropbox/Dropbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
163 changes: 163 additions & 0 deletions src/Dropbox/Models/FileRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
<?php
namespace Kunnu\Dropbox\Models;

class FileRequest extends BaseModel
{

/**
* A unique identifier of the file request
*
* @var string
*/
protected $id;

/**
* The url.
*
* @var string
*/
protected $url;

/**
* The title.
*
* @var string
*/
protected $title;

/**
* The destination.
*
* @var string
*/
protected $destination;

/**
* The created.
*
* @var DateTime
*/
protected $created;

/**
* The is_open.
*
* @var bool
*/
protected $is_open;

/**
* The file_count.
*
* @var int
*/
protected $file_count;

/**
* Set if this file is contained in a shared folder.
*
* @var \Kunnu\Dropbox\Models\FileRequestDeadline
*/
protected $deadline;

/**
* Create a new FileRequest instance
*
* @param array $data
*/
public function __construct(array $data)
{
parent::__construct($data);
$this->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;
}
}
55 changes: 55 additions & 0 deletions src/Dropbox/Models/FileRequestCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php
namespace Kunnu\Dropbox\Models;

class FileRequestCollection extends ModelCollection
{

/**
* Collection Items Key
*
* @const string
*/
const COLLECTION_ITEMS_KEY = 'file_requests';

/**
* Get the Collection Items Key
*
* @return string
*/
public function getCollectionItemsKey()
{
return static::COLLECTION_ITEMS_KEY;
}

/**
* Create a new FileRequest Collection
*
* @param array $data Collection Data
*/
public function __construct(array $data)
{
$this->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);
}
}
Loading