Skip to content
This repository was archived by the owner on Jul 6, 2020. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@




#################
## Eclipse
#################

*.pydevproject
.project
.metadata
bin/
tmp/
*.tmp
*.bak
*.swp
*~.nib
local.properties
.classpath
.settings/
.loadpath

# External tool builders
.externalToolBuilders/

# Locally stored "Eclipse launch configurations"
*.launch

# CDT-specific
.cproject

# PDT-specific
.buildpath
42 changes: 40 additions & 2 deletions RestService/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,23 @@ public function getController()
return $this->controller;
}

/**
* @param string $out
*/
protected function output($out){
if( !ini_get('zlib.output_compression') && 'ob_gzhandler' != ini_get('output_handler') && isset($_SERVER['HTTP_ACCEPT_ENCODING']) ){
header('Vary: Accept-Encoding'); // Handle proxies
if( false !== stripos($_SERVER['HTTP_ACCEPT_ENCODING'], 'deflate') && function_exists('gzdeflate') ){

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This condition is the YODA condition.

Does this package use YODA condition?

header('Content-Encoding: deflate');
$out = gzdeflate($out, 3);
} elseif( false !== stripos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') && function_exists('gzencode') ){
header('Content-Encoding: gzip');
$out = gzencode($out, 3);
}
}
echo $out;
}

/**
* Sends the actual response.
*
Expand All @@ -145,7 +162,7 @@ public function sendResponse($pHttpCode = '200', $pMessage)
$pMessage = array_reverse($pMessage, true);

$method = $this->getOutputFormatMethod($this->getOutputFormat());
echo $this->$method($pMessage);
self::output($this->$method($pMessage));
exit;
}

Expand Down Expand Up @@ -237,6 +254,27 @@ public function asJSON($pMessage)
return $result;
}

function prepareUTF8($matches){
return json_decode('"'.$matches[1].'"');
}

/**
* Format data to JSON
*
* @param string $data The original data to process.
* @return string JSON string.
*/
public function jsonEncode($data)
{
if (defined('JSON_UNESCAPED_UNICODE')) {
return json_encode($data, JSON_UNESCAPED_UNICODE);
}else{
return preg_replace_callback('/((\\\u[01-9a-fA-F]{4})+)/',
array($this, 'prepareUTF8'), json_encode($data)
);
}
}

/**
* Indents a flat JSON string to make it more human-readable.
*
Expand All @@ -248,7 +286,7 @@ public function asJSON($pMessage)
*/
public function jsonFormat($json)
{
if (!is_string($json)) $json = json_encode($json);
if (!is_string($json)) $json = $this->jsonEncode($json);

$result = '';
$pos = 0;
Expand Down
9 changes: 9 additions & 0 deletions Test/Controller/MyRoutes.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,13 @@ public function getTest()
return 'test';
}

/**
* @param string $text
* @return string
*/
public function postEcho($text)
{
return $text;
}

}
48 changes: 48 additions & 0 deletions Test/Synthetic/UnicodeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace Test\Synthetic;

use RestService\Server;
use Test\Controller\MyRoutes;

class UnicodeTest extends \PHPUnit_Framework_TestCase
{
/**
* @var Server
*/
private $restService;

public function setUp()
{
$this->restService = Server::create('/', new MyRoutes)
->setClient('RestService\\InternalClient')
->collectRoutes();
}

private function assertEcho($test_string){
$response = $this->restService->simulateCall("/echo?text=$test_string", 'post');
$this->assertEquals("{
\"status\": 200,
\"data\": \"$test_string\"
}", $response);
}

public function testUnicode()
{
$this->assertEcho('ru: В чащах юга жил бы цитрус? Да, но фальшивый экземпляр!');
$this->assertEcho('da: Quizdeltagerne spiste jordbær med fløde, mens cirkusklovnen Wolther spillede på xylofon');
$this->assertEcho('de: Falsches Üben von Xylophonmusik quält jeden größeren Zwerg');
$this->assertEcho('el: Γαζέες καὶ μυρτιὲς δὲν θὰ βρῶ πιὰ στὸ χρυσαφὶ ξέφωτο');
$this->assertEcho('es: El pingüino Wenceslao hizo kilómetros bajo exhaustiva lluvia y frío, añoraba a su querido cachorro.');
$this->assertEcho('fr: Le cœur déçu mais l\'âme plutôt naïve, Louÿs rêva de crapaüter en canoë au delà des îles, près du mälström où brûlent les novæ.');
$this->assertEcho('ga: D\'fhuascail Íosa, Úrmhac na hÓighe Beannaithe, pór Éava agus Ádhaimh');
$this->assertEcho('hu: Árvíztűrő tükörfúrógép');
$this->assertEcho('is: Kæmi ný öxi hér ykist þjófum nú bæði víl og ádrepa');
$this->assertEcho('jp1: いろはにほへとちりぬるを わかよたれそつねならむ うゐのおくやまけふこえて あさきゆめみしゑひもせす');
$this->assertEcho('jp2: イロハニホヘト チリヌルヲ ワカヨタレソ ツネナラム ウヰノオクヤマ ケフコエテ アサキユメミシ ヱヒモセスン');
$this->assertEcho('iw: דג סקרן שט בים מאוכזב ולפתע מצא לו חברה איך הקליטה');
$this->assertEcho('pl: Pchnąć w tę łódź jeża lub ośm skrzyń fig');
$this->assertEcho('th: ๏ เป็นมนุษย์สุดประเสริฐเลิศคุณค่า กว่าบรรดาฝูงสัตว์เดรัจฉาน จงฝ่าฟันพัฒนาวิชาการ อย่าล้างผลาญฤๅเข่นฆ่าบีฑาใคร ไม่ถือโทษโกรธแช่งซัดฮึดฮัดด่า หัดอภัยเหมือนกีฬาอัชฌาสัย ปฏิบัติประพฤติกฎกำหนดใจ พูดจาให้จ๊ะๆ จ๋าๆ น่าฟังเอย ฯ');
$this->assertEcho('tr: Pijamalı hasta, yağız şoföre çabucak güvendi');
}
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It misses end of file.