IssueCollection.php 1.7 KB
Newer Older
Ketan's avatar
Ketan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
<?php

/*
 * This file is part of StaticReview
 *
 * Copyright (c) 2014 Samuel Parkinson <@samparkinson_>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @see http://github.com/sjparkinson/static-review/blob/master/LICENSE.md
 */

namespace StaticReview\Collection;

use StaticReview\Issue\IssueInterface;

class IssueCollection extends Collection
{
    /**
     * Validates that $object is an instance of IssueInterface.
     *
     * @param  IssueInterface           $object
     * @return bool
     * @throws InvalidArgumentException
     */
    public function validate($object)
    {
        if ($object instanceof IssueInterface) {
            return true;
        }

        throw new \InvalidArgumentException($object . ' was not an instance of IssueInterface.');
    }

    /**
     * Filters the collection with the given closure, returning a new collection.
     *
     * @return IssueCollection
     */
    public function select(callable $filter)
    {
        if (! $this->collection) {
            return new IssueCollection();
        }

        $filtered = array_filter($this->collection, $filter);

        return new IssueCollection($filtered);
    }

    /**
     * Returns a new IssueCollection filtered by the given level option.
     *
     * @param  int             $level
     * @return IssueCollection
     */
    public function forLevel($option)
    {
        // Only return issues matching the level.
        $filter = function ($issue) use ($option) {
            if ($issue->matches($option)) {
                return true;
            }

            return false;
        };

        return $this->select($filter);
    }
}