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 json_decode;
13
use function json_last_error;
14
use function sprintf;
15
 
16
/**
17
 * @no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit
18
 */
19
final class IsJson extends Constraint
20
{
21
    /**
22
     * Returns a string representation of the constraint.
23
     */
24
    public function toString(): string
25
    {
26
        return 'is valid JSON';
27
    }
28
 
29
    /**
30
     * Evaluates the constraint for parameter $other. Returns true if the
31
     * constraint is met, false otherwise.
32
     *
33
     * @param mixed $other value or object to evaluate
34
     */
35
    protected function matches($other): bool
36
    {
37
        if ($other === '') {
38
            return false;
39
        }
40
 
41
        json_decode($other);
42
 
43
        if (json_last_error()) {
44
            return false;
45
        }
46
 
47
        return true;
48
    }
49
 
50
    /**
51
     * Returns the description of the failure.
52
     *
53
     * The beginning of failure messages is "Failed asserting that" in most
54
     * cases. This method should return the second part of that sentence.
55
     *
56
     * @param mixed $other evaluated value or object
57
     *
58
     * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
59
     */
60
    protected function failureDescription($other): string
61
    {
62
        if ($other === '') {
63
            return 'an empty string is valid JSON';
64
        }
65
 
66
        json_decode($other);
67
        $error = (string) JsonMatchesErrorMessageProvider::determineJsonError(
68
            (string) json_last_error()
69
        );
70
 
71
        return sprintf(
72
            '%s is valid JSON (%s)',
73
            $this->exporter()->shortenedExport($other),
74
            $error
75
        );
76
    }
77
}