1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace One\Model; |
4
|
|
|
|
5
|
|
|
use One\Collection; |
6
|
|
|
use Psr\Http\Message\UriInterface; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Model base class |
10
|
|
|
* @method \One\Collection getCollection() |
11
|
|
|
* @method self withCollection(Collection $collection) |
12
|
|
|
* @method mixed|null get(string $key) |
13
|
|
|
* @method self set(string $key, mixed $value) |
14
|
|
|
* @method self add(string $key, mixed $value) |
15
|
|
|
* @method self map(\Closure $callback, array $context) |
16
|
|
|
* @method self filter filter(\Closure $callback) |
17
|
|
|
*/ |
18
|
|
|
class Model |
19
|
|
|
{ |
20
|
|
|
public const LEAD_SENTENCES_LIMIT = 5; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* data variable to that work as One\Collection |
24
|
|
|
* |
25
|
|
|
* @var \One\Collection |
26
|
|
|
*/ |
27
|
|
|
protected $collection = null; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* proxy method to chain it to Collection class |
31
|
|
|
* @return mixed |
32
|
|
|
* @throws \Exception |
33
|
|
|
*/ |
34
|
|
|
public function __call(string $name, array $arguments) |
35
|
|
|
{ |
36
|
|
|
if (method_exists($this->getCollection(), $name)) { |
37
|
|
|
return call_user_func_array([$this->getCollection(), $name], $arguments); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
throw new \Exception("method ${name} not exist"); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* get Data Collection |
45
|
|
|
*/ |
46
|
|
|
public function getCollection(): ?\One\Collection |
47
|
|
|
{ |
48
|
|
|
return $this->collection; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* immutable, return CLONE of original object with changed collection |
53
|
|
|
*/ |
54
|
|
|
public function withCollection(Collection $collection): self |
55
|
|
|
{ |
56
|
|
|
$clone = clone $this; |
57
|
|
|
$clone->collection = $collection; |
58
|
|
|
|
59
|
|
|
return $clone; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Clean non parseable char from string |
64
|
|
|
* |
65
|
|
|
* @param mixed $string |
66
|
|
|
* @return mixed |
67
|
|
|
*/ |
68
|
|
|
protected function filterStringInstance($string) |
69
|
|
|
{ |
70
|
|
|
if (empty($string)) { |
71
|
|
|
return ''; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return $this->convertNonAscii( |
75
|
|
|
$string |
76
|
|
|
); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Make Sure Uri is a Psr\Http\Message\UriInterface instance |
81
|
|
|
* |
82
|
|
|
* @param \Psr\Http\Message\UriInterface|string|null $uri |
83
|
|
|
*/ |
84
|
|
|
protected function filterUriInstance($uri): string |
85
|
|
|
{ |
86
|
|
|
if ($uri instanceof UriInterface) { |
87
|
|
|
return (string) $uri; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
if (is_string($uri)) { |
91
|
|
|
return (string) \One\createUriFromString($uri); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return ''; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Make Sure Date in string with correct format state |
99
|
|
|
* |
100
|
|
|
* @param \DateTimeInterface|string|int|null $date |
101
|
|
|
*/ |
102
|
|
|
protected function filterDateInstance($date): string |
103
|
|
|
{ |
104
|
|
|
if (empty($date)) { |
105
|
|
|
$date = new \DateTime('now', new \DateTimeZone('Asia/Jakarta')); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
if (is_string($date) || is_int($date)) { |
109
|
|
|
$date = new \DateTime($date, new \DateTimeZone('Asia/Jakarta')); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return $this->formatDate($date); |
|
|
|
|
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* format date into required format |
117
|
|
|
*/ |
118
|
|
|
protected function formatDate(\DateTimeInterface $date): string |
119
|
|
|
{ |
120
|
|
|
return $date->format('Y-m-d H:i:s'); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* create lead/synopsis from body content if not available |
125
|
|
|
*/ |
126
|
|
|
protected function createLeadFromBody(string $body): string |
127
|
|
|
{ |
128
|
|
|
$body = strip_tags($body); |
129
|
|
|
$sentences = array_filter( |
130
|
|
|
explode('.', $body), |
131
|
|
|
function ($item) { |
132
|
|
|
return trim($item); |
133
|
|
|
} |
134
|
|
|
); |
135
|
|
|
|
136
|
|
|
return implode( |
137
|
|
|
'. ', |
138
|
|
|
array_slice( |
139
|
|
|
$sentences, |
140
|
|
|
0, |
141
|
|
|
self::LEAD_SENTENCES_LIMIT |
142
|
|
|
) |
143
|
|
|
); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Clean non ASCII char from string |
148
|
|
|
*/ |
149
|
|
|
private function convertNonAscii(string $string): string |
150
|
|
|
{ |
151
|
|
|
$search = $replace = []; |
152
|
|
|
|
153
|
|
|
// Replace Single Curly Quotes |
154
|
|
|
$search[] = chr(226) . chr(128) . chr(152); |
155
|
|
|
$replace[] = "'"; |
156
|
|
|
$search[] = chr(226) . chr(128) . chr(153); |
157
|
|
|
$replace[] = "'"; |
158
|
|
|
|
159
|
|
|
// Replace Smart Double Curly Quotes |
160
|
|
|
$search[] = chr(226) . chr(128) . chr(156); |
161
|
|
|
$replace[] = '"'; |
162
|
|
|
$search[] = chr(226) . chr(128) . chr(157); |
163
|
|
|
$replace[] = '"'; |
164
|
|
|
|
165
|
|
|
// Replace En Dash |
166
|
|
|
$search[] = chr(226) . chr(128) . chr(147); |
167
|
|
|
$replace[] = '--'; |
168
|
|
|
|
169
|
|
|
// Replace Em Dash |
170
|
|
|
$search[] = chr(226) . chr(128) . chr(148); |
171
|
|
|
$replace[] = '---'; |
172
|
|
|
|
173
|
|
|
// Replace Bullet |
174
|
|
|
$search[] = chr(226) . chr(128) . chr(162); |
175
|
|
|
$replace[] = '*'; |
176
|
|
|
|
177
|
|
|
// Replace Middle Dot |
178
|
|
|
$search[] = chr(194) . chr(183); |
179
|
|
|
$replace[] = '*'; |
180
|
|
|
|
181
|
|
|
// Replace Ellipsis with three consecutive dots |
182
|
|
|
$search[] = chr(226) . chr(128) . chr(166); |
183
|
|
|
$replace[] = '...'; |
184
|
|
|
|
185
|
|
|
// Apply Replacements |
186
|
|
|
$string = str_replace($search, $replace, $string); |
187
|
|
|
|
188
|
|
|
// Remove any non-ASCII Characters |
189
|
|
|
return preg_replace("/[^\x01-\x7F]/", '', $string); |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
|