forked from gharlan/alfred-github-workflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
item.php
177 lines (161 loc) · 4.81 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
165
166
167
168
169
170
171
172
173
174
175
176
177
<?php
/*
* This file is part of the alfred-github-workflow package.
*
* (c) Gregor Harlan
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
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 = $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, $hotkey, $baseUrl)
{
$xml = new SimpleXMLElement('<items></items>');
$prefix = $hotkey ? '' : ' ';
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 = ($enterprise ? 'e ' : '').$arg;
}
$c->addAttribute('arg', $arg);
}
if ($item->autocomplete) {
if (is_string($item->autocomplete)) {
$autocomplete = $item->autocomplete;
} elseif (null !== $item->comparator) {
$autocomplete = $item->comparator;
} else {
$autocomplete = $item->title;
}
$c->addAttribute('autocomplete', $prefix.($item->prefixOnlyTitle ? $autocomplete : $item->prefix.$autocomplete));
}
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();
}
}