ContentTypeConstraint::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * This source file is proprietary and part of Rebilly.
4
 *
5
 * (c) Rebilly SRL
6
 *     Rebilly Ltd.
7
 *     Rebilly Inc.
8
 *
9
 * @see https://www.rebilly.com
10
 */
11
12
namespace Rebilly\OpenAPI\PhpUnit;
13
14
use PHPUnit\Framework\Constraint\Constraint;
15
16
/**
17
 * Constraint that asserts that the content-type matches the expected types.
18
 *
19
 * TODO: Checking params, instead of skipping
20
 */
21
final class ContentTypeConstraint extends Constraint
22
{
23
    private $allowedTypes;
24
25 9
    public function __construct(array $allowedTypes)
26
    {
27 9
        $this->allowedTypes = array_map([$this, 'stripParams'], $allowedTypes);
28
    }
29
30 2
    public function toString(): string
31
    {
32 2
        return 'is an allowed content-type (' . implode(', ', $this->allowedTypes) . ')';
33
    }
34
35 9
    protected function matches($other): bool
36
    {
37 9
        return in_array($this->stripParams($other), $this->allowedTypes, true);
38
    }
39
40 9
    private static function stripParams(string $type): string
41
    {
42 9
        return mb_strstr($type, ';', true) ?: $type;
43
    }
44
}
45