Passed
Push — new-api ( f151f9...5a646f )
by Sebastian
04:44
created

Factory::createConstraint()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3.0067

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 11
nc 3
nop 3
dl 0
loc 16
ccs 10
cts 11
cp 0.9091
crap 3.0067
rs 9.9
c 1
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
/*
4
 * citeproc-php
5
 *
6
 * @link        http://github.com/seboettg/citeproc-php for the source repository
7
 * @copyright   Copyright (c) 2016 Sebastian Böttger.
8
 * @license     https://opensource.org/licenses/MIT
9
 */
10
11
namespace Seboettg\CiteProc\Constraint;
12
13
use Seboettg\CiteProc\CiteProc;
14
use Seboettg\CiteProc\Exception\ClassNotFoundException;
15
use Seboettg\CiteProc\Rendering\Observer\RenderingObserver;
16
use function Seboettg\CiteProc\ucfirst;
17
18
/**
19
 * Class Factory
20
 * @package Seboettg\CiteProc\Constraint
21
 */
22
abstract class Factory
23
{
24
    const NAMESPACE_CONSTRAINTS = "Seboettg\\CiteProc\\Constraint\\";
25
26
    /**
27
     * @param string $name
28
     * @param string $value
29
     * @param string $match
30
     * @return Constraint
31
     * @throws ClassNotFoundException
32
     */
33 63
    public static function createConstraint(string $name, string $value, string $match): Constraint
34
    {
35 63
        $parts = explode("-", $name);
36
        $className = implode("", array_map(function (string $part) {
37 63
            return ucfirst($part); //use locale-safe ucfirst function
38 63
        }, $parts));
39 63
        $className = self::NAMESPACE_CONSTRAINTS . $className;
40
41 63
        if (!class_exists($className)) {
42
            throw new ClassNotFoundException($className);
43
        }
44 63
        $constraint = new $className($value, $match);
45 63
        if ($constraint instanceof RenderingObserver) {
46 51
            CiteProc::getContext()->addObserver($constraint);
47
        }
48 63
        return $constraint;
49
    }
50
}
51