Update codebase to use new Json class

This commit is contained in:
Timothy Warren 2016-01-07 13:45:43 -05:00
parent fa4940f22d
commit 275b0eea40
13 changed files with 48 additions and 75 deletions

View File

@ -63,36 +63,6 @@ class AnimeClient {
return ($a !== $b) ? 'selected' : '';
}
/**
* Decode a json file into a php data structure
*
* @param string $file
* @param bool $as_array
* @return array|object
*/
public static function json_file_decode($file, $as_array=TRUE)
{
return json_decode(
file_get_contents($file),
$as_array
);
}
/**
* Encode json data and save to the selected file
*
* @param string $file
* @param mixed $data
* @return bool
*/
public static function json_file_encode($file, $data)
{
return file_put_contents(
$file,
json_encode($data)
);
}
/**
* Determine whether to show the sub-menu
*

View File

@ -13,7 +13,7 @@
namespace Aviat\AnimeClient\Model;
use Aviat\AnimeClient\AnimeClient;
use Aviat\Ion\Json;
use Aviat\AnimeClient\Hummingbird\Enum\AnimeWatchingStatus;
use Aviat\AnimeClient\Hummingbird\Transformer\AnimeListTransformer;
@ -70,7 +70,7 @@ class Anime extends API {
return [
'statusCode' => $response->getStatusCode(),
'body' => json_decode($response->getBody(), TRUE)
'body' => Json::decode($response->getBody(), TRUE)
];
}
@ -138,7 +138,7 @@ class Anime extends API {
$response = $this->client->get("anime/{$anime_id}", $config);
return json_decode($response->getBody(), TRUE);
return Json::decode($response->getBody(), TRUE);
}
/**
@ -165,7 +165,7 @@ class Anime extends API {
throw new RuntimeException($response->getEffectiveUrl());
}
return json_decode($response->getBody(), TRUE);
return Json::decode($response->getBody(), TRUE);
}
/**
@ -218,20 +218,20 @@ class Anime extends API {
$transformed_cache_file = _dir($this->config->get('data_cache_path'), "anime-{$status}-transformed.json");
$cached = (file_exists($cache_file))
? AnimeClient::json_file_decode($cache_file)
? Json::decodeFile($cache_file)
: [];
$api_data = json_decode($response->getBody(), TRUE);
$api_data = Json::decode($response->getBody(), TRUE);
if ($api_data === $cached && file_exists($transformed_cache_file))
{
return AnimeClient::json_file_decode($transformed_cache_file);
return Json::decodeFile($transformed_cache_file);
}
else
{
AnimeClient::json_file_encode($cache_file, $api_data);
Json::encodeFile($cache_file, $api_data);
$transformer = new AnimeListTransformer();
$transformed = $transformer->transform_collection($api_data);
AnimeClient::json_file_encode($transformed_cache_file, $transformed);
Json::encodeFile($transformed_cache_file, $transformed);
return $transformed;
}
}

View File

@ -13,6 +13,7 @@
namespace Aviat\AnimeClient\Model;
use Aviat\Ion\Json;
use Aviat\Ion\Di\ContainerInterface;
use Aviat\AnimeClient\AnimeClient;
use Aviat\AnimeClient\Model\Anime as AnimeModel;
@ -306,7 +307,7 @@ class AnimeCollection extends DB {
return;
}
$anime = AnimeClient::json_file_decode("import.json");
$anime = Json::decodeFile("import.json");
foreach ($anime as $item)
{

View File

@ -16,7 +16,7 @@ namespace Aviat\AnimeClient\Model;
use GuzzleHttp\Cookie\Cookiejar;
use GuzzleHttp\Cookie\SetCookie;
use Aviat\AnimeClient\AnimeClient;
use Aviat\Ion\Json;
use Aviat\AnimeClient\Model\API;
use Aviat\AnimeClient\Hummingbird\Transformer;
use Aviat\AnimeClient\Hummingbird\Enum\MangaReadingStatus;
@ -80,7 +80,7 @@ class Manga extends API {
return [
'statusCode' => $result->getStatusCode(),
'body' => json_decode($result->getBody(), TRUE)
'body' => Json::decode($result->getBody(), TRUE)
];
}
@ -149,7 +149,7 @@ class Manga extends API {
private function _check_cache($response)
{
// Bail out early if there isn't any manga data
$api_data = json_decode($response->getBody(), TRUE);
$api_data = Json::decode($response->getBody(), TRUE);
if ( ! array_key_exists('manga', $api_data))
{
return [];
@ -162,21 +162,21 @@ class Manga extends API {
);
$cached_data = file_exists($cache_file)
? AnimeClient::json_file_decode($cache_file)
? Json::decodeFile($cache_file)
: [];
if ($cached_data === $api_data && file_exists($transformed_cache_file))
{
return AnimeClient::json_file_decode($transformed_cache_file);
return Json::decodeFile($transformed_cache_file);
}
else
{
AnimeClient::json_file_encode($cache_file, $api_data);
Json::encodeFile($cache_file, $api_data);
$zippered_data = $this->zipper_lists($api_data);
$transformer = new Transformer\MangaListTransformer();
$transformed_data = $transformer->transform_collection($zippered_data);
AnimeClient::json_file_encode($transformed_cache_file, $transformed_data);
Json::encodeFile($transformed_cache_file, $transformed_data);
return $transformed_data;
}
}

View File

@ -69,12 +69,15 @@ class Json {
* Decode json data loaded from the passed filename
*
* @param string $filename
* @param bool $assoc
* @param int $depth
* @param int $options
* @return mixed
*/
public static function decodeFile($filename)
public static function decodeFile($filename, $assoc = TRUE, $depth = 512, $options = 0)
{
$json = file_get_contents($filename);
return self::decode($json);
return self::decode($json, $assoc, $depth, $options);
}
/**

View File

@ -12,6 +12,7 @@
namespace Aviat\Ion\View;
use Aviat\Ion\Json;
use Aviat\Ion\View\HttpView;
use Aviat\Ion\View as BaseView;
@ -37,7 +38,7 @@ class JsonView extends HttpView {
{
if ( ! is_string($string))
{
$string = json_encode($string);
$string = Json::encode($string);
}
return parent::setOutput($string);

View File

@ -1,6 +1,7 @@
<?php
use Aviat\Ion\Friend;
use Aviat\Ion\Json;
use Aviat\AnimeClient\Hummingbird\Transformer\AnimeListTransformer;
class AnimeListTransformerTest extends AnimeClient_TestCase {
@ -41,8 +42,8 @@ class AnimeListTransformerTest extends AnimeClient_TestCase {
public function testTransform()
{
$json = json_file_decode($this->start_file);
$expected = json_file_decode($this->res_file);
$json = Json::decodeFile($this->start_file);
$expected = Json::decodeFile($this->res_file);
$actual = $this->transformer->transform_collection($json);
//file_put_contents($this->res_file, json_encode($actual));
$this->assertEquals($expected, $actual);

View File

@ -1,5 +1,6 @@
<?php
use Aviat\Ion\Json;
use Aviat\AnimeClient\Hummingbird\Transformer\MangaListTransformer;
class MangaListTransformerTest extends AnimeClient_TestCase {
@ -15,8 +16,8 @@ class MangaListTransformerTest extends AnimeClient_TestCase {
public function testTransform()
{
$orig_json = json_file_decode($this->start_file);
$expected = json_file_decode($this->res_file);
$orig_json = Json::decodeFile($this->start_file);
$expected = Json::decodeFile($this->res_file);
$actual = $this->transformer->transform_collection($orig_json);
$this->assertEquals($expected, $actual);

View File

@ -1,5 +1,6 @@
<?php
use Aviat\Ion\Json;
use Aviat\AnimeClient\Hummingbird\Transformer\MangaListsZipper;
class MangaListsZipperTest extends AnimeClient_TestCase {
@ -12,13 +13,13 @@ class MangaListsZipperTest extends AnimeClient_TestCase {
$this->start_file = __DIR__ . '/../../../test_data/manga_list/manga.json';
$this->res_file = __DIR__ . '/../../../test_data/manga_list/manga-zippered.json';
$json = json_file_decode($this->start_file);
$json = Json::decodeFile($this->start_file);
$this->mangaListsZipper = new MangaListsZipper($json);
}
public function testTransform()
{
$zippered_json = json_file_decode($this->res_file);
$zippered_json = Json::decodeFile($this->res_file);
$transformed = $this->mangaListsZipper->transform();
$this->assertEquals($zippered_json, $transformed);

View File

@ -2,6 +2,7 @@
use GuzzleHttp\Psr7\Response;
use Aviat\Ion\Friend;
use Aviat\Ion\Json;
use Aviat\Ion\Di\ContainerInterface;
use Aviat\AnimeClient\Model\Manga as MangaModel;
use Aviat\AnimeClient\Hummingbird\Enum\MangaReadingStatus;
@ -17,16 +18,16 @@ class MangaModelTest extends AnimeClient_TestCase {
public function testZipperLists()
{
$raw_data = json_file_decode($this->mockDir . '/manga.json');
$expected = json_file_decode($this->mockDir . '/manga-zippered.json');
$raw_data = Json::decodeFile($this->mockDir . '/manga.json');
$expected = Json::decodeFile($this->mockDir . '/manga-zippered.json');
$this->assertEquals($expected, $this->model->zipper_lists($raw_data));
}
public function testMapByStatus()
{
$original = json_file_decode($this->mockDir . '/manga-transformed.json');
$expected = json_file_decode($this->mockDir . '/manga-mapped.json');
$original = Json::decodeFile($this->mockDir . '/manga-transformed.json');
$expected = Json::decodeFile($this->mockDir . '/manga-mapped.json');
$actual = $this->model->map_by_status($original);
$this->assertEquals($expected, $actual);
@ -43,7 +44,7 @@ class MangaModelTest extends AnimeClient_TestCase {
$reflect = new ReflectionClass($this->model);
$constants = $reflect->getConstants();
$expected_all = json_file_decode($this->mockDir . '/manga-mapped.json');
$expected_all = Json::decodeFile($this->mockDir . '/manga-mapped.json');
$this->assertEquals($expected_all, $this->model->_get_list_from_api());
@ -74,7 +75,7 @@ $this->markTestSkipped();
$this->markTestSkipped();
}
$data = json_file_decode($this->mockDir . '/manga-mapped.json');
$data = Json::decodeFile($this->mockDir . '/manga-mapped.json');
foreach($data as &$val)
{

View File

@ -17,6 +17,10 @@ class ViewTest extends AnimeClient_TestCase {
{
$this->friend->output = 'foo';
$this->assertEquals($this->friend->output, $this->friend->getOutput());
$this->assertFalse($this->friend->hasRendered);
$this->assertEquals($this->friend->getOutput(), $this->friend->__toString());
$this->assertTrue($this->friend->hasRendered);
}
public function testSetOutput()

View File

@ -3,6 +3,7 @@
* Global setup for unit tests
*/
use Aviat\Ion\Json;
use Aviat\AnimeClient\AnimeClient;
// -----------------------------------------------------------------------------
@ -20,18 +21,6 @@ function _dir()
return implode(DIRECTORY_SEPARATOR, func_get_args());
}
/**
* Decode a json file into a php data structure
*
* @param string $file
* @param bool $as_array
* @return array|object
*/
function json_file_decode($file, $as_array=TRUE)
{
return AnimeClient::json_file_decode($file, $as_array);
}
// -----------------------------------------------------------------------------
// Autoloading
// -----------------------------------------------------------------------------

View File

@ -5,6 +5,7 @@
use Aviat\Ion\Enum;
use Aviat\Ion\Friend;
use Aviat\Ion\Json;
use Aviat\Ion\Di\ContainerInterface;
use Aviat\Ion\Transformer\AbstractTransformer;
use Aviat\Ion\View;
@ -151,7 +152,7 @@ class TestMangaModel extends MangaModel {
protected function _check_cache($response)
{
$file = __DIR__ . '/test_data/manga_list/manga-transformed.json';
return json_file_decode($file);
return Json::decodeFile($file);
}
}
// End of mocks.php