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 every entry token.
16 *
17 * @author Adrien Brault <adrien.brault@gmail.com>
18 */
19class ArrayEveryEntryToken implements TokenInterface
20{
21    /**
22     * @var TokenInterface
23     */
24    private $value;
25
26    /**
27     * @param mixed $value exact value or token
28     */
29    public function __construct($value)
30    {
31        if (!$value instanceof TokenInterface) {
32            $value = new ExactValueToken($value);
33        }
34
35        $this->value = $value;
36    }
37
38    /**
39     * {@inheritdoc}
40     */
41    public function scoreArgument($argument)
42    {
43        if (!$argument instanceof \Traversable && !is_array($argument)) {
44            return false;
45        }
46
47        $scores = array();
48        foreach ($argument as $key => $argumentEntry) {
49            $scores[] = $this->value->scoreArgument($argumentEntry);
50        }
51
52        if (empty($scores) || in_array(false, $scores, true)) {
53            return false;
54        }
55
56        return array_sum($scores) / count($scores);
57    }
58
59    /**
60     * {@inheritdoc}
61     */
62    public function isLast()
63    {
64        return false;
65    }
66
67    /**
68     * {@inheritdoc}
69     */
70    public function __toString()
71    {
72        return sprintf('[%s, ..., %s]', $this->value, $this->value);
73    }
74
75    /**
76     * @return TokenInterface
77     */
78    public function getValue()
79    {
80        return $this->value;
81    }
82}
83