forked from gharlan/alfred-github-workflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
item.php
164 lines (149 loc) · 4.45 KB
/
item.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
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<?php
class Item
{
private $randomUid = false;
private $prefix = '';
private $prefixOnlyTitle = true;
private $title;
private $comparator;
private $subtitle;
private $icon;
private $arg;
private $valid = true;
private $add = '…';
private $autocomplete = true;
private $prio = 0;
private $missingChars = 0;
private $sameChars = 0;
public static function create()
{
return new self;
}
public function randomUid()
{
$this->randomUid = true;
return $this;
}
public function prefix($prefix, $onlyTitle = true)
{
$this->prefix = $prefix;
$this->prefixOnlyTitle = $onlyTitle;
return $this;
}
public function title($title)
{
$this->title = $title;
return $this;
}
public function comparator($comparator)
{
$this->comparator = $comparator;
return $this;
}
public function subtitle($subtitle)
{
$this->subtitle = $subtitle;
return $this;
}
public function icon($icon)
{
$this->icon = $icon;
return $this;
}
public function arg($arg)
{
$this->arg = $arg;
return $this;
}
public function valid($valid, $add = '…')
{
$this->valid = (bool) $valid;
$this->add = $add;
return $this;
}
public function autocomplete($autocomplete = true)
{
$this->autocomplete = (bool) $autocomplete;
return $this;
}
public function prio($prio)
{
$this->prio = $prio;
return $this;
}
public function match($query)
{
$comparator = strtolower($this->comparator ?: $this->title);
$query = strtolower($query);
if (!$this->prefixOnlyTitle && stripos($query, $this->prefix) === 0) {
$query = substr($query, strlen($this->prefix));
}
$this->sameChars = 0;
$queryLength = strlen($query);
for ($i = 0, $k = 0; $i < $queryLength; ++$i, $k++) {
for (; isset($comparator[$k]) && $comparator[$k] !== $query[$i]; ++$k);
if (!isset($comparator[$k])) {
return false;
}
if ($i === $k) {
$this->sameChars++;
}
}
$this->missingChars = strlen($comparator) - $queryLength;
return true;
}
public function compare(self $another)
{
if ($this->sameChars != $another->sameChars) {
return $this->sameChars < $another->sameChars ? 1 : -1;
}
if ($this->prio != $another->prio) {
return $this->prio < $another->prio ? 1 : -1;
}
return $this->missingChars > $another->missingChars ? 1 : -1;
}
/**
* @param self[] $items
* @return string
*/
public static function toXml(array $items, $enterprise, $baseUrl)
{
$xml = new SimpleXMLElement('<items></items>');
$prefix = $enterprise ? 'e ' : ' ';
foreach ($items as $item) {
$c = $xml->addChild('item');
$title = $item->prefix . $item->title;
$c->addAttribute('uid', $item->randomUid ? md5(time() . $title) : md5($title));
if ($item->icon && file_exists(__DIR__ . '/icons/' . $item->icon . '.png')) {
$c->addChild('icon', 'icons/' . $item->icon . '.png');
} else {
$c->addChild('icon', 'icon.png');
}
if ($item->arg) {
$arg = $item->arg;
if ('/' === $arg[0]) {
$arg = $baseUrl . $arg;
} elseif (false === strpos($arg, '://')) {
$arg = ltrim($prefix . $arg);
}
$c->addAttribute('arg', $arg);
}
if ($item->autocomplete) {
if ($item->comparator) {
$c->addAttribute('autocomplete', $prefix . $item->comparator);
} else {
$c->addAttribute('autocomplete', $prefix . ($item->prefixOnlyTitle ? $item->title : $item->prefix . $item->title));
}
}
if (!$item->valid) {
$c->addAttribute('valid', 'no');
$title .= $item->add;
}
$c->addChild('title', htmlspecialchars($title));
if ($item->subtitle) {
$c->addChild('subtitle', htmlspecialchars($item->subtitle));
}
}
return $xml->asXML();
}
}