-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
let_json.php
84 lines (72 loc) · 1.43 KB
/
let_json.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
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
<?php
//namespace letjson;
/**
* @param array|string $url
* @param null $callback
* @param false $associative
* @return false|mixed|string
*
* @throws Exception
*/
function let_json($url, $callback = null, $associative = false)
{
if (empty($url)){
throw new Exception("Url: $url is empty");
}
$urls = [];
if (!is_array($url)){
$urls[] = $url;
} else {
$urls = $url;
}
foreach($urls as $url_item){
if (!file_exists($url_item)) {
throw new Exception("Url: $url_item not exist");
}
$file = file_get_contents($url, true);
if(count($urls)>1){
$json[] = json_decode($file, $associative);
} else {
$json = json_decode($file, $associative);
}
}
if (is_callable($callback)) {
return $callback($json);
}
return $json;
}
/**
* Class LetJson
*/
class LetJson
{
/** @var array|mixed */
public $json = [];
/** @var string */
public $url = '';
/**
* LetJson constructor.
* @param $url
*/
function __construct($url)
{
$this->url = $url;
$this->json = let_json($url);
}
/**
* @return mixed
*/
function first()
{
return $this->json[0];
}
/**
* @param $callback
*/
function each($callback)
{
foreach ($this->json as $item) {
$callback($item);
}
}
}