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

Factory   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Test Coverage

Coverage 90.91%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
dl 0
loc 27
ccs 10
cts 11
cp 0.9091
rs 10
c 1
b 0
f 0
wmc 3

1 Method

Rating   Name   Duplication   Size   Complexity  
A createConstraint() 0 16 3
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