Skip to content
This repository has been archived by the owner on Feb 2, 2021. It is now read-only.

Commit

Permalink
Folders can be requested and item collections in those folders can be…
Browse files Browse the repository at this point in the history
… obtained.
  • Loading branch information
Helveg committed Dec 1, 2018
1 parent df6551b commit 6c8abbc
Show file tree
Hide file tree
Showing 7 changed files with 153 additions and 90 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
"description": "A PHP Box package.",
"type": "library",
"require": {
"guzzlehttp/guzzle": "^6.3@dev",
"firebase/php-jwt": "dev-master",
"guzzlehttp/guzzle": "^6.3.3",
"firebase/php-jwt": "5.0.0",
"php": ">=7.0"
},
"license": "GPL-3.0-only",
Expand Down
111 changes: 33 additions & 78 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

52 changes: 48 additions & 4 deletions src/Box.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

namespace PhpBox;
use PhpBox\Config\Config;
use PhpBox\Items\{Folder};

class Box {
private $baseUrl = "https://api.box.com/2.0/";
private $AccessToken;
private $config;

Expand All @@ -28,16 +30,58 @@ public function requestAccessToken() {
'client_secret' => $appDetails->Secret
]
]);
$data = $response->getBody()->getContents();
$this->AccessToken = new Token(json_decode($data));
if($this->handleResponse($response)) {
$data = $response->getBody()->getContents();
$this->AccessToken = new Token(json_decode($data));
}
return false;
}

public function getAccessToken() {
return $this->AccessToken;
}

public function requestFolder($id, $findpath = false) {

public function getValidAccessToken() {
if(!Token::isValid($this->AccessToken)) {
$this->requestAccessToken();
}
return $this->AccessToken;
}

public function requestFolder($id = "0", $fields = []) {
$client = new \GuzzleHttp\Client();
$headers = $this->getDefaultHeaders();
$query = [];
if(!empty($fields)) {
$query['fields'] = implode(",", $fields);
}
$response = $client->request('GET', $this->baseUrl."folders/$id", [
'headers' => $headers,
'query' => $query
]);
if($this->handleResponse($response)){
return new Folder(json_decode($response->getBody()->getContents()));
}
return false;
}

public function getDefaultHeaders() {
$token = $this->getValidAccessToken();
return [
'Authorization' => "Bearer $token",
'Accept' => 'application/json',
];
}

public function handleResponse(\GuzzleHttp\Psr7\Response $response) {
$statusCode = $response->getStatusCode();
switch($statusCode) {
case 200:
return true;
default:
throw new \Exception("Uncaught response code $statusCode");
}
return true;
}
}

Expand Down
27 changes: 27 additions & 0 deletions src/Items/Folder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace PhpBox\Items;

class Folder extends Item {
protected $contains = [];

public function __construct($data) {
parent::__construct($data);
if(isset($data->item_collection)) {
foreach ($data->item_collection->entries as $key => $value) {
$this->contains[] = new Item($value);
}
}
}

public function getItems() {
return $this->contains;
}

public function getFiles() {
return array_filter($this->contains, function($x) {return $x->isFile();});
}

}

?>
25 changes: 25 additions & 0 deletions src/Items/Item.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace PhpBox\Items;

class Item implements ItemInterface {
protected $data;
protected $id;
protected $type;

public function __construct($data) {
$this->data = $data;
$this->type = $data->type;
$this->id = $data->id;
}

public function isFolder() {
return $this->type == 'folder';
}

public function isFile() {
return $this->type == 'file';
}
}

?>
9 changes: 9 additions & 0 deletions src/Items/ItemInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace PhpBox\Items;

interface ItemInterface {

}

?>
Loading

0 comments on commit 6c8abbc

Please sign in to comment.