Skip to content

Commit 0ee0e6f

Browse files
committed
getters/setters for custom headers and regex check
1 parent 53a1e52 commit 0ee0e6f

File tree

8 files changed

+95
-82
lines changed

8 files changed

+95
-82
lines changed

composer.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@
1616
}
1717
],
1818
"require-dev": {
19-
"fabpot/php-cs-fixer": ">=1.9",
20-
"phpdocumentor/phpdocumentor": "~2.8",
19+
"friendsofphp/php-cs-fixer": ">=1.9",
2120
"phpspec/phpspec": "~2.5"
2221
},
2322
"config": {

examples/docker/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@ WORKDIR /php/examples
1515
RUN curl -sS https://getcomposer.org/installer | php && mv composer.phar /usr/local/bin/composer && composer require "rosette/api: ~1.0.0"
1616

1717
# allow interactive bash inside docker container
18-
CMD ./run_php.sh $API_KEY $ALT_URL; /bin/bash
18+
CMD ./run_php.sh $API_KEY $ALT_URL
1919

2020
VOLUME ["/source"]

examples/language.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@
1717
$api = isset($options['url']) ? new Api($options['key'], $options['url']) : new Api($options['key']);
1818
$params = new DocumentParameters();
1919
$content = $language_data;
20+
$params->set('content', $content);
2021
$appHeader = [];
2122
$appHeader = "X-RosetteAPI-App: php-app";
2223
$customHeaders = [];
2324
$customHeaders[0] = $appHeader;
24-
$params->set('content', $content);
25-
$params->loadCustomHeaders($customHeaders);
25+
$api->setCustomHeaders($customHeaders);
2626

2727
try {
2828
$result = $api->language($params);

source/rosette/api/Api.php

Lines changed: 83 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,12 @@ class Api
109109
*/
110110
private $options;
111111

112+
/**
113+
* internal customHeaders array
114+
* @var array
115+
*/
116+
private $customHeaders;
117+
112118

113119
/**
114120
* Create an L{API} object.
@@ -121,6 +127,8 @@ public function __construct($user_key, $service_url = 'https://api.rosette.com/
121127
{
122128
$this->user_key = $user_key;
123129

130+
131+
124132
$this->headers = array("X-RosetteAPI-Key: $user_key",
125133
"Content-Type: application/json",
126134
"Accept: application/json",
@@ -129,6 +137,14 @@ public function __construct($user_key, $service_url = 'https://api.rosette.com/
129137
"X-RosetteAPI-Binding: php",
130138
"X-RosetteAPI-Binding-Version: " . self::$binding_version );
131139

140+
/*$this->headers = $this->addHeaders(array("X-RosetteAPI-Key: $user_key",
141+
"Content-Type: application/json",
142+
"Accept: application/json",
143+
"Accept-Encoding: gzip",
144+
"User-Agent: RosetteAPIPHP/" . self::$binding_version,
145+
"X-RosetteAPI-Binding: php",
146+
"X-RosetteAPI-Binding-Version: " . self::$binding_version ));*/
147+
132148
$this->setServiceUrl($service_url);
133149
$this->setDebug(false);
134150
$this->setTimeout(300);
@@ -301,6 +317,72 @@ public function clearOptions()
301317
$this->options = array();
302318
}
303319

320+
/**
321+
* Setter for options
322+
*
323+
*
324+
* @return array
325+
*/
326+
public function getCustomHeaders()
327+
{
328+
/*if (sizeof($this->customHeaders) > 0) {
329+
return $this->customHeaders;
330+
} else {
331+
return null;
332+
}*/
333+
return $this->customHeaders;
334+
}
335+
336+
/**
337+
* Setter for custom headers
338+
*
339+
* @param array $headers
340+
*
341+
*/
342+
public function setCustomHeaders($headers)
343+
{
344+
$this->clearCustomHeaders();
345+
if($headers != null){
346+
foreach ($headers as $key => $value) {
347+
if(preg_match("/^X-RosetteAPI-/", $value)){
348+
array_push($this->customHeaders, $value);
349+
} else {
350+
throw new RosetteException("Custom headers must start with \"X-\"");
351+
}
352+
}
353+
}
354+
}
355+
356+
/**
357+
* Adds custom headers to headers array if there are any
358+
*
359+
* @param array $headers
360+
*
361+
* @return array $headers
362+
**/
363+
private function addHeaders($headers)
364+
{
365+
$customHeaders = $this->getCustomHeaders();
366+
367+
if(sizeof($customHeaders) > 0){
368+
foreach($customHeaders as $value)
369+
{
370+
array_push($headers, $value);
371+
}
372+
return $headers;
373+
} else {
374+
return $headers;
375+
}
376+
}
377+
378+
/**
379+
* Clears all custom headers
380+
*/
381+
public function clearCustomHeaders()
382+
{
383+
$this->customHeaders = array();
384+
}
385+
304386

305387
/**
306388
* Replaces a header item with a new one
@@ -331,16 +413,7 @@ private function callEndpoint($parameters, $subUrl)
331413
$this->subUrl = $subUrl;
332414
$resultObject = '';
333415

334-
// if custom headers exist, add them
335-
if($parameters->customHeaders != null){
336-
foreach ($parameters->customHeaders as $key => $value) {
337-
if(strpos($value, "X") === 0 && strpos($value, "-") === 1){
338-
array_push($this->headers, $value);
339-
} else {
340-
throw new RosetteException("Custom headers must start with \"X-\"");
341-
}
342-
}
343-
}
416+
$this->headers = $this->addHeaders($this->headers);
344417

345418
if (strlen($parameters->getMultiPartContent()) > 0) {
346419
$content = $parameters->getMultiPartContent();

source/rosette/api/DocumentParameters.php

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,6 @@ class DocumentParameters extends RosetteParamsSetBase
5353
*/
5454
public $genre;
5555

56-
/**
57-
* @var array customHeaders to allow user to provide custom headers for an API request
58-
*/
59-
public $customHeaders;
60-
6156
/**
6257
* Constructor.
6358
*
@@ -160,19 +155,4 @@ public function loadDocumentString($stringData, $multiPart = false)
160155
$this->multiPartContent = '';
161156
}
162157
}
163-
164-
/**
165-
* Loads a custom header into the object.
166-
*
167-
*
168-
* @param $customHeaders : Array of custom headers to be passed during the request
169-
*
170-
*
171-
* @throws RosetteException
172-
*/
173-
174-
public function loadCustomHeaders($headers)
175-
{
176-
$this->customHeaders = $headers;
177-
}
178158
}

source/rosette/api/NameSimilarityParameters.php

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,6 @@ class NameSimilarityParameters extends RosetteParamsSetBase
3434
* @var string genre to categorize the input data
3535
*/
3636
public $genre;
37-
/**
38-
* @var array customHeaders to allow user to provide custom headers for an API request
39-
*/
40-
public $customHeaders;
4137
/**
4238
* constructor.
4339
*
@@ -71,19 +67,4 @@ public function validate()
7167
);
7268
}
7369
}
74-
75-
/**
76-
* Loads a custom header into the object.
77-
*
78-
*
79-
* @param $customHeaders : Array of custom headers to be passed during the request
80-
*
81-
*
82-
* @throws RosetteException
83-
*/
84-
85-
public function loadCustomHeaders($headers)
86-
{
87-
$this->customHeaders = $headers;
88-
}
8970
}

source/rosette/api/NameTranslationParameters.php

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,6 @@ class NameTranslationParameters extends RosetteParamsSetBase
5858
* @var string genre to categorize the input data
5959
*/
6060
public $genre;
61-
/**
62-
* @var array customHeaders to allow user to provide custom headers for an API request
63-
*/
64-
public $customHeaders;
6561
/**
6662
* constructor.
6763
*/
@@ -89,19 +85,4 @@ public function validate()
8985
);
9086
}
9187
}
92-
93-
/**
94-
* Loads a custom header into the object.
95-
*
96-
*
97-
* @param $customHeaders : Array of custom headers to be passed during the request
98-
*
99-
*
100-
* @throws RosetteException
101-
*/
102-
103-
public function loadCustomHeaders($headers)
104-
{
105-
$this->customHeaders = $headers;
106-
}
10788
}

spec/rosette/api/ApiSpec.php

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -275,15 +275,14 @@ public function it_fails_with_non_200_response($params, $request)
275275

276276
public function it_fails_with_incorrectly_formatted_custom_header($params, $request)
277277
{
278-
$params->beADoubleOf('\rosette\api\DocumentParameters');
279-
$params->contentUri = 'http://some.dummysite.com';
280-
$appHeader = [];
281-
$appHeader = "RosetteAPI-App: php-app";
282-
$customHeaders = [];
283-
$customHeaders[0] = $appHeader;
284-
$params->set('content', "some content");
285-
$params->loadCustomHeaders($customHeaders);
286-
$this->shouldThrow('rosette\api\RosetteException')->duringLanguage($params);
278+
$this->shouldThrow('rosette\api\RosetteException')->duringSetCustomHeaders(array("test"));
279+
}
287280

281+
public function it_sets_gets_clears_customHeaders()
282+
{
283+
$this->setCustomHeaders(array('X-RosetteAPI-test'));
284+
$this->getCustomHeaders()->shouldBe(array('X-RosetteAPI-test'));
285+
$this->clearCustomHeaders();
286+
$this->getCustomHeaders()->shouldBe(array());
288287
}
289288
}

0 commit comments

Comments
 (0)