1
|
|
|
<?php |
2
|
|
|
namespace App\Gitter; |
3
|
|
|
|
4
|
|
|
use Illuminate\Contracts\Support\Arrayable; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Class Response |
8
|
|
|
* @package App\Gitter\Extensions\Middleware |
9
|
|
|
*/ |
10
|
|
|
class Response |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var string |
14
|
|
|
*/ |
15
|
|
|
protected $content = ''; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var bool |
19
|
|
|
*/ |
20
|
|
|
protected $empty = true; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Response constructor. |
24
|
|
|
* @param string $content |
25
|
|
|
*/ |
26
|
|
|
public function __construct($content = '') |
27
|
|
|
{ |
28
|
|
|
$this->setContent($content); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param $result |
33
|
|
|
* @return null|string |
34
|
|
|
*/ |
35
|
|
|
protected function parse($result) |
36
|
|
|
{ |
37
|
|
|
switch (true) { |
38
|
|
|
case is_string($result) || is_int($result) || |
39
|
|
|
(is_object($result) && method_exists($result, '__toString')): |
40
|
|
|
return (string)$result; |
41
|
|
|
|
42
|
|
|
case is_array($result): |
43
|
|
|
return implode("\n", $result); |
44
|
|
|
|
45
|
|
|
case is_object($result) && method_exists($result, 'toString'): |
46
|
|
|
return $result->toString(); |
47
|
|
|
|
48
|
|
|
case is_object($result) && $result instanceof Arrayable: |
49
|
|
|
return implode("\n", $result->toArray()); |
50
|
|
|
|
51
|
|
|
case ($result instanceof static): |
52
|
|
|
return $result->getContent(); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
return null; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @return string |
60
|
|
|
*/ |
61
|
|
|
public function getContent() |
62
|
|
|
{ |
63
|
|
|
return $this->content; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param $content |
68
|
|
|
* @return Response |
69
|
|
|
*/ |
70
|
|
|
public function setContent($content) |
71
|
|
|
{ |
72
|
|
|
$content = $this->parse($content); |
73
|
|
|
|
74
|
|
|
$this->empty = !$content; |
75
|
|
|
$this->content = $content; |
76
|
|
|
|
77
|
|
|
return $this; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @return boolean |
82
|
|
|
*/ |
83
|
|
|
public function isEmpty() |
84
|
|
|
{ |
85
|
|
|
return $this->empty; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @return Response |
90
|
|
|
*/ |
91
|
|
|
public function italic() |
92
|
|
|
{ |
93
|
|
|
return $this->wrap('_'); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @return Response |
98
|
|
|
*/ |
99
|
|
|
public function bold() |
100
|
|
|
{ |
101
|
|
|
return $this->wrap('**'); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param string|null $lang |
106
|
|
|
* @return Response |
107
|
|
|
*/ |
108
|
|
|
public function code(string $lang = null) |
109
|
|
|
{ |
110
|
|
|
if ($lang === null) { |
111
|
|
|
return $this->wrap('`'); |
112
|
|
|
} |
113
|
|
|
return $this->wrap('```' . $lang . "\n", "\n```"); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param $src |
118
|
|
|
* @param bool $after |
119
|
|
|
* @return Response |
120
|
|
|
*/ |
121
|
|
|
public function image($src, $after = true) |
122
|
|
|
{ |
123
|
|
|
$code = ''; |
124
|
|
|
if ($after) { |
125
|
|
|
return $this->after($code); |
126
|
|
|
} |
127
|
|
|
return $this->before($code); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @param bool $after |
132
|
|
|
* @return Response |
133
|
|
|
*/ |
134
|
|
|
public function hr($after = true) |
135
|
|
|
{ |
136
|
|
|
if ($after) { |
137
|
|
|
return $this->after('---', true); |
138
|
|
|
} |
139
|
|
|
return $this->before('---', true); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @return Response |
144
|
|
|
*/ |
145
|
|
|
public function quote() |
146
|
|
|
{ |
147
|
|
|
return $this->before('> '); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @param string $symbol |
152
|
|
|
* @param string|null $symbolAfter |
153
|
|
|
* @return Response |
154
|
|
|
*/ |
155
|
|
|
public function wrap(string $symbol, string $symbolAfter = null) |
156
|
|
|
{ |
157
|
|
|
if ($symbolAfter !== null) { |
158
|
|
|
$symbolAfter = $symbol; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
$this->setContent($symbol . $this->getContent() . $symbolAfter); |
162
|
|
|
return $this; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @param $text |
167
|
|
|
* @param bool $newline |
168
|
|
|
* @return Response |
169
|
|
|
*/ |
170
|
|
|
public function before($text, $newline = false) |
171
|
|
|
{ |
172
|
|
|
return $this->wrap($text . ($newline ? "\n" : ''), ''); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @param $text |
177
|
|
|
* @param bool $newline |
178
|
|
|
* @return Response |
179
|
|
|
*/ |
180
|
|
|
public function after($text, $newline = false) |
181
|
|
|
{ |
182
|
|
|
return $this->wrap('', ($newline ? "\n" : '') . $text); |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
|