Constraint::getName()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
/**
4
 * This file is part of Railt package.
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
declare(strict_types=1);
11
12
namespace Railt\SDL\Spec\Constraint;
13
14
use Phplrt\Contracts\Ast\NodeInterface;
15
use Railt\SDL\Spec\SpecificationInterface;
16
17
/**
18
 * Class Constraint
19
 */
20
abstract class Constraint
21
{
22
    /**
23
     * @var string
24
     */
25
    protected const ERROR_NOT_SUPPORTED_MESSAGE =
26
        '%s not supported by GraphQL %s specification. Please indicate a more ' .
27
        'modern version of the language specification to enable support for ' .
28
        'this functionality';
29
30
    /**
31
     * @param NodeInterface $node
32
     * @param SpecificationInterface $spec
33
     * @return void
34
     */
35
    abstract public static function assert(NodeInterface $node, SpecificationInterface $spec): void;
36
37
    /**
38
     * @param SpecificationInterface $spec
39
     * @return string
40
     */
41
    protected static function notSupported(SpecificationInterface $spec): string
42
    {
43
        return \vsprintf(self::ERROR_NOT_SUPPORTED_MESSAGE, [
44
            static::getName(),
45
            static::basename(\get_class($spec)),
46
        ]);
47
    }
48
49
    /**
50
     * @return string
51
     */
52
    public static function getName(): string
53
    {
54
        $class = self::basename(static::class);
55
56
        return \ucfirst(\strtolower(\preg_replace('/(.)(?=[A-Z])/u', '$1 ', $class)));
57
    }
58
59
    /**
60
     * @param string $fqn
61
     * @return string
62
     */
63
    protected static function basename(string $fqn): string
64
    {
65
        return \basename(\str_replace('\\', \DIRECTORY_SEPARATOR, $fqn));
66
    }
67
}
68