f208b709c47f798721af07b255c3a6b6598b05bb
[yaffs-website] / vendor / phpunit / phpunit / src / Framework / Constraint / ArrayHasKey.php
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 array it is evaluated for has a given key.
13  *
14  * Uses array_key_exists() to check if the key is found in the input array, if
15  * not found the evaluation fails.
16  *
17  * The array key is passed in the constructor.
18  *
19  * @since Class available since Release 3.0.0
20  */
21 class PHPUnit_Framework_Constraint_ArrayHasKey extends PHPUnit_Framework_Constraint
22 {
23     /**
24      * @var int|string
25      */
26     protected $key;
27
28     /**
29      * @param int|string $key
30      */
31     public function __construct($key)
32     {
33         parent::__construct();
34         $this->key = $key;
35     }
36
37     /**
38      * Evaluates the constraint for parameter $other. Returns true if the
39      * constraint is met, false otherwise.
40      *
41      * @param mixed $other Value or object to evaluate.
42      *
43      * @return bool
44      */
45     protected function matches($other)
46     {
47         if (is_array($other)) {
48             return array_key_exists($this->key, $other);
49         }
50
51         if ($other instanceof ArrayAccess) {
52             return $other->offsetExists($this->key);
53         }
54
55         return false;
56     }
57
58     /**
59      * Returns a string representation of the constraint.
60      *
61      * @return string
62      */
63     public function toString()
64     {
65         return 'has the key ' . $this->exporter->export($this->key);
66     }
67
68     /**
69      * Returns the description of the failure
70      *
71      * The beginning of failure messages is "Failed asserting that" in most
72      * cases. This method should return the second part of that sentence.
73      *
74      * @param mixed $other Evaluated value or object.
75      *
76      * @return string
77      */
78     protected function failureDescription($other)
79     {
80         return 'an array ' . $this->toString();
81     }
82 }