-
Notifications
You must be signed in to change notification settings - Fork 0
/
Utils.php
64 lines (61 loc) · 1.53 KB
/
Utils.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
<?php
/*
* @file utils.php
* @encoding UTF-8
*
* Copyright (c) 2016, [email protected]
*
* @author Anton Presnyakov <[email protected]>
* @date 24.11.2016 8:54:07
*/
namespace app\components\emotion;
use yii\base\Component;
/**
* Helper class
*/
class Utils extends Component {
/**
* simple array merge helper for parameters merge
* (unlike array_merge_recursive it merges sub arrays as array_merge)
* useful for merging default parameters like this
* $param = utils::mergeParam(array(
* 'Folder' => array(
* 'FolderClass' => 'IPF.Note',
* 'DisplayName' => null
* ),
* 'ParentFolderId' => '...'
* ), $param);
* @param array $default array of defaults (merge into it)
* @param array $param array to merge from
* @return array
*/
public static function mergeParam($default=array(), $param=array(), $extra = array()){
if (!is_array($param)) {
$param = array();
}
if (!is_array($default)) {
$default = array();
}
$bExclude = !empty($extra['bExclude']);
$level = array();
foreach ($default as $k=> $d) {
$b_exists = array_key_exists($k, $param);
if (is_array($d)) {
$level[$k] = self::mergeParam($d, $b_exists ? $param[$k] : null, $extra);
} else if ($b_exists){
$level[$k] = $param[$k];
} else {
$level[$k] = $d;
}
}
if (!$bExclude) {
foreach ($param as $k=> $p) {
$b_exists = array_key_exists($k, $default);
if (!$b_exists) {
$level[$k] = $p;
}
}
}
return $level;
}
}