/
JsonDecoder.php
54 lines (47 loc) · 1.25 KB
/
JsonDecoder.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<?php
namespace Michaels\Manager\Decoders;
use Michaels\Manager\Contracts\DecoderInterface;
use Michaels\Manager\Exceptions\JsonDecodingFailedException;
/**
* A standard Json Decoder Module for the data manager file loader.
*
* @package Michaels\Manager
*/
class JsonDecoder implements DecoderInterface
{
/** @var array Decoded data */
protected $arrayData = [];
/**
* Decodes JSON data to array
*
* @param $data string
* @return array
* @throws JsonDecodingFailedException
*/
public function decode($data)
{
if (is_string($data)) {
$this->arrayData = json_decode($data, true); // true gives us associative arrays
}
if (!is_string($data) || !$this->isValidJson()) {
throw new JsonDecodingFailedException('The data provided was not proper JSON');
}
return $this->arrayData;
}
/**
* Returns MimeType
* @return string[]
*/
public function getMimeType()
{
return ['json'];
}
/**
* Checks if the input is really a json string and if the PHP Json decoding was successful.
* @return boolean
*/
protected function isValidJson()
{
return (json_last_error() === JSON_ERROR_NONE);
}
}