public function testSaveFile() { $obj = new _Json(); $json_data = ['key1' => 'value1', 'key2' => 'value2']; $tmp_filename = TEST_FILES . 'tmp.json'; // Check standard output $obj->saveFile($json_data, $tmp_filename); $file_content = file_get_contents($tmp_filename); $this->string($file_content)->isEqualTo('{"key1":"value1","key2":"value2"}'); // Check prettified output $obj->saveFile($json_data, $tmp_filename, true); $file_content = file_get_contents($tmp_filename); $this->string($file_content)->isEqualTo("{\n \"key1\": \"value1\",\n \"key2\": \"value2\"\n}"); // Remove temp file unlink($tmp_filename); }
<?php use Json\Json; $file = INSTALL_ROOT . 'data/stats.json'; $link = isset($_GET['link']) ? $_GET['link'] : ''; $json_obj = new Json($file); if (file_exists($file)) { $stats = $json_obj->fetchContent(); if (!is_array($stats)) { trigger_error("JSON content is not an array, aborting stats recording.", E_USER_ERROR); exit; } } else { $stats = []; } $stats[$link] = in_array($link, array_keys($stats)) ? $stats[$link] + 1 : 1; $json_obj->saveFile($stats, $file, true);
<?php namespace Transvision; use Json\Json; // Create a JSON file logging locale/number of requests $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; $json_data->saveFile($stats, $local_filename); // Create a JSON file logging search options to determine if some are unused $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) { $stats[$k] = array_key_exists($k, $stats) ? $stats[$k] += 1 : 1; } if (in_array($k, array_diff($form_search_options, $form_checkboxes))) { $stats[$v] = array_key_exists($v, $stats) ? $stats[$v] += 1 : 1; } $json_data->saveFile($stats, $local_filename); } unset($stats);