Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00%
0 / 1
66.67%
2 / 3
CRAP
88.89%
16 / 18
Json
0.00%
0 / 1
66.67%
2 / 3
7.07
88.89%
16 / 18
 __construct($filename)
100.00%
1 / 1
1
100.00%
2 / 2
 read()
0.00%
0 / 1
3.33
66.67%
4 / 6
 write()
100.00%
1 / 1
3
100.00%
10 / 10
<?php
/**
* This file is part of the Statistical Classifier package.
*
* (c) Cam Spiers <camspiers@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Camspiers\StatisticalClassifier\DataSource;
/**
* @author Cam Spiers <camspiers@gmail.com>
* @package Statistical Classifier
*/
class Json extends DataArray
{
/**
* The filename of the json file
* @var string
*/
private $filename;
/**
* Creates the object from the filename
* @param string $filename The filename of the json file
*/
public function __construct($filename)
{
$this->filename = $filename;
}
/**
* @{inheritdoc}
*/
public function read()
{
if (file_exists($this->filename)) {
$data = json_decode(file_get_contents($this->filename), true);
if (is_array($data)) {
return $data;
}
}
return array();
}
/**
* @{inheritdoc}
*/
public function write()
{
$data = array();
foreach ($this->data as $category => $documents) {
foreach ($documents as $document) {
$data[] = array(
'category' => $category,
'document' => $document
);
}
}
file_put_contents($this->filename, json_encode($data));
}
}