forked from modstart/ModStartBlog
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTags.php
More file actions
116 lines (100 loc) · 2.91 KB
/
Copy pathTags.php
File metadata and controls
116 lines (100 loc) · 2.91 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
<?php
namespace ModStart\Field;
use ModStart\Core\Dao\ModelUtil;
use ModStart\Core\Util\ConvertUtil;
use ModStart\Core\Util\SerializeUtil;
use ModStart\Core\Util\TagUtil;
class Tags extends AbstractField
{
/**
* 使用JSON
*/
const SERIALIZE_TYPE_DEFAULT = null;
/**
* 使用冒号分割
*/
const SERIALIZE_TYPE_COLON_SEPARATED = 1;
protected function setup()
{
$this->addVariables([
'tags' => [],
'serializeType' => null,
]);
}
public function tags($value)
{
$this->addVariables(['tags' => $value]);
return $this;
}
public function serializeType($value)
{
$this->addVariables(['serializeType' => $value]);
return $this;
}
public function serializeAsColonSeparated()
{
$this->serializeType(self::SERIALIZE_TYPE_COLON_SEPARATED);
return $this;
}
public function tagType($type)
{
return self::tags($type::getList());
}
public function tagModel($table, $keyName = 'id', $labelName = 'title')
{
return $this->tags(ModelUtil::valueMap($table, $keyName, $labelName));
}
public function tagModelField($table, $tagNameField = 'tag', $serializeType = null)
{
$values = ModelUtil::values($table, $tagNameField);
$tags = [];
foreach ($values as $value) {
$tagList = null;
switch ($serializeType) {
case self::SERIALIZE_TYPE_COLON_SEPARATED:
$tagList = TagUtil::string2Array($value);
break;
default:
$tagList = ConvertUtil::toArray($value);
break;
}
if (empty($tagList)) {
continue;
}
foreach ($tagList as $v) {
$tags[$v] = $v;
}
}
return $this->tags($tags);
}
public function unserializeValue($value, AbstractField $field)
{
if (null === $value) {
return $value;
}
switch ($this->getVariable('serializeType')) {
case self::SERIALIZE_TYPE_COLON_SEPARATED:
return TagUtil::string2Array($value);
default:
return ConvertUtil::toArray($value);
}
}
public function serializeValue($value, $model)
{
switch ($this->getVariable('serializeType')) {
case self::SERIALIZE_TYPE_COLON_SEPARATED:
return TagUtil::array2String($value);
default:
return SerializeUtil::jsonEncode($value);
}
}
public function prepareInput($value, $model)
{
switch ($this->getVariable('serializeType')) {
case self::SERIALIZE_TYPE_COLON_SEPARATED:
return TagUtil::string2Array($value);
default:
return ConvertUtil::toArray($value);
}
}
}