Completed
Push — master ( 34fcab...f7da4c )
by Veaceslav
04:53 queued 02:37
created

ContentTypeConstraint::stripParams()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 2
eloc 2
nc 2
nop 1
crap 2
1
<?php
2
/**
3
 * This file is part of Rebilly.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 *
8
 * @see http://rebilly.com
9
 */
10
11
namespace Rebilly\OpenAPI\PhpUnit;
12
13
use PHPUnit_Framework_Constraint as Constraint;
14
15
/**
16
 * Constraint that asserts that the content-type matches the expected types.
17
 *
18
 * TODO: Checking params, instead of skipping
19
 */
20
final class ContentTypeConstraint extends Constraint
21
{
22
    /**
23
     * @var array
24
     */
25
    private $allowedTypes;
26
27
    /**
28
     * @param array $allowedTypes
29
     */
30 5
    public function __construct(array $allowedTypes)
31
    {
32 5
        parent::__construct();
33
34 5
        $this->allowedTypes = array_map([$this, 'stripParams'], $allowedTypes);
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40 5
    protected function matches($other)
41
    {
42 5
        return in_array($this->stripParams($other), $this->allowedTypes, true);
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48 1
    public function toString()
49
    {
50 1
        return 'is an allowed content-type (' . implode(', ', $this->allowedTypes) . ')';
51
    }
52
53
    /**
54
     * @param string $type
55
     *
56
     * @return string
57
     */
58 5
    private static function stripParams($type)
59
    {
60 5
        return strstr($type, ';', true) ?: $type;
61
    }
62
}
63