Archived
1
0
Fork 0
This repository has been archived on 2021-07-30. You can view files and clone it, but cannot push or open issues or pull requests.
ifnews/app/Models/Game.php

95 lines
2.7 KiB
PHP
Raw Normal View History

2019-09-12 20:03:56 +03:00
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use App\Models\Author;
use App\Models\Language;
use App\Models\Tag;
use App\Models\Platform;
2020-04-10 06:35:55 +03:00
use App\Events\GameSaving;
2019-09-12 20:03:56 +03:00
class Game extends Model
{
2019-11-30 08:08:17 +02:00
protected $table = 'games';
public $guarded = ['id'];
2019-09-12 20:03:56 +03:00
2020-04-10 06:35:55 +03:00
/**
* The "booted" method of the model.
*
* @return void
*/
protected static function booted()
{
2020-05-03 09:49:56 +03:00
static::saving(function (Game $game) {
2020-04-10 06:35:55 +03:00
// Replace non-breaking space with regular one
2020-05-03 09:49:56 +03:00
$game->title = str_replace([' ','&nbsp;'], ' ', $game->title);
$game->title = preg_replace('/\s+/', ' ', $game->title);
2020-04-10 06:35:55 +03:00
$game->title = trim($game->title);
2020-04-10 07:01:09 +03:00
// strip quotes
$game->title = trim($game->title,'"');
$game->title = trim($game->title,"'");
2020-05-03 09:49:56 +03:00
if (isset($game->description)) {
$game->description = $game->normalizeDescription($game->description);
}
if (isset($game->short_description)) {
$game->short_description = $game->normalizeDescription($game->short_description);
}
2020-04-10 06:35:55 +03:00
});
}
2019-09-12 20:03:56 +03:00
2020-04-10 06:35:55 +03:00
public function authors() {
return $this->belongsToMany(Author::class, 'authors_games');
}
2019-09-12 20:03:56 +03:00
2020-04-10 06:35:55 +03:00
public function languages() {
return $this->belongsToMany(Language::class, 'languages_games');
}
2019-09-12 20:03:56 +03:00
2020-04-10 06:35:55 +03:00
public function tags() {
return $this->belongsToMany(Tag::class, 'tags_games');
}
public function platforms() {
return $this->belongsToMany(Platform::class, 'games_platforms', 'game_id', 'platform_id');
}
2020-05-02 20:52:40 +03:00
public function printShort() {
2020-05-03 09:44:18 +03:00
echo '#'.$this->id.' ';
$names = [];
foreach ($this->authors as $author) {
$names[] = $author->name;
}
echo implode(', ', $names);
echo ' «'.$this->title.'»'.PHP_EOL;
if (!empty($this->short_description)) {
$this->short_description = $this->normalizeDescription($this->short_description);
$lines = explode("\n", $this->short_description);
foreach ($lines as $line) {
echo '> '.$line.PHP_EOL;
}
}
2020-05-02 20:52:40 +03:00
}
public function printDetailed() {
$this->printShort();
2020-05-03 09:56:07 +03:00
echo _('Released').': '.$this->release_date.PHP_EOL;
2020-05-03 09:44:18 +03:00
if (!empty($this->description)) {
echo $this->normalizeDescription($this->description);
}
}
public function normalizeDescription($description) {
2020-05-03 09:49:56 +03:00
$description = str_replace(' ', ' ', $description);
2020-05-03 09:44:18 +03:00
$description = str_replace('...','…',$description);
$description = str_replace('\r','',$description);
$description = str_replace(['<br>', '<br/>', '<br />'], "\n", $description);
$description = htmlentities(html_entity_decode($description));
$description = preg_replace('/[ ]+/', ' ', $description);
$lines = explode("\n", $description);
$lines = array_map('trim', $lines);
$out = implode("\n", $lines);
return $out;
2020-05-02 20:52:40 +03:00
}
2019-09-12 20:03:56 +03:00
}