-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathKendoBuild.php
90 lines (77 loc) · 3.09 KB
/
KendoBuild.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
85
86
87
88
89
90
<?php
namespace tigrov\kendoui;
use yii\helpers\ArrayHelper;
use yii\helpers\Inflector;
class KendoBuild {
private static $_actions = [
'create' => ['class' => '\tigrov\kendoui\actions\Create'],
'read' => ['class' => '\tigrov\kendoui\actions\Read'],
'update' => ['class' => '\tigrov\kendoui\actions\Update'],
'delete' => ['class' => '\tigrov\kendoui\actions\Delete']
];
/**
* Create actions list for Controller::actions()
*
* @param $config array|string Configuration for creating action list.
* If $config is string then $config is using as modelClass.
* If $config is array then it must contain settings for each action.
* Also $config might contain list of action types ['actions' => ['create', 'read', 'update', 'delete']]
* and individual config for each action ['actions' => ['create' => ['model' => 'ClassName'], 'read']],
* in the last example will have created list with two action types 'create' and 'read'.
* Id for each action will have generated from "model class name" as prefix and type name as suffix "class-name-create".
* Id can be specified as individual action setting ['actions' => ['create' => ['id' => 'model-create']]]
* or ['actions' => ['create' => 'model-create']]
*
* @return array List of actions [id => settings]
*/
public static function actions($config)
{
if (is_string($config)) {
return static::mergeConfig(['model' => $config, 'actions' => static::$_actions]);
}
if (empty($config['actions'])) {
$config['actions'] = static::$_actions;
} else {
$config['actions'] = static::toAssociative($config['actions']);
}
return static::mergeConfig($config);
}
public static function mergeConfig($config)
{
$actions = $config['actions'];
unset($config['actions']);
$prefix = static::actionPrefix($config);
$list = [];
foreach ($actions as $id => $actionConfig) {
if (is_array($actionConfig)) {
$actionPrefix = !empty($actionConfig['model']) ? static::actionPrefix($actionConfig) : $prefix;
$actionId = !empty($actionConfig['id']) ? $actionConfig['id'] : $actionPrefix.$id;
unset($actionConfig['id']);
} else {
$actionId = $actionConfig;
$actionConfig = [];
}
$list[$actionId] = ArrayHelper::merge(static::$_actions[$id], $config, $actionConfig);
}
return $list;
}
public static function actionPrefix($config)
{
$className = explode('\\',
is_string($config['model']) ? $config['model'] : $config['model']['class']
);
return Inflector::camel2id(end($className)).'-';
}
public static function toAssociative($actions)
{
$list = [];
foreach ($actions as $id => $config) {
if (is_numeric($id)) {
$list[$config] = [];
} else {
$list[$id] = $config;
}
}
return $list;
}
}