Skip to content

Commit 1595b96

Browse files
author
Chris Park
committed
Merge branch 'RCB-412' of github.com:samhausmann/php
2 parents 75a3463 + 73cacd1 commit 1595b96

File tree

6 files changed

+88
-4
lines changed

6 files changed

+88
-4
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": {

docker/run_php.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ fi
120120

121121

122122
#Run unit tests
123-
cd /php-dev && ./bin/phpspec run
123+
cd /php-dev && ./bin/phpspec run --config=phpspec.yml --bootstrap=./vendor/autoload.php --no-interaction --format=pretty
124124

125125
#Run php-cs-fixer
126126
./bin/php-cs-fixer fix . --dry-run --diff --level=psr2

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 & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
$params = new DocumentParameters();
1919
$content = $language_data;
2020
$params->set('content', $content);
21+
$customHeader = "X-RosetteAPI-App: php-app";
22+
$api->setCustomHeaders($customHeader);
2123

2224
try {
2325
$result = $api->language($params);

source/rosette/api/Api.php

Lines changed: 70 additions & 0 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",
@@ -301,6 +309,65 @@ public function clearOptions()
301309
$this->options = array();
302310
}
303311

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

305372
/**
306373
* Replaces a header item with a new one
@@ -331,6 +398,8 @@ private function callEndpoint($parameters, $subUrl)
331398
$this->subUrl = $subUrl;
332399
$resultObject = '';
333400

401+
$this->headers = $this->addHeaders($this->headers);
402+
334403
if (strlen($parameters->getMultiPartContent()) > 0) {
335404
$content = $parameters->getMultiPartContent();
336405
$filename = $parameters->fileName;
@@ -357,6 +426,7 @@ private function callEndpoint($parameters, $subUrl)
357426

358427
$resultObject = $this->postHttp($url, $this->headers, $multi);
359428
} else {
429+
360430
$url = $this->service_url . $this->subUrl;
361431
$resultObject = $this->postHttp($url, $this->headers, $parameters->serialize($this->options));
362432
}

spec/rosette/api/ApiSpec.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,4 +272,17 @@ public function it_fails_with_non_200_response($params, $request)
272272
$this->setMockRequest($request);
273273
$this->shouldThrow('rosette\api\RosetteException')->duringRelationships($params);
274274
}
275+
276+
public function it_fails_with_incorrectly_formatted_custom_header($params, $request)
277+
{
278+
$this->shouldThrow('rosette\api\RosetteException')->duringSetCustomHeaders("test");
279+
}
280+
281+
public function it_sets_gets_clears_customHeaders()
282+
{
283+
$this->setCustomHeaders('X-RosetteAPI-test');
284+
$this->getCustomHeaders()->shouldBe(array('X-RosetteAPI-test'));
285+
$this->clearCustomHeaders();
286+
$this->getCustomHeaders()->shouldBe(array());
287+
}
275288
}

0 commit comments

Comments
 (0)