Skip to content

Commit 8acf44b

Browse files
authored
Merge pull request #14 from czarpino/add-user-metadata-calls
Add user metadata calls
2 parents 8927ca9 + 1b0ae9b commit 8acf44b

File tree

5 files changed

+737
-1
lines changed

5 files changed

+737
-1
lines changed

fccloudapi.php

Lines changed: 128 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,10 +228,41 @@ protected function reverseCastFromType($data, int $type)
228228

229229
return $data;
230230
}
231+
232+
/**
233+
* Best effort guess of datatype
234+
*
235+
* @param $data
236+
* @return int
237+
*/
238+
protected function guessType($data)
239+
{
240+
if ($data instanceof \DateTime) {
241+
return MetadataAttributeTypes::TYPE_DATE;
242+
}
243+
244+
if (is_int($data)) {
245+
return MetadataAttributeTypes::TYPE_INTEGER;
246+
}
247+
248+
if (is_float($data)) {
249+
return MetadataAttributeTypes::TYPE_DECIMAL;
250+
}
251+
252+
if (is_bool($data)) {
253+
return MetadataAttributeTypes::TYPE_BOOLEAN;
254+
}
255+
256+
if (is_array($data)) {
257+
return MetadataAttributeTypes::TYPE_ARRAY;
258+
}
259+
260+
return MetadataAttributeTypes::TYPE_TEXT;
261+
}
231262
}
232263

233264
/**
234-
* Class AbstractMetadataRecord
265+
* Class MetadataAttributeTypes
235266
* @package codelathe\fccloudapi
236267
*/
237268
final class MetadataAttributeTypes
@@ -2733,6 +2764,8 @@ protected function doChunkedUpload($url, $filechunkpath, $filename) {
27332764
}
27342765

27352766
class CloudAPI extends APICore {
2767+
2768+
use MetadataAttributeTypeCasterTrait;
27362769

27372770
public function __construct($SERVER_URL, $debug = false) {
27382771
parent::__construct($SERVER_URL, $debug);
@@ -4619,6 +4652,100 @@ public function getMetadataValues(string $fullPath)
46194652

46204653
return $collection;
46214654
}
4655+
4656+
/**
4657+
* Get metadata sets for search
4658+
*
4659+
* @param string $fullPath
4660+
* @return Collection|null
4661+
*/
4662+
public function getMetadataSetsForSearch(string $fullPath): ?Collection
4663+
{
4664+
$this->startTimer();
4665+
$response = $this->doPOST("{$this->server_url}/core/getmetadatasetsforsearch", http_build_query([
4666+
'fullpath' => $fullPath
4667+
]));
4668+
$collection = new Collection($response, 'metadataset', MetadataSetRecord::class);
4669+
$this->stopTimer();
4670+
4671+
return $collection;
4672+
}
4673+
4674+
/**
4675+
* Add specified metadata set to file object
4676+
*
4677+
* @param string $fullPath
4678+
* @param string $setId
4679+
* @return CommandRecord|null
4680+
*/
4681+
public function addSetToFileObject(string $fullPath, string $setId): ?CommandRecord
4682+
{
4683+
$this->startTimer();
4684+
$response = $this->doPOST("{$this->server_url}/core/addsettofileobject", http_build_query([
4685+
'fullpath' => $fullPath,
4686+
'setid' => $setId,
4687+
]));
4688+
$collection = new Collection($response, 'command', CommandRecord::class);
4689+
$records = $collection->getRecords();
4690+
$record = $collection->getNumberOfRecords() > 0 ? reset($records) : null;
4691+
$this->stopTimer();
4692+
4693+
return $record;
4694+
}
4695+
4696+
/**
4697+
* Remove specified metadata set from file object
4698+
*
4699+
* @param string $fullPath
4700+
* @param string $setId
4701+
* @return CommandRecord|null
4702+
*/
4703+
public function removeSetFromFileObject(string $fullPath, string $setId): ?CommandRecord
4704+
{
4705+
$this->startTimer();
4706+
$response = $this->doPOST("{$this->server_url}/core/removesetfromfileobject", http_build_query([
4707+
'fullpath' => $fullPath,
4708+
'setid' => $setId,
4709+
]));
4710+
$collection = new Collection($response, 'command', CommandRecord::class);
4711+
$records = $collection->getRecords();
4712+
$record = $collection->getNumberOfRecords() > 0 ? reset($records) : null;
4713+
$this->stopTimer();
4714+
4715+
return $record;
4716+
}
4717+
4718+
/**
4719+
* Update attribute values of a file object's metadata
4720+
*
4721+
* @param string $setId
4722+
* @param string $fullPath
4723+
* @param array $attributes
4724+
* @return CommandRecord|null
4725+
*/
4726+
public function saveAttributeValues(string $fullPath, string $setId, array $attributes): ?CommandRecord
4727+
{
4728+
$this->startTimer();
4729+
$args = [
4730+
'fullpath' => $fullPath,
4731+
'setid' => $setId,
4732+
];
4733+
4734+
foreach ($attributes as $i => $attribute) {
4735+
$args["attribute{$i}_attributeid"] = $attribute['attributeid'];
4736+
$args["attribute{$i}_value"] = $this->reverseCastFromType($attribute['value'], $this->guessType($attribute['value']));
4737+
}
4738+
4739+
$args['attributes_total'] = count($attributes);
4740+
4741+
$response = $this->doPOST("{$this->server_url}/core/saveattributevalues", http_build_query($args));
4742+
$collection = new Collection($response, 'command', CommandRecord::class);
4743+
$records = $collection->getRecords();
4744+
$record = $collection->getNumberOfRecords() > 0 ? reset($records) : null;
4745+
$this->stopTimer();
4746+
4747+
return $record;
4748+
}
46224749

46234750
public function getUITranslations() {
46244751
$this->startTimer();
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?php
2+
3+
namespace CodeLathe\FileCloudApi\Tests\CloudApi;
4+
5+
use codelathe\fccloudapi\CloudAPI;
6+
use codelathe\fccloudapi\CommandRecord;
7+
use CodeLathe\FileCloudApi\Tests\Fixtures\AccessibleCloudApi;
8+
use PHPUnit\Framework\TestCase;
9+
10+
class AddSetToFileObjectTest extends TestCase
11+
{
12+
public function testOnSuccess()
13+
{
14+
$serverUrl = 'https://fcapi.example.com';
15+
$cloudApiMock = $this->getMockBuilder(AccessibleCloudApi::class)
16+
->setConstructorArgs([$serverUrl])
17+
->setMethods(['init', '__destruct', 'doPost'])
18+
->getMock();
19+
20+
$mockApiResponse = <<<RESPONSE
21+
<commands>
22+
<command>
23+
<type>addsettofileobject</type>
24+
<result>1</result>
25+
<message>Metadata set (setId: 5ccafe12adccf621f80342e6) was successfully added to File Object (/tester/textfile1.txt)</message>
26+
</command>
27+
</commands>
28+
RESPONSE;
29+
$mockApiRequest = $this->getValidApiRequest();
30+
31+
$cloudApiMock->method('doPost')
32+
->with("{$serverUrl}/core/addsettofileobject", http_build_query($mockApiRequest))
33+
->willReturn($mockApiResponse);
34+
35+
/** @var CloudAPI $cloudApiMock */
36+
/** @var CommandRecord $commandRecord */
37+
$commandRecord = $cloudApiMock->addSetToFileObject(...array_values($mockApiRequest));
38+
$this->assertEquals(1, $commandRecord->getResult());
39+
$this->assertEquals('addsettofileobject', $commandRecord->getType());
40+
$this->assertEquals(
41+
"Metadata set (setId: {$mockApiRequest['setid']}) was successfully added to File Object ({$mockApiRequest['fullpath']})",
42+
$commandRecord->getMessage()
43+
);
44+
}
45+
46+
public function testOnFailure()
47+
{
48+
$serverUrl = 'https://fcapi.example.com';
49+
$cloudApiMock = $this->getMockBuilder(AccessibleCloudApi::class)
50+
->setConstructorArgs([$serverUrl])
51+
->setMethods(['init', '__destruct', 'doPost'])
52+
->getMock();
53+
54+
$mockApiResponse = <<<RESPONSE
55+
<commands>
56+
<command>
57+
<type>addsettofileobject</type>
58+
<result>0</result>
59+
<message>Failed to bind Set Definition (setId: 5ccafe12adccf621f80342e7) to File Object (/tester/textfile1.txt). Reason: Incorrect set id provided</message>
60+
</command>
61+
</commands>
62+
RESPONSE;
63+
$mockApiRequest = $this->getValidApiRequest();
64+
$mockApiRequest['setid'] = '5ccafe12adccf621f80342e7'; // Pretend this is an invalid id
65+
66+
67+
$cloudApiMock->method('doPost')
68+
->with("{$serverUrl}/core/addsettofileobject", http_build_query($mockApiRequest))
69+
->willReturn($mockApiResponse);
70+
71+
/** @var CloudAPI $cloudApiMock */
72+
/** @var CommandRecord $commandRecord */
73+
$commandRecord = $cloudApiMock->addSetToFileObject(...array_values($mockApiRequest));
74+
$this->assertEquals(0, $commandRecord->getResult());
75+
$this->assertEquals('addsettofileobject', $commandRecord->getType());
76+
$this->assertEquals(
77+
"Failed to bind Set Definition (setId: {$mockApiRequest['setid']}) to File Object ({$mockApiRequest['fullpath']}). Reason: Incorrect set id provided",
78+
$commandRecord->getMessage()
79+
);
80+
}
81+
82+
private function getValidApiRequest()
83+
{
84+
return [
85+
'fullpath' => '/tester/textfile1.txt',
86+
'setid' => '5ccafe12adccf621f80342e6',
87+
];
88+
}
89+
}

0 commit comments

Comments
 (0)