Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php declare(strict_types=1);
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
namespace PHPUnit\Framework\Constraint;
11
 
12
use function array_key_exists;
13
use function is_array;
14
use ArrayAccess;
15
 
16
/**
17
 * @no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit
18
 */
19
final class ArrayHasKey extends Constraint
20
{
21
    /**
22
     * @var int|string
23
     */
24
    private $key;
25
 
26
    /**
27
     * @param int|string $key
28
     */
29
    public function __construct($key)
30
    {
31
        $this->key = $key;
32
    }
33
 
34
    /**
35
     * Returns a string representation of the constraint.
36
     *
37
     * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
38
     */
39
    public function toString(): string
40
    {
41
        return 'has the key ' . $this->exporter()->export($this->key);
42
    }
43
 
44
    /**
45
     * Evaluates the constraint for parameter $other. Returns true if the
46
     * constraint is met, false otherwise.
47
     *
48
     * @param mixed $other value or object to evaluate
49
     */
50
    protected function matches($other): bool
51
    {
52
        if (is_array($other)) {
53
            return array_key_exists($this->key, $other);
54
        }
55
 
56
        if ($other instanceof ArrayAccess) {
57
            return $other->offsetExists($this->key);
58
        }
59
 
60
        return false;
61
    }
62
 
63
    /**
64
     * Returns the description of the failure.
65
     *
66
     * The beginning of failure messages is "Failed asserting that" in most
67
     * cases. This method should return the second part of that sentence.
68
     *
69
     * @param mixed $other evaluated value or object
70
     *
71
     * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
72
     */
73
    protected function failureDescription($other): string
74
    {
75
        return 'an array ' . $this->toString();
76
    }
77
}