Source of file HasCategories.php

Size: 0,877 Bytes - Last Modified: 2020-12-03T04:11:30+00:00

/home/vagrant/Code/projects/podcast-feed-parser/src/Traits/HasCategories.php

123456789101112131415161718192021222324252627282930313233343536373839404142434445
<?php


namespace Lukaswhite\PodcastFeedParser\Traits;

use Lukaswhite\PodcastFeedParser\Category;

/**
 * Trait HasCategories
 * @package Lukaswhite\PodcastFeedParser\Traits
 */
trait HasCategories
{
    /**
     * @var array
     */
    protected $categories;

    /**
     * @param string $type
     * @return array
     */
    public function getCategories(string $type = null)
    {
        if (!$type) {
            return $this->categories;
        }
        return array_values(array_filter(
            $this->categories,
            function(Category $category) use ($type){
                return $category->getType() === $type;
            }
        ));
    }

    /**
     * @param Category $category
     * @return $this
     */
    public function addCategory(Category $category): self
    {
        $this->categories[] = $category;
        return $this;
    }
}