1<?php
2
3/*
4 * This file is part of the Prophecy.
5 * (c) Konstantin Kudryashov <ever.zet@gmail.com>
6 *     Marcello Duarte <marcello.duarte@gmail.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12namespace Prophecy\Argument\Token;
13
14/**
15 * Array elements count token.
16 *
17 * @author Boris Mikhaylov <kaguxmail@gmail.com>
18 */
19
20class ArrayCountToken implements TokenInterface
21{
22    private $count;
23
24    /**
25     * @param integer $value
26     */
27    public function __construct($value)
28    {
29        $this->count = $value;
30    }
31
32    /**
33     * Scores 6 when argument has preset number of elements.
34     *
35     * @param $argument
36     *
37     * @return bool|int
38     */
39    public function scoreArgument($argument)
40    {
41        return $this->isCountable($argument) && $this->hasProperCount($argument) ? 6 : false;
42    }
43
44    /**
45     * Returns false.
46     *
47     * @return boolean
48     */
49    public function isLast()
50    {
51        return false;
52    }
53
54    /**
55     * Returns string representation for token.
56     *
57     * @return string
58     */
59    public function __toString()
60    {
61        return sprintf('count(%s)', $this->count);
62    }
63
64    /**
65     * Returns true if object is either array or instance of \Countable
66     *
67     * @param $argument
68     * @return bool
69     */
70    private function isCountable($argument)
71    {
72        return (is_array($argument) || $argument instanceof \Countable);
73    }
74
75    /**
76     * Returns true if $argument has expected number of elements
77     *
78     * @param array|\Countable $argument
79     *
80     * @return bool
81     */
82    private function hasProperCount($argument)
83    {
84        return $this->count === count($argument);
85    }
86}
87