PostsGridComposer.php 1.37 KB
<?php

namespace FootyRoom\Http\ViewComposers;

use FootyRoom\Http\Match\MatchSlug;
use Illuminate\Contracts\View\View;
use FootyRoom\Queries\Post\MatchPost;

class PostsGridComposer
{
    /**
     * Compose the view.
     *
     * @param \Illuminate\Contracts\View\View $view
     * @param \FootyRoom\Queries\Post\Post[] $posts
     * @param int $page
     * @param bool $legendary
     *
     * @return \Illuminate\Contracts\View\View
     */
    public function compose(View $view, $posts, $page, $legendary = false)
    {
        foreach ($posts as $post) {
            if (!isset($post->url) && $post instanceof MatchPost) {
                $post->url = route('matchReview', [
                    'id' => $post->matchId,
                    'slug' => MatchSlug::fromTeams($post->homeTeamName, $post->awayTeamName),
                ]);
            }

            if ($post instanceof MatchPost) {
                $post->titleNoSpoilers = "{$post->homeTeamName} vs {$post->awayTeamName}";
                $post->date = $post->matchDate;
            }

            if ($legendary) {
                $post->date = $post->matchDate;
            }

            if ($post->sticky > 0) {
                $post->date = 'Sticky';
            }
        }

        $view->with('page', $page);
        $view->with('posts', $posts);
        $view->with('legendary', $legendary);

        return $view;
    }
}