Update codebase to use new Json class

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

View File

@ -63,36 +63,6 @@ class AnimeClient {
return ($a !== $b) ? 'selected' : ''; 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 * Determine whether to show the sub-menu
* *

View File

@ -13,7 +13,7 @@
namespace Aviat\AnimeClient\Model; namespace Aviat\AnimeClient\Model;
use Aviat\AnimeClient\AnimeClient; use Aviat\Ion\Json;
use Aviat\AnimeClient\Hummingbird\Enum\AnimeWatchingStatus; use Aviat\AnimeClient\Hummingbird\Enum\AnimeWatchingStatus;
use Aviat\AnimeClient\Hummingbird\Transformer\AnimeListTransformer; use Aviat\AnimeClient\Hummingbird\Transformer\AnimeListTransformer;
@ -70,7 +70,7 @@ class Anime extends API {
return [ return [
'statusCode' => $response->getStatusCode(), '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); $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()); 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"); $transformed_cache_file = _dir($this->config->get('data_cache_path'), "anime-{$status}-transformed.json");
$cached = (file_exists($cache_file)) $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)) if ($api_data === $cached && file_exists($transformed_cache_file))
{ {
return AnimeClient::json_file_decode($transformed_cache_file); return Json::decodeFile($transformed_cache_file);
} }
else else
{ {
AnimeClient::json_file_encode($cache_file, $api_data); Json::encodeFile($cache_file, $api_data);
$transformer = new AnimeListTransformer(); $transformer = new AnimeListTransformer();
$transformed = $transformer->transform_collection($api_data); $transformed = $transformer->transform_collection($api_data);
AnimeClient::json_file_encode($transformed_cache_file, $transformed); Json::encodeFile($transformed_cache_file, $transformed);
return $transformed; return $transformed;
} }
} }

View File

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

View File

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

View File

@ -69,12 +69,15 @@ class Json {
* Decode json data loaded from the passed filename * Decode json data loaded from the passed filename
* *
* @param string $filename * @param string $filename
* @param bool $assoc
* @param int $depth
* @param int $options
* @return mixed * @return mixed
*/ */
public static function decodeFile($filename) public static function decodeFile($filename, $assoc = TRUE, $depth = 512, $options = 0)
{ {
$json = file_get_contents($filename); $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; namespace Aviat\Ion\View;
use Aviat\Ion\Json;
use Aviat\Ion\View\HttpView; use Aviat\Ion\View\HttpView;
use Aviat\Ion\View as BaseView; use Aviat\Ion\View as BaseView;
@ -37,7 +38,7 @@ class JsonView extends HttpView {
{ {
if ( ! is_string($string)) if ( ! is_string($string))
{ {
$string = json_encode($string); $string = Json::encode($string);
} }
return parent::setOutput($string); return parent::setOutput($string);

View File

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

View File

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

View File

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

View File

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

View File

@ -17,6 +17,10 @@ class ViewTest extends AnimeClient_TestCase {
{ {
$this->friend->output = 'foo'; $this->friend->output = 'foo';
$this->assertEquals($this->friend->output, $this->friend->getOutput()); $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() public function testSetOutput()

View File

@ -3,6 +3,7 @@
* Global setup for unit tests * Global setup for unit tests
*/ */
use Aviat\Ion\Json;
use Aviat\AnimeClient\AnimeClient; use Aviat\AnimeClient\AnimeClient;
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
@ -20,18 +21,6 @@ function _dir()
return implode(DIRECTORY_SEPARATOR, func_get_args()); 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 // Autoloading
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------

View File

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