-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathclass-wp-codebox-json.php
More file actions
142 lines (110 loc) · 3.6 KB
/
Copy pathclass-wp-codebox-json.php
File metadata and controls
142 lines (110 loc) · 3.6 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<?php
/**
* Shared JSON helpers for WP Codebox PHP code.
*
* @package WPCodebox
*/
defined( 'ABSPATH' ) || exit;
final class WP_Codebox_Json {
public static function encode( mixed $value, int $flags = 0, string $fallback = '{}' ): string {
$encoded = self::try_encode( $value, $flags );
return false === $encoded ? $fallback : $encoded;
}
/** @return string|false */
public static function try_encode( mixed $value, int $flags = 0 ): string|false {
if ( function_exists( 'wp_json_encode' ) ) {
return wp_json_encode( $value, $flags );
}
return json_encode( $value, $flags );
}
public static function decode( string $json ): mixed {
return json_decode( $json, true );
}
/** @return array<mixed>|null */
public static function decode_array( string $json ): ?array {
$decoded = self::decode( $json );
return is_array( $decoded ) ? $decoded : null;
}
/** @return array<string,mixed>|null */
public static function decode_object( string $json ): ?array {
$decoded = self::decode_array( $json );
return null !== $decoded && ! array_is_list( $decoded ) ? $decoded : null;
}
/** @return array<int,mixed>|null */
public static function decode_list( string $json ): ?array {
$decoded = self::decode_array( $json );
return null !== $decoded && array_is_list( $decoded ) ? $decoded : null;
}
/** @return array<mixed>|null */
public static function decode_trailing_array( string $output ): ?array {
$trimmed = trim( $output );
if ( '' === $trimmed ) {
return null;
}
$decoded = self::decode_array( $trimmed );
if ( null !== $decoded ) {
return $decoded;
}
$offset = strrpos( $trimmed, "\n{" );
if ( false === $offset ) {
return null;
}
return self::decode_array( substr( $trimmed, $offset + 1 ) );
}
/** @return array<mixed>|null */
public static function decode_fragment_array( string $text ): ?array {
$trimmed = trim( $text );
if ( '' === $trimmed ) {
return null;
}
$decoded = self::decode_array( $trimmed );
if ( null !== $decoded ) {
return $decoded;
}
$start = strpos( $trimmed, '{' );
$end = strrpos( $trimmed, '}' );
if ( false === $start || false === $end || $end <= $start ) {
return null;
}
return self::decode_array( substr( $trimmed, $start, $end - $start + 1 ) );
}
/** @return array<mixed>|null */
public static function read_array_file( string $path ): ?array {
$contents = is_file( $path ) ? file_get_contents( $path ) : false;
if ( false === $contents ) {
return null;
}
return self::decode_array( (string) $contents );
}
public static function write_file( string $path, mixed $value, int $flags = 0, bool $append_newline = true ): bool {
$directory = dirname( $path );
if ( ! self::ensure_directory( $directory ) ) {
return false;
}
$encoded = self::try_encode( $value, $flags );
if ( false === $encoded ) {
return false;
}
return false !== file_put_contents( $path, $encoded . ( $append_newline ? "\n" : '' ) );
}
public static function append_jsonl( string $path, mixed $record, int $flags = 0, int $file_flags = FILE_APPEND ): bool {
$directory = dirname( $path );
if ( ! self::ensure_directory( $directory ) ) {
return false;
}
$encoded = self::try_encode( $record, $flags );
if ( false === $encoded ) {
return false;
}
return false !== file_put_contents( $path, $encoded . "\n", $file_flags );
}
private static function ensure_directory( string $path ): bool {
if ( '' === $path || '.' === $path || is_dir( $path ) ) {
return true;
}
if ( function_exists( 'wp_mkdir_p' ) ) {
return (bool) wp_mkdir_p( $path );
}
return mkdir( $path, 0777, true );
}
}