forked from vgrem/phpSPO
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServiceOperationPath.php
More file actions
122 lines (108 loc) · 3.94 KB
/
ServiceOperationPath.php
File metadata and controls
122 lines (108 loc) · 3.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<?php
namespace Office365\Runtime\Paths;
use Exception;
use Office365\Runtime\ClientObject;
use Office365\Runtime\ClientValue;
use Office365\Runtime\CSOM\ICSOMCallable;
use Office365\Runtime\ResourcePath;
use SimpleXMLElement;
/**
* Resource path to address Service Operations which represents simple functions exposed by an OData service
*/
class ServiceOperationPath extends ResourcePath implements ICSOMCallable
{
/**
* ResourcePathMethod constructor.
* @param string $methodName
* @param array|ClientObject|ClientValue $methodParameters
* @param ResourcePath|null $parent
*/
public function __construct($methodName=null, $methodParameters = null, ?ResourcePath $parent=null)
{
parent::__construct($this->buildSegment($methodName,$methodParameters), $parent);
$this->methodName = $methodName;
$this->methodParameters = $methodParameters;
}
/**
* @param string $methodName
* @param array $methodParameters
* @return string|null
*/
private function buildSegment($methodName,$methodParameters)
{
$url = isset($methodName) ? $methodName : "";
if (!isset($methodParameters) || !is_array($methodParameters))
return $url;
if (count(array_filter(array_keys($methodParameters), 'is_string')) === 0) {
$url = $url . "(" . implode(',', array_map(
function ($value) {
$encValue = self::escapeValue($value);
return "$encValue";
}, $methodParameters)
) . ")";
} else {
$url = $url . "(" . implode(',', array_map(
function ($key, $value) {
$encValue = self::escapeValue($value);
return "$key=$encValue";
}, array_keys($methodParameters), $methodParameters)
) . ")";
}
return $url;
}
private static function escapeValue($value)
{
if (is_string($value)) {
/**
* Given value that is a path, like `/sites/Site/O'Reilly`, needs to be enclosed within quotes:
* ```
* GET https://{site_url}/_api/web/GetFolderByServerRelativeUrl('/sites/Site/O'Reilly')/Files
* ```
* because the quote within the name of the folder `O'Reilly` is messing up where the argument ends,
* it needs to be escaped, like so:
* ```
* GET https://{site_url}/_api/web/GetFolderByServerRelativeUrl('/sites/Site/O''Reilly')/Files
* ```
* before sending it to the API.
*
*
* `rawurlencode`ing it doesn't solve the issue, the URL is decoded before it is evaluated by the API it seems.
*
*
* Sources:
* - https://sharepoint.stackexchange.com/questions/154590/getfilebyserverrelativeurl-fails-when-the-filename-contains-a-quote
* - https://web.archive.org/web/20230325070719/http://www.sharepointnadeem.com/2012/06/special-characters-in-rest-query-filter.html
*/
$value = str_replace('%27', '%27%27', $value);
$value = str_replace("'", "''", $value);
$value = "'" . $value . "'";
} elseif (is_bool($value)) {
$value = var_export($value, true);
}
return $value;
}
function buildQuery(SimpleXMLElement $writer)
{
throw new Exception("Not implemented");
}
/**
* @return string|null
*/
function getMethodName(){
return $this->methodName;
}
/**
* @return array|ClientObject|ClientValue|null
*/
function getMethodParameters(){
return $this->methodParameters;
}
/**
* @var array|ClientObject|ClientValue
*/
protected $methodParameters;
/**
* @var string
*/
protected $methodName;
}