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 PHPUnit\Framework\ExpectationFailedException;
13
use Traversable;
14
 
15
/**
16
 * @no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit
17
 */
18
final class TraversableContainsOnly extends Constraint
19
{
20
    /**
21
     * @var Constraint
22
     */
23
    private $constraint;
24
 
25
    /**
26
     * @var string
27
     */
28
    private $type;
29
 
30
    /**
31
     * @throws \PHPUnit\Framework\Exception
32
     */
33
    public function __construct(string $type, bool $isNativeType = true)
34
    {
35
        if ($isNativeType) {
36
            $this->constraint = new IsType($type);
37
        } else {
38
            $this->constraint = new IsInstanceOf(
39
                $type
40
            );
41
        }
42
 
43
        $this->type = $type;
44
    }
45
 
46
    /**
47
     * Evaluates the constraint for parameter $other.
48
     *
49
     * If $returnResult is set to false (the default), an exception is thrown
50
     * in case of a failure. null is returned otherwise.
51
     *
52
     * If $returnResult is true, the result of the evaluation is returned as
53
     * a boolean value instead: true in case of success, false in case of a
54
     * failure.
55
     *
56
     * @param mixed|Traversable $other
57
     *
58
     * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
59
     * @throws ExpectationFailedException
60
     */
61
    public function evaluate($other, string $description = '', bool $returnResult = false): ?bool
62
    {
63
        $success = true;
64
 
65
        foreach ($other as $item) {
66
            if (!$this->constraint->evaluate($item, '', true)) {
67
                $success = false;
68
 
69
                break;
70
            }
71
        }
72
 
73
        if ($returnResult) {
74
            return $success;
75
        }
76
 
77
        if (!$success) {
78
            $this->fail($other, $description);
79
        }
80
 
81
        return null;
82
    }
83
 
84
    /**
85
     * Returns a string representation of the constraint.
86
     */
87
    public function toString(): string
88
    {
89
        return 'contains only values of type "' . $this->type . '"';
90
    }
91
}