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 * Logical AND token.
16 *
17 * @author Boris Mikhaylov <kaguxmail@gmail.com>
18 */
19class LogicalAndToken implements TokenInterface
20{
21    private $tokens = array();
22
23    /**
24     * @param array $arguments exact values or tokens
25     */
26    public function __construct(array $arguments)
27    {
28        foreach ($arguments as $argument) {
29            if (!$argument instanceof TokenInterface) {
30                $argument = new ExactValueToken($argument);
31            }
32            $this->tokens[] = $argument;
33        }
34    }
35
36    /**
37     * Scores maximum score from scores returned by tokens for this argument if all of them score.
38     *
39     * @param $argument
40     *
41     * @return bool|int
42     */
43    public function scoreArgument($argument)
44    {
45        if (0 === count($this->tokens)) {
46            return false;
47        }
48
49        $maxScore = 0;
50        foreach ($this->tokens as $token) {
51            $score = $token->scoreArgument($argument);
52            if (false === $score) {
53                return false;
54            }
55            $maxScore = max($score, $maxScore);
56        }
57
58        return $maxScore;
59    }
60
61    /**
62     * Returns false.
63     *
64     * @return boolean
65     */
66    public function isLast()
67    {
68        return false;
69    }
70
71    /**
72     * Returns string representation for token.
73     *
74     * @return string
75     */
76    public function __toString()
77    {
78        return sprintf('bool(%s)', implode(' AND ', $this->tokens));
79    }
80}
81