|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Méthodes utiles pour les fichiers JSON. |
|
4
|
|
|
* |
|
5
|
|
|
* @author Eoxia <[email protected]> |
|
6
|
|
|
* @since 0.1.0 |
|
7
|
|
|
* @version 1.0.0 |
|
8
|
|
|
* @copyright 2015-2018 Eoxia |
|
9
|
|
|
* @package EO_Framework\Core\Util |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace eoxia; |
|
13
|
|
|
|
|
14
|
|
|
if ( ! defined( 'ABSPATH' ) ) { |
|
15
|
|
|
exit; |
|
16
|
|
|
} |
|
17
|
|
|
|
|
18
|
|
|
if ( ! class_exists( '\eoxia\JSON_Util' ) ) { |
|
19
|
|
|
/** |
|
20
|
|
|
* Méthodes utiles pour les fichiers JSON. |
|
21
|
|
|
*/ |
|
22
|
|
|
class JSON_Util extends \eoxia\Singleton_Util { |
|
23
|
|
|
/** |
|
24
|
|
|
* Le constructeur obligatoirement pour utiliser la classe \eoxia\Singleton_Util |
|
25
|
|
|
* |
|
26
|
|
|
* @return void nothing |
|
27
|
|
|
*/ |
|
28
|
|
|
protected function construct() {} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Ouvres et décode le fichier JSON $path_to_json |
|
32
|
|
|
* |
|
33
|
|
|
* @since 0.1.0 |
|
34
|
|
|
* @version 1.0.0 |
|
35
|
|
|
* |
|
36
|
|
|
* @param string $path_to_json Le chemin vers le fichier JSON. |
|
37
|
|
|
* @param string $output Peut être STDCLASS ou ARRAY_A ou ARRAY_N. Défault STDCLASS. |
|
38
|
|
|
* |
|
39
|
|
|
* @return array Les données du fichier JSON |
|
40
|
|
|
*/ |
|
41
|
|
|
public function open_and_decode( $path_to_json, $output = 'STDCLASS' ) { |
|
42
|
|
|
if ( ! file_exists( $path_to_json ) ) { |
|
43
|
|
|
return new \WP_Error( 'broke', __( 'Enabled to load JSON file', 'eoxia' ) ); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
$config_content = file_get_contents( $path_to_json ); |
|
47
|
|
|
|
|
48
|
|
|
if ( 'STDCLASS' === $output ) { |
|
49
|
|
|
$data = json_decode( $config_content ); |
|
50
|
|
|
} elseif ( 'ARRAY_A' === $output ) { |
|
51
|
|
|
$data = json_decode( $config_content, true ); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
if ( null === $data && json_last_error() !== JSON_ERROR_NONE ) { |
|
55
|
|
|
return new \WP_Error( 'broke', __( 'Error in JSON file', 'eoxia' ) ); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
return $data; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Décodes la chaine de caractère $json_to_decode |
|
63
|
|
|
* |
|
64
|
|
|
* @since 0.1.0 |
|
65
|
|
|
* @version 1.0.0 |
|
66
|
|
|
* |
|
67
|
|
|
* @param string $json_to_decode La chaine de caractère JSON. |
|
68
|
|
|
* @return array Les données décodées |
|
69
|
|
|
*/ |
|
70
|
|
|
public function decode( $json_to_decode ) { |
|
71
|
|
|
if ( ! is_string( $json_to_decode ) ) { |
|
72
|
|
|
return $json_to_decode; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
$json_decoded = json_decode( $json_to_decode, true ); |
|
76
|
|
|
|
|
77
|
|
|
if ( ! $json_decoded ) { |
|
78
|
|
|
$json_to_decode = str_replace( '\\', '', $json_to_decode ); |
|
79
|
|
|
$json_decoded = json_decode( $json_to_decode, true ); |
|
80
|
|
|
|
|
81
|
|
|
if ( ! $json_decoded ) { |
|
82
|
|
|
return $json_to_decode; |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
return $json_decoded; |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
} // End if(). |
|
90
|
|
|
|