Skip to content

Commit

Permalink
Update codebase to use new Json class
Browse files Browse the repository at this point in the history
  • Loading branch information
timw4mail committed Jan 7, 2016
1 parent fa4940f commit 275b0ee
Show file tree
Hide file tree
Showing 13 changed files with 48 additions and 75 deletions.
30 changes: 0 additions & 30 deletions src/Aviat/AnimeClient/AnimeClient.php
Expand Up @@ -63,36 +63,6 @@ public static function is_not_selected($a, $b)
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
*
Expand Down
18 changes: 9 additions & 9 deletions src/Aviat/AnimeClient/Model/Anime.php
Expand Up @@ -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;

Expand Down Expand Up @@ -70,7 +70,7 @@ public function update($data)

return [
'statusCode' => $response->getStatusCode(),
'body' => json_decode($response->getBody(), TRUE)
'body' => Json::decode($response->getBody(), TRUE)
];
}

Expand Down Expand Up @@ -138,7 +138,7 @@ public function get_anime($anime_id)

$response = $this->client->get("anime/{$anime_id}", $config);

return json_decode($response->getBody(), TRUE);
return Json::decode($response->getBody(), TRUE);
}

/**
Expand All @@ -165,7 +165,7 @@ public function search($name)
throw new RuntimeException($response->getEffectiveUrl());
}

return json_decode($response->getBody(), TRUE);
return Json::decode($response->getBody(), TRUE);
}

/**
Expand Down Expand Up @@ -218,20 +218,20 @@ protected function _check_cache($status, $response)
$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;
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/Aviat/AnimeClient/Model/AnimeCollection.php
Expand Up @@ -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;
Expand Down Expand Up @@ -306,7 +307,7 @@ private function json_import()
return;
}

$anime = AnimeClient::json_file_decode("import.json");
$anime = Json::decodeFile("import.json");

foreach ($anime as $item)
{
Expand Down
14 changes: 7 additions & 7 deletions src/Aviat/AnimeClient/Model/Manga.php
Expand Up @@ -16,7 +16,7 @@
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;
Expand Down Expand Up @@ -80,7 +80,7 @@ public function update($data)

return [
'statusCode' => $result->getStatusCode(),
'body' => json_decode($result->getBody(), TRUE)
'body' => Json::decode($result->getBody(), TRUE)
];
}

Expand Down Expand Up @@ -149,7 +149,7 @@ protected function _get_list_from_api($status = "All")
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 [];
Expand All @@ -162,21 +162,21 @@ private function _check_cache($response)
);

$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;
}
}
Expand Down
7 changes: 5 additions & 2 deletions src/Aviat/Ion/Json.php
Expand Up @@ -69,12 +69,15 @@ public static function decode($json, $assoc = TRUE, $depth = 512, $options = 0)
* 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);
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/Aviat/Ion/View/JsonView.php
Expand Up @@ -12,6 +12,7 @@

namespace Aviat\Ion\View;

use Aviat\Ion\Json;
use Aviat\Ion\View\HttpView;
use Aviat\Ion\View as BaseView;

Expand All @@ -37,7 +38,7 @@ public function setOutput($string)
{
if ( ! is_string($string))
{
$string = json_encode($string);
$string = Json::encode($string);
}

return parent::setOutput($string);
Expand Down
@@ -1,6 +1,7 @@
<?php

use Aviat\Ion\Friend;
use Aviat\Ion\Json;
use Aviat\AnimeClient\Hummingbird\Transformer\AnimeListTransformer;

class AnimeListTransformerTest extends AnimeClient_TestCase {
Expand Down Expand Up @@ -41,8 +42,8 @@ public function testLinearizeGenres($original, $expected)

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);
Expand Down
@@ -1,5 +1,6 @@
<?php

use Aviat\Ion\Json;
use Aviat\AnimeClient\Hummingbird\Transformer\MangaListTransformer;

class MangaListTransformerTest extends AnimeClient_TestCase {
Expand All @@ -15,8 +16,8 @@ public function setUp()

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);
Expand Down
@@ -1,5 +1,6 @@
<?php

use Aviat\Ion\Json;
use Aviat\AnimeClient\Hummingbird\Transformer\MangaListsZipper;

class MangaListsZipperTest extends AnimeClient_TestCase {
Expand All @@ -12,13 +13,13 @@ public function setUp()
$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);
Expand Down
13 changes: 7 additions & 6 deletions tests/AnimeClient/Model/MangaModelTest.php
Expand Up @@ -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;
Expand All @@ -17,16 +18,16 @@ public function setUp()

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);
Expand All @@ -43,7 +44,7 @@ public function testGetListFromApi()
$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());

Expand Down Expand Up @@ -74,7 +75,7 @@ public function testGetAllLists()
$this->markTestSkipped();
}

$data = json_file_decode($this->mockDir . '/manga-mapped.json');
$data = Json::decodeFile($this->mockDir . '/manga-mapped.json');

foreach($data as &$val)
{
Expand Down
4 changes: 4 additions & 0 deletions tests/Ion/ViewTest.php
Expand Up @@ -17,6 +17,10 @@ public function testGetOutput()
{
$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()
Expand Down
13 changes: 1 addition & 12 deletions tests/bootstrap.php
Expand Up @@ -3,6 +3,7 @@
* Global setup for unit tests
*/

use Aviat\Ion\Json;
use Aviat\AnimeClient\AnimeClient;

// -----------------------------------------------------------------------------
Expand All @@ -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
// -----------------------------------------------------------------------------
Expand Down
3 changes: 2 additions & 1 deletion tests/mocks.php
Expand Up @@ -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;
Expand Down Expand Up @@ -151,7 +152,7 @@ public function get_cached_image($api_path, $series_slug, $type = "anime")
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

0 comments on commit 275b0ee

Please sign in to comment.