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 * Constraint that asserts that the Traversable it is applied to contains
13 * a given value.
14 */
15class PHPUnit_Framework_Constraint_TraversableContains extends PHPUnit_Framework_Constraint
16{
17    /**
18     * @var bool
19     */
20    protected $checkForObjectIdentity;
21
22    /**
23     * @var bool
24     */
25    protected $checkForNonObjectIdentity;
26
27    /**
28     * @var mixed
29     */
30    protected $value;
31
32    /**
33     * @param mixed $value
34     * @param bool  $checkForObjectIdentity
35     * @param bool  $checkForNonObjectIdentity
36     *
37     * @throws PHPUnit_Framework_Exception
38     */
39    public function __construct($value, $checkForObjectIdentity = true, $checkForNonObjectIdentity = false)
40    {
41        parent::__construct();
42
43        if (!is_bool($checkForObjectIdentity)) {
44            throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'boolean');
45        }
46
47        if (!is_bool($checkForNonObjectIdentity)) {
48            throw PHPUnit_Util_InvalidArgumentHelper::factory(3, 'boolean');
49        }
50
51        $this->checkForObjectIdentity    = $checkForObjectIdentity;
52        $this->checkForNonObjectIdentity = $checkForNonObjectIdentity;
53        $this->value                     = $value;
54    }
55
56    /**
57     * Evaluates the constraint for parameter $other. Returns true if the
58     * constraint is met, false otherwise.
59     *
60     * @param mixed $other Value or object to evaluate.
61     *
62     * @return bool
63     */
64    protected function matches($other)
65    {
66        if ($other instanceof SplObjectStorage) {
67            return $other->contains($this->value);
68        }
69
70        if (is_object($this->value)) {
71            foreach ($other as $element) {
72                if ($this->checkForObjectIdentity && $element === $this->value) {
73                    return true;
74                } elseif (!$this->checkForObjectIdentity && $element == $this->value) {
75                    return true;
76                }
77            }
78        } else {
79            foreach ($other as $element) {
80                if ($this->checkForNonObjectIdentity && $element === $this->value) {
81                    return true;
82                } elseif (!$this->checkForNonObjectIdentity && $element == $this->value) {
83                    return true;
84                }
85            }
86        }
87
88        return false;
89    }
90
91    /**
92     * Returns a string representation of the constraint.
93     *
94     * @return string
95     */
96    public function toString()
97    {
98        if (is_string($this->value) && strpos($this->value, "\n") !== false) {
99            return 'contains "' . $this->value . '"';
100        } else {
101            return 'contains ' . $this->exporter->export($this->value);
102        }
103    }
104
105    /**
106     * Returns the description of the failure
107     *
108     * The beginning of failure messages is "Failed asserting that" in most
109     * cases. This method should return the second part of that sentence.
110     *
111     * @param mixed $other Evaluated value or object.
112     *
113     * @return string
114     */
115    protected function failureDescription($other)
116    {
117        return sprintf(
118            '%s %s',
119            is_array($other) ? 'an array' : 'a traversable',
120            $this->toString()
121        );
122    }
123}
124