1<?php
2/*
3 * This file is part of PHPUnit.
4 *
5 * (c) Sebastian Bergmann <sebastian@phpunit.de>
6 *
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
9 */
10
11/**
12 * Logical OR.
13 */
14class PHPUnit_Framework_Constraint_Or extends PHPUnit_Framework_Constraint
15{
16    /**
17     * @var PHPUnit_Framework_Constraint[]
18     */
19    protected $constraints = [];
20
21    /**
22     * @param PHPUnit_Framework_Constraint[] $constraints
23     */
24    public function setConstraints(array $constraints)
25    {
26        $this->constraints = [];
27
28        foreach ($constraints as $constraint) {
29            if (!($constraint instanceof PHPUnit_Framework_Constraint)) {
30                $constraint = new PHPUnit_Framework_Constraint_IsEqual(
31                    $constraint
32                );
33            }
34
35            $this->constraints[] = $constraint;
36        }
37    }
38
39    /**
40     * Evaluates the constraint for parameter $other
41     *
42     * If $returnResult is set to false (the default), an exception is thrown
43     * in case of a failure. null is returned otherwise.
44     *
45     * If $returnResult is true, the result of the evaluation is returned as
46     * a boolean value instead: true in case of success, false in case of a
47     * failure.
48     *
49     * @param mixed  $other        Value or object to evaluate.
50     * @param string $description  Additional information about the test
51     * @param bool   $returnResult Whether to return a result or throw an exception
52     *
53     * @return mixed
54     *
55     * @throws PHPUnit_Framework_ExpectationFailedException
56     */
57    public function evaluate($other, $description = '', $returnResult = false)
58    {
59        $success    = false;
60        $constraint = null;
61
62        foreach ($this->constraints as $constraint) {
63            if ($constraint->evaluate($other, $description, true)) {
64                $success = true;
65                break;
66            }
67        }
68
69        if ($returnResult) {
70            return $success;
71        }
72
73        if (!$success) {
74            $this->fail($other, $description);
75        }
76    }
77
78    /**
79     * Returns a string representation of the constraint.
80     *
81     * @return string
82     */
83    public function toString()
84    {
85        $text = '';
86
87        foreach ($this->constraints as $key => $constraint) {
88            if ($key > 0) {
89                $text .= ' or ';
90            }
91
92            $text .= $constraint->toString();
93        }
94
95        return $text;
96    }
97
98    /**
99     * Counts the number of constraint elements.
100     *
101     * @return int
102     */
103    public function count()
104    {
105        $count = 0;
106
107        foreach ($this->constraints as $constraint) {
108            $count += count($constraint);
109        }
110
111        return $count;
112    }
113}
114