Skip to content

Commit 2ae0639

Browse files
DriveItem operations
1 parent 229219c commit 2ae0639

File tree

10 files changed

+226
-8
lines changed

10 files changed

+226
-8
lines changed
1.76 KB
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
4+
namespace Office365;
5+
6+
7+
class EnumType
8+
{
9+
10+
}

src/Graph/DriveItem.php

+80
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,82 @@
77

88

99

10+
use Office365\Runtime\DeleteEntityQuery;
11+
use Office365\Runtime\Http\HttpMethod;
12+
use Office365\Runtime\Http\RequestOptions;
13+
use Office365\Runtime\InvokeMethodQuery;
14+
use Office365\Runtime\InvokePostMethodQuery;
1015
use Office365\Runtime\ResourcePath;
16+
use Office365\Runtime\ResourcePathUrl;
1117

1218
/**
1319
* Item is the main data model in the OneDrive API. Everything is an item.
1420
*/
1521
class DriveItem extends BaseItem
1622
{
1723

24+
/**
25+
* The simple upload API allows you to provide the contents of a new file or update the contents of an
26+
* existing file in a single API call. This method only supports files up to 4MB in size.
27+
* @param string $name
28+
* @param string $content
29+
* @return DriveItem
30+
*/
31+
public function upload($name, $content)
32+
{
33+
$driveItem = new DriveItem($this->getContext(), new ResourcePathUrl($name,$this->resourcePath));
34+
$qry = new InvokePostMethodQuery($driveItem, null,null,null,$content);
35+
$this->getContext()->addQueryAndResultObject($qry,$driveItem);
36+
$this->getContext()->getPendingRequest()->beforeExecuteQuery(function (RequestOptions $request){
37+
$request->Url .= "content";
38+
$request->Method = HttpMethod::Put;
39+
},true);
40+
return $driveItem;
41+
}
42+
43+
44+
/**
45+
* Download the contents of the primary stream (file) of a DriveItem. Only driveItems with the file property
46+
* can be downloaded.
47+
* @param resource $handle
48+
*/
49+
public function download($handle){
50+
$qry = new InvokeMethodQuery($this);
51+
$this->getContext()->getPendingRequest()->beforeExecuteQuery(function (RequestOptions $request) use ($handle){
52+
$request->Url .= "content";
53+
$request->StreamHandle = $handle;
54+
$request->FollowLocation = true;
55+
},true);
56+
$this->getContext()->addQuery($qry);
57+
}
58+
59+
60+
/**
61+
* Converts the contents of an item in a specific format
62+
* @param resource $handle
63+
* @param string $format
64+
*/
65+
public function convert($handle, $format)
66+
{
67+
$qry = new InvokeMethodQuery($this);
68+
$this->getContext()->getPendingRequest()->beforeExecuteQuery(function (RequestOptions $request) use ($handle,$format){
69+
$request->Url .= "content?\$format=$format";
70+
$request->StreamHandle = $handle;
71+
$request->FollowLocation = true;
72+
},true);
73+
$this->getContext()->addQuery($qry);
74+
}
75+
76+
/**
77+
* Delete a DriveItem by using its ID or path. Note that deleting items using this method will move the items to
78+
* the recycle bin instead of permanently deleting the item.
79+
*/
80+
public function delete()
81+
{
82+
$qry = new DeleteEntityQuery($this);
83+
$this->getContext()->addQuery($qry);
84+
}
85+
1886

1987
/**
2088
* @return string
@@ -406,4 +474,16 @@ public function getPermissions()
406474
}
407475
return $this->getProperty("Permissions");
408476
}
477+
478+
public function setProperty($name, $value, $persistChanges = true)
479+
{
480+
parent::setProperty($name, $value, $persistChanges);
481+
if($name == "id" && $this->resourcePath->getParent()->getSegment() == "Children"){
482+
$this->resourcePath = new ResourcePath($value,
483+
new ResourcePath("items", $this->parentCollection->getResourcePath()->getParent()->getParent()));
484+
}
485+
}
486+
487+
488+
409489
}

src/Graph/Entity.php

+6
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88

99
use Office365\Runtime\ClientObject;
10+
use Office365\Runtime\ResourcePath;
1011

1112
class Entity extends ClientObject
1213
{
@@ -31,6 +32,11 @@ public function setId($value)
3132
function setProperty($name, $value, $persistChanges = true)
3233
{
3334
$name = ucfirst($name);
35+
if($name == "Id"){
36+
if (is_null($this->getResourcePath())) {
37+
$this->resourcePath = new ResourcePath($value, $this->parentCollection->getResourcePath());
38+
}
39+
}
3440
parent::setProperty($name, $value, $persistChanges);
3541
}
3642

src/Graph/EntityCollection.php

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
4+
namespace Office365\Graph;
5+
6+
use Office365\Runtime\ClientObjectCollection;
7+
8+
class EntityCollection extends ClientObjectCollection
9+
{
10+
11+
}

src/Runtime/ClientRuntimeContext.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ public function hasPendingRequest()
153153

154154

155155
/**
156-
* Gets the build version of Microsoft.SharePoint.Client.ServerRuntime.dll on the server.
156+
* Gets the build version.
157157
* @return Version
158158
*/
159159
public function getServerLibraryVersion(){

src/Runtime/Http/RequestOptions.php

+23-2
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,22 @@ public function __construct($url, $headers = array(), $data = null, $methodType
3131
$this->Data = $data;
3232
$this->ConnectTimeout = null;
3333
$this->TransferEncodingChunkedAllowed = false;
34+
$this->FollowLocation = false;
3435
}
3536

37+
/**
38+
* @return array
39+
*/
3640
public function toArray()
3741
{
3842
return get_object_vars($this);
3943
}
4044

45+
46+
/**
47+
* @param string $name
48+
* @param string $value
49+
*/
4150
public function addCustomHeader($name, $value)
4251
{
4352
if (is_null($this->Headers)) {
@@ -48,16 +57,19 @@ public function addCustomHeader($name, $value)
4857
}
4958
}
5059

60+
61+
/**
62+
* @return string[]
63+
*/
5164
public function getRawHeaders()
5265
{
53-
$headers = array_map(
66+
return array_map(
5467
function ($k, $v) {
5568
return "$k:$v";
5669
},
5770
array_keys($this->Headers),
5871
array_values($this->Headers)
5972
);
60-
return $headers;
6173
}
6274

6375

@@ -117,6 +129,7 @@ function ($k, $v) {
117129

118130

119131
/**
132+
* Control which version range of SSL/TLS versions to use
120133
* @var int
121134
*/
122135
public $SSLVersion;
@@ -135,6 +148,7 @@ function ($k, $v) {
135148

136149

137150
/**
151+
* It should contain the maximum time in seconds that you allow the connection phase to the server to take
138152
* @var ?int
139153
*/
140154
public $ConnectTimeout;
@@ -144,4 +158,11 @@ function ($k, $v) {
144158
* @var bool
145159
*/
146160
public $TransferEncodingChunkedAllowed;
161+
162+
/**
163+
* tells the library to follow any Location: header that the server sends as part of an HTTP header
164+
* in a 3xx response. The Location: header can specify a relative or an absolute URL to follow.
165+
* @var bool
166+
*/
167+
public $FollowLocation;
147168
}

src/Runtime/Http/Requests.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,8 @@ private static function init(RequestOptions $options)
149149
curl_setopt($ch,CURLOPT_USERPWD, $options->UserCredentials->toString());
150150
if(!is_null($options->ConnectTimeout))
151151
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $options->ConnectTimeout);
152-
152+
if($options->FollowLocation)
153+
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
153154
return $ch;
154155
}
155156

src/Runtime/ResourcePathUrl.php

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
4+
namespace Office365\Runtime;
5+
6+
/**
7+
* Resource path for OneDrive path-based addressing
8+
*/
9+
class ResourcePathUrl extends ResourcePath
10+
{
11+
public function __construct($url, ResourcePath $parent = null)
12+
{
13+
parent::__construct(rawurlencode($url), $parent);
14+
}
15+
16+
public function toUrl()
17+
{
18+
return $this->parent->toUrl() . ":/$this->segment:/";
19+
}
20+
21+
}

tests/microsoftgraph/OneDriveTest.php

+72-4
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,102 @@
22

33
namespace Office365;
44

5+
use Office365\Graph\DriveItem;
56
use Office365\Graph\IdentitySet;
67

8+
79
class OneDriveTest extends GraphTestCase
810
{
11+
private static $localFile;
12+
13+
public static function setUpBeforeClass()
14+
{
15+
self::$localFile = __DIR__ . "/../../examples/data/SharePoint User Guide.docx";
16+
parent::setUpBeforeClass();
17+
}
918

10-
public function testMyDrive(){
19+
public function testMyDrive()
20+
{
1121
$myDrive = self::$graphClient->getMe()->getDrive();
1222
self::$graphClient->load($myDrive);
1323
self::$graphClient->executeQuery();
1424
self::assertNotNull($myDrive->getWebUrl());
1525
}
1626

1727

18-
public function testMyDriveProperties(){
28+
public function testMyDriveProperties()
29+
{
1930
$myDrive = self::$graphClient->getMe()->getDrive();
20-
self::$graphClient->load($myDrive,["Owner"]);
31+
self::$graphClient->load($myDrive, ["Owner"]);
2132
self::$graphClient->executeQuery();
2233
$owner = $myDrive->getOwner();
2334
self::assertTrue($owner instanceof IdentitySet);
2435
}
2536

2637

27-
public function testCreateFolder(){
38+
public function testCreateFolder()
39+
{
2840
$myDrive = self::$graphClient->getMe()->getDrive();
2941
$folderName = "Archive_" . rand(1, 100000);
3042
$targetFolder = $myDrive->getRoot()->getChildren()->createFolder($folderName);
3143
self::$graphClient->executeQuery();
3244
self::assertNotNull($targetFolder->getName());
45+
return $targetFolder;
46+
}
47+
48+
49+
/**
50+
* @depends testCreateFolder
51+
* @param DriveItem $folderItem
52+
* @return DriveItem
53+
*/
54+
public function testUploadFile(DriveItem $folderItem)
55+
{
56+
$fileContent = file_get_contents(self::$localFile);
57+
$fileName = basename(self::$localFile);
58+
$uploadFileItem = $folderItem->upload($fileName, $fileContent);
59+
self::$graphClient->executeQuery();
60+
self::assertNotNull($uploadFileItem->getWebUrl());
61+
return $uploadFileItem;
62+
}
63+
64+
65+
/**
66+
* @depends testUploadFile
67+
* @param DriveItem $fileItem
68+
*/
69+
public function testConvertFile(DriveItem $fileItem)
70+
{
71+
$fileName = join(DIRECTORY_SEPARATOR, [sys_get_temp_dir(),"SampleFile.pdf" ]);
72+
$fh = fopen($fileName, 'w+');
73+
$fileItem->convert($fh,"pdf");
74+
self::$graphClient->executeQuery();
75+
fclose($fh);
76+
}
77+
78+
79+
/**
80+
* @depends testUploadFile
81+
* @param DriveItem $fileItem
82+
*/
83+
public function testDownloadFile(DriveItem $fileItem)
84+
{
85+
$fileName = join(DIRECTORY_SEPARATOR, [sys_get_temp_dir(), basename(self::$localFile)]);
86+
$fh = fopen($fileName, 'w+');
87+
$fileItem->download($fh);
88+
self::$graphClient->executeQuery();
89+
fclose($fh);
90+
}
91+
92+
93+
/**
94+
* @depends testCreateFolder
95+
* @param DriveItem $folderItem
96+
*/
97+
public function testDeleteDriveItem(DriveItem $folderItem)
98+
{
99+
$folderItem->delete();
100+
self::$graphClient->executeQuery();
33101
}
34102

35103
}

0 commit comments

Comments
 (0)