Simplification
This commit is contained in:
@@ -62,13 +62,16 @@ class Post extends DB\SQL\Mapper
|
||||
}
|
||||
|
||||
$row = $this->cast();
|
||||
$post = $this->summary($row) + ['body_html' => (string) $row['body_html']];
|
||||
|
||||
return $post;
|
||||
return $this->summary($row) + ['body_html' => (string) $row['body_html']];
|
||||
}
|
||||
|
||||
public function findForForm(int $id): ?array
|
||||
{
|
||||
if ($id <= 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$this->load(['id = ?', $id]);
|
||||
if ($this->dry()) {
|
||||
return null;
|
||||
@@ -84,12 +87,12 @@ class Post extends DB\SQL\Mapper
|
||||
|
||||
public function savePost(array $input, ?int $id = null): int
|
||||
{
|
||||
$payload = $this->payload($input);
|
||||
$payload = $this->normalizePayload($input);
|
||||
$now = app_now();
|
||||
|
||||
if ($id === null) {
|
||||
$this->reset();
|
||||
$payload['slug'] = $this->uniqueSlug($payload['title']);
|
||||
$payload['slug'] = $this->nextSlug($payload['title']);
|
||||
$payload['created_at'] = $now;
|
||||
} else {
|
||||
$this->load(['id = ?', $id]);
|
||||
@@ -120,11 +123,11 @@ class Post extends DB\SQL\Mapper
|
||||
return $this->count(['body_markdown LIKE ?', '%media:' . $fileName . '%']) > 0;
|
||||
}
|
||||
|
||||
private function payload(array $input): array
|
||||
private function normalizePayload(array $input): array
|
||||
{
|
||||
$title = trim((string) ($input['title'] ?? ''));
|
||||
$excerpt = trim((string) ($input['excerpt'] ?? ''));
|
||||
$body = trim((string) ($input['body_markdown'] ?? ''));
|
||||
$bodyMarkdown = trim((string) ($input['body_markdown'] ?? ''));
|
||||
|
||||
if ($title === '') {
|
||||
throw new RuntimeException('Ajoute un titre.');
|
||||
@@ -142,20 +145,20 @@ class Post extends DB\SQL\Mapper
|
||||
return [
|
||||
'title' => $title,
|
||||
'excerpt' => $excerpt,
|
||||
'body_markdown' => $body,
|
||||
'body_html' => MarkdownService::instance()->compile($body, new Media()),
|
||||
'body_markdown' => $bodyMarkdown,
|
||||
'body_html' => MarkdownService::instance()->compile($bodyMarkdown, new Media()),
|
||||
];
|
||||
}
|
||||
|
||||
private function uniqueSlug(string $title): string
|
||||
private function nextSlug(string $title): string
|
||||
{
|
||||
$base = app_slug($title);
|
||||
$slug = $base;
|
||||
$n = 2;
|
||||
$suffix = 2;
|
||||
|
||||
while ($this->count(['slug = ?', $slug]) > 0) {
|
||||
$slug = $base . '-' . $n;
|
||||
$n++;
|
||||
$slug = $base . '-' . $suffix;
|
||||
$suffix++;
|
||||
}
|
||||
|
||||
return $slug;
|
||||
@@ -163,7 +166,7 @@ class Post extends DB\SQL\Mapper
|
||||
|
||||
private function summary(array $row): array
|
||||
{
|
||||
$thumbnail = $this->firstImage((string) ($row['body_html'] ?? ''));
|
||||
$thumbnail = $this->extractThumbnail((string) ($row['body_html'] ?? ''));
|
||||
|
||||
return [
|
||||
'id' => (int) $row['id'],
|
||||
@@ -177,20 +180,14 @@ class Post extends DB\SQL\Mapper
|
||||
];
|
||||
}
|
||||
|
||||
private function firstImage(string $html): array
|
||||
private function extractThumbnail(string $html): array
|
||||
{
|
||||
if ($html === '') {
|
||||
if ($html === '' || !preg_match('~(<img\s[^>]*src="([^"]+)"[^>]*>)~i', $html, $match)) {
|
||||
return ['url' => '', 'alt' => ''];
|
||||
}
|
||||
|
||||
if (!preg_match('~(<img\s[^>]*src="([^"]+)"[^>]*>)~i', $html, $match)) {
|
||||
return ['url' => '', 'alt' => ''];
|
||||
}
|
||||
|
||||
$tag = $match[1];
|
||||
$alt = '';
|
||||
|
||||
if (preg_match('~alt="([^"]*)"~i', $tag, $altMatch)) {
|
||||
if (preg_match('~alt="([^"]*)"~i', $match[1], $altMatch)) {
|
||||
$alt = html_entity_decode($altMatch[1], ENT_QUOTES | ENT_HTML5, 'UTF-8');
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user