Permalink
Browse files

Use external library for JSON

  • Loading branch information...
flodolo committed Nov 19, 2015
1 parent 1e13de2 commit 268850bef2a95a448a78e5ca958096d82f776e60
@@ -1,51 +0,0 @@
<?php
namespace Transvision;
/**
* Json class
*
* All the methods we need to work with or generate Json data
*
* @package Transvision
*/
class Json
{
/**
* Transforms an array into JSON/JSONP
*
* @param array $data The data we want to convert to json
* @param boolean $jsonp Optional, false by default, true to generate JSONP
* @param boolean $pretty_print Optional. Output as readable JSON_PRETTY_PRINT
* @return string Json data
*/
public static function output(array $data, $jsonp = false, $pretty_print = false)
{
$json = $pretty_print ? json_encode($data, JSON_PRETTY_PRINT) : json_encode($data);
$mime = 'application/json';
if ($jsonp) {
$mime = 'application/javascript';
$json = $jsonp . '(' . $json . ')';
}
ob_start();
header("access-control-allow-origin: *");
header("Content-type: {$mime}; charset=UTF-8");
echo $json;
$json = ob_get_contents();
ob_end_clean();
return $json;
}
/**
* Fetch a local or remote JSON source and returns as a PHP array
*
* @param string $uri Location of the json file, local or remote
* @return array Data converted to an array
*/
public static function fetch($uri)
{
return json_decode(file_get_contents($uri), true);
}
}
View
@@ -1,13 +1,23 @@
<?php
namespace Transvision;
use Json\Json;
// Create a JSON file logging locale/number of requests
$stats = Json::fetch(CACHE_PATH . 'stats_locales.json');
$json_data = new Json;
$local_filename = CACHE_PATH . 'stats_locales.json';
$stats = $json_data
->setURI($local_filename)
->fetchContent();
$stats[$locale] = array_key_exists($locale, $stats) ? $stats[$locale] += 1 : 1;
file_put_contents(CACHE_PATH . 'stats_locales.json', json_encode($stats));
$json_data->saveFile($stats, $local_filename);
// Create a JSON file logging search options to determine if some are unused
$stats = Json::fetch(CACHE_PATH . 'stats_requests.json');
$local_filename = CACHE_PATH . 'stats_requests.json';
$stats = $json_data
->setURI($local_filename)
->fetchContent();
foreach ($check as $k => $v) {
if (in_array($k, $form_checkboxes) && $v == 1) {
@@ -18,6 +28,6 @@
$stats[$v] = array_key_exists($v, $stats) ? $stats[$v] += 1 : 1;
}
file_put_contents(CACHE_PATH . 'stats_requests.json', json_encode($stats));
$json_data->saveFile($stats, $local_filename);
}
unset($stats);
View
@@ -1,15 +1,18 @@
<?php
namespace Transvision;
use Json\Json;
/*
* This view outputs a json or jsonp representation of search results
* This view outputs a JSON or JSONP representation of search results
*/
// Log script performance in PHP integrated developement server console
Utils::logScriptPerformances();
// We die here because we never want to send anything more after the Json file
die(Json::output(
// We die here because we never want to send anything more after the JSON file
$json_data = new Json;
die($json_data->outputContent(
$json,
isset($_GET['callback']) ? $_GET['callback'] : false
));
View
@@ -8,6 +8,10 @@
"type": "vcs",
"url": "https://github.com/mozilla-l10n/phpclass-cache"
},
{
"type": "vcs",
"url": "https://github.com/mozilla-l10n/phpclass-json"
},
{
"type": "vcs",
"url": "https://github.com/pascalchevrel/Verif/"
@@ -47,6 +51,7 @@
"monolog/monolog" : "~1.1",
"mozillal10n/bugzilla": "~0.1",
"mozillal10n/cache": "~0.1",
"mozillal10n/json": "~0.1",
"pascalc/tinyl10n" : "~0.1",
"pascalc/vcs": "~0.1",
"robloach/component-installer": "*",
@@ -1,2 +0,0 @@
{
}
@@ -1,2 +0,0 @@
{
}
@@ -1 +0,0 @@
{"user_name": "Pascal"}
@@ -1,64 +0,0 @@
<?php
namespace tests\units\Transvision;
use atoum;
use Transvision\Json as _Json;
require_once __DIR__ . '/../bootstrap.php';
class Json extends atoum\test
{
public function fetchDP()
{
return [
[TEST_FILES . 'json/empty.json', []],
[TEST_FILES . 'json/test1.json', ['user_name' => 'Pascal']],
];
}
/**
* @dataProvider fetchDP
*/
public function testFetch($a, $b)
{
$obj = new _Json();
$this
->array($obj->fetch($a))
->isEqualTo($b);
}
public function outputDP()
{
return [
[
['foo' => 'bar'],
false,
false,
'{"foo":"bar"}',
],
[
['foo' => 'bar'],
false,
true,
"{\n " . '"foo": "bar"' . "\n}",
],
[
['foo' => 'bar'],
'toto',
false,
'toto({"foo":"bar"})',
],
];
}
/**
* @dataProvider outputDP
*/
public function testOutput($a, $b, $c, $d)
{
$obj = new _Json();
$this
->string($obj->output($a, $b, $c))
->isEqualTo($d);
}
}

0 comments on commit 268850b

Please sign in to comment.