FaZeBookSocialNetwork / www / src / FaZeBook / PostUtils.php
PostUtils.php
Raw
<?php

namespace FaZeBook;

use FaZeBook\Data\Post;
use FaZeBook\Data\User;

class PostUtils
{
    public static function postText(User $author, User $pageUser, bool $gossip, string $text) {
        Data::getInstance()->addPost($author, $pageUser, $gossip, PostType::TEXT, $text);
    }

    public static function postImage(User $author, User $pageUser, bool $gossip, string $imageUrl) {
        Data::getInstance()->addPost($author, $pageUser, $gossip, PostType::IMAGE_URL, $imageUrl);
    }

    public static function postVideoYoutube(User $author, User $pageUser, bool $gossip, string $youtubeUrl) {
        if (filter_var($youtubeUrl, FILTER_VALIDATE_URL) &&
            (substr($youtubeUrl, 0, strlen("https://www.youtube.com/watch?v="))
            === "https://www.youtube.com/watch?v=") ||
            (substr($youtubeUrl, 0, strlen("https://youtube.com/watch?v="))
            === "https://youtube.com/watch?v=")) {
            parse_str(parse_url($youtubeUrl, PHP_URL_QUERY), $urlVariables);
            Data::getInstance()->addPost($author, $pageUser, $gossip, PostType::VIDEO_YOUTUBE, $urlVariables['v']);
        }
    }

    public static function render(Post $post): string {
        switch ($post->getType()) {
            case PostType::TEXT:
                return $post->getContent();
            case PostType::IMAGE_URL:
                return '<img src="' . htmlspecialchars($post->getContent()) . '">';
            case PostType::VIDEO_YOUTUBE:
                return '<iframe width="500" height="282" src="https://www.youtube.com/embed/'
                    . preg_replace('[^_a-zA-Z0-9]', '' ,$post->getContent())
                    . '" frameborder="0" allowfullscreen></iframe>';
            default:
                return 'unsupported';
        }
    }
}