<commit_before><?php

namespace SebastiaanLuca\Blog\Services;

use Illuminate\Support\Collection;
use SebastiaanLuca\Blog\Models\Post;

class PostService
{
    /**
     * @var \SebastiaanLuca\Blog\Models\Post
     */
    protected $posts;
    
    /**
     * @var \League\CommonMark\Converter
     */
    protected $markdown;
    
    /**
     * PostService constructor.
     *
     * @param \SebastiaanLuca\Blog\Models\Post $posts
     * @param \League\CommonMark\Converter $markdown
     */
    public function __construct(Post $posts, \League\CommonMark\Converter $markdown)
    {
        $this->posts = $posts;
        $this->markdown = $markdown;
    }
    
    /**
     * Get a chronological list of published posts.
     *
     * @param array|null $columns
     *
     * @return \Illuminate\Support\Collection
     */
    public function getPublishedPosts($columns = null) : Collection
    {
        if (is_null($columns)) {
            $columns = [
                'id',
                'slug',
                'title',
                'intro',
                'published_at',
            ];
        }
        
        return $this->posts->published()->orderChronologically()->get($columns);
    }
    
    /**
     * Get a published post by its slug.
     *
     * @param string $slug
     *
     * @return \SebastiaanLuca\Blog\Models\Post
     */
    public function getPublishedPostBySlug(string $slug) : Post
    {
        $post = $this->posts->published()->where('slug', $slug)->firstOrFail();
        
        $post = $this->removeIntroTagFromPostBody($post);
        $post = $this->parseBody($post);
        
        return $post;
    }
    
    /**
     * Remove the intro tag from the post's body.
     *
     * @param \SebastiaanLuca\Blog\Models\Post $post
     *<commit_msg>Compute intro if it doesn't exist<commit_after><?php

namespace SebastiaanLuca\Blog\Services;

use Illuminate\Support\Collection;
use SebastiaanLuca\Blog\Models\Post;

class PostService
{
    /**
     * @var \SebastiaanLuca\Blog\Models\Post
     */
    protected $posts;
    
    /**
     * @var \League\CommonMark\Converter
     */
    protected $markdown;
    
    /**
     * PostService constructor.
     *
     * @param \SebastiaanLuca\Blog\Models\Post $posts
     * @param \League\CommonMark\Converter $markdown
     */
    public function __construct(Post $posts, \League\CommonMark\Converter $markdown)
    {
        $this->posts = $posts;
        $this->markdown = $markdown;
    }
    
    /**
     * Get a chronological list of published posts.
     *
     * @param array|null $columns
     *
     * @return \Illuminate\Support\Collection
     */
    public function getPublishedPosts($columns = null) : Collection
    {
        if (is_null($columns)) {
            $columns = [
                'id',
                'slug',
                'title',
                'intro',
                'body',
                'published_at',
            ];
        }
        
        // TODO: override intro with first few chars of body if empty
        // TODO: parse intro
        
        $posts = $this->posts->published()->orderChronologically()->get($columns);
        
        $posts = $posts->map(function(Post $post) {
            if (is_null($post->intro)) {
                $post->intro = str_limit($post->body, 300);
            }
            
            return $post;
        });
        
        return $posts;
    }
    
    /**
     * Get a published post by its slug.
     *
     * @param string $slug
     *
     * @return \SebastiaanLuca\Blog\Models\Post
     */
    public function getPublishedPostBySlug(string $slug) : Post
    {
        $post = $this->posts->published()->where('slug', $slug)->firstOrFail();
        
        $post = $this->removeIntroTagFromPostBody($post);
        $post = $this->parseBody($post);
        
        return $post;
    }
    
    /**
     * Remove the intro tag from the post's body.
     *
     * @param \SebastiaanLuca\Blog\Models\Post $post
     *
{'fraction_non_alphanumeric': 0.12928232058014505, 'fraction_numerical': 0.0007501875468867217, 'mean_word_length': 1.7247956403269755, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1335033', 'n_tokens_mistral': 1230, 'n_tokens_neox': 1191, 'n_words': 230}