/** * Writes JSON to a file * * @param $file * * @param $contents * * @return null */ public static function Write($file, $contents) { /** * Does this file exist? */ if (JsonWriter::FileExists($file)) { /** * Don't overwrite */ return null; } /** * Does this directory exist? */ if (!JsonWriter::DirectoryExists(JsonWriter::GetDirectories($file))) { JsonWriter::MakeDirectory(JsonWriter::GetDirectories($file)); } /** * Writes this data to file. */ JsonWriter::WriteData($file, json_encode($contents, JSON_PRETTY_PRINT)); /** * Returns the file on a successful completion */ return JsonReader::ReadFile($file); }
/** * Writes a json file * * @param $path * * @param $contents * * @return mixed|null */ public static function WriteFile($path, $contents) { /** * Gets the correct path of this file */ $corrected_path = Path::Get($path); /** * If this file exists, return null ( use above function ) */ if (file_exists($path)) { return null; } /** * Else, Lets first create directories */ JsonWriter::CreateDirectories($path); /** * Then, lets write to file */ JsonWriter::PutContents($corrected_path, $contents); /** * Then lets return this written file */ return JsonReader::ReadFile($corrected_path); }
/** * Because I kept writing this a lot * * @param $file * * @return mixed|null */ public static function PathRead($file) { $file = JsonReader::GetRawFile(PathFinder::Path($file)); if ($file != null) { return json_decode($file, true); } return null; }
/** * Reads the file * * @param $path * * @return mixed|null */ public static function ReadFile($path) { /** * Get the correct path */ $corrected_path = Path::Get($path); /** * Does this exist? */ if (file_exists($corrected_path)) { /** * Return the encoded json */ return json_decode(JsonReader::OpenFile($corrected_path), true); } /** * This does not exist! */ return null; }
/** * Reads the saved path. * * @param $correct_path * * @return mixed|null */ private static function ReadSavedPath($correct_path) { if (FileMethods::FileExists($correct_path . "Json/resource/filepath.json")) { return JsonReader::ReadFile($correct_path . "Json/resource/filepath.json"); } return null; }
function parse_gist($raw_gist) { // var_dump($raw_gist); $reader = new JsonReader($raw_gist); return $reader->get_path('files,gistfile1.txt,content'); }
/** * @test * @covers Plum\PlumJson\JsonReader::accepts() */ public function acceptsReturnsFalseIfInputIsNotArray() { $this->assertFalse(JsonReader::accepts('foo')); }
/** * Read tor exit points. * * @return mixed|null */ public static function ReadTorExitPoints() { if (FileMethods::FileExists(PathFinder::Path('Json/resources/tor_exit_points.json'))) { return JsonReader::PathRead('Json/resources/tor_exit_points.json'); } return null; }
/** * Reads the default parser needles * * @return mixed|null */ public static function ReadDefaults() { return JsonReader::ReadFile(PathFinder::RealPath("Json/misc/default_parser_needles.json")); }
<?php // include $_SERVER['DOCUMENT_ROOT'].'/verify/shared/errors.php'; include 'test_helper.php'; include '../json_reader.php'; // mimicking json from eyal $json = load_json('verified'); $reader = new JsonReader($json); // var_dump($reader); $path = 'social,twitter,pid'; $twitter_pid_test = $reader->get_path($path) == '651645990554968064' ? PASS : FAIL; echo "<br/>twitter_pid_test: [" . $twitter_pid_test . "]"; if ($twitter_pid_test == FAIL) { var_dump($reader); } $path = 'social,twitter,aid'; $twitter_aid_test = $reader->get_path($path) == 'LJEC6Q2h9JKNvZqEC87TbEXvxm4br1uivb2QX' ? PASS : FAIL; echo "<br/>twitter_aid_test: [" . $twitter_aid_test . "]"; if ($twitter_aid_test == FAIL) { var_dump($reader); } $path = 'domain,url'; $bofa_url_test = $reader->get_path($path) == 'https://www.bankofamerica.com' ? PASS : FAIL; echo "<br/>bofa_url_test: [" . $bofa_url_test . "]"; if ($bofa_url_test == FAIL) { var_dump($reader); } ?>
/** * @test * @expectedException \Jsor\LocaleData\Exception\InvalidJsonException */ public function read_throws_for_invalid_json() { $reader = new JsonReader(); $reader->read(__DIR__ . '/_fixtures', 'invalid'); }