-
Notifications
You must be signed in to change notification settings - Fork 109
/
ModelJson.php
52 lines (41 loc) · 1.15 KB
/
ModelJson.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
<?php
class ModelJson
{
protected $config;
public function __construct($config)
{
$this->config = $config;
}
public function has($keychain)
{
return ModelJson::_has($this->config, $keychain);
}
public static function _has($model, $keychain)
{
if(!is_array($keychain))
$keychain = explode('.', $keychain);
$key = strtolower($keychain[0]);
if(is_array($model) ? !isset($model[$key]) : !isset($model->$key))
return false;
if(count($keychain) == 1)
return true;
$value = is_array($model) ? $model[$key] : $model->$key;
return ModelJson::_has($value, array_slice($keychain, 1));
}
public function get($keychain, $default = null)
{
return ModelJson::_get($this->config, $keychain, $default);
}
public static function _get($model, $keychain, $default)
{
if(!is_array($keychain))
$keychain = explode('.', $keychain);
$key = strtolower($keychain[0]);
if (is_array($model) ? !isset($model[$key]) : !isset($model->$key))
return $default;
$value = is_array($model) ? $model[$key] : $model->$key;
if(count($keychain) == 1)
return $value;
return ModelJson::_get($value, array_slice($keychain, 1), $default);
}
}