Passed
Push — master ( 36097f...93304f )
by Rafael
03:14
created

ClassUtils::getNodeFromClass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 0
cts 4
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
/*******************************************************************************
3
 *  This file is part of the GraphQL Bundle package.
4
 *
5
 *  (c) YnloUltratech <[email protected]>
6
 *
7
 *  For the full copyright and license information, please view the LICENSE
8
 *  file that was distributed with this source code.
9
 ******************************************************************************/
10
11
namespace Ynlo\GraphQLBundle\Util;
12
13
/**
14
 * ClassUtils
15
 */
16
class ClassUtils
17
{
18
    /**
19
     * Get the bundle namespace class to any class inside a bundle
20
     *
21
     * @param string $class
22
     *
23
     * @return string
24
     */
25
    public static function relatedBundleNamespace($class)
26
    {
27
        return preg_replace('~Bundle(?!.*Bundle)[\\\\\w+]+~', null, $class).'Bundle';
28
    }
29
30
    /**
31
     * Apply a naming convention based on namespace and path for given node
32
     *
33
     * e.g.
34
     * $namespace = AppBundle
35
     * $path = Form\Input
36
     * $node = User
37
     * $name = AddUser
38
     * $input = Input
39
     *
40
     * result: AppBundle\Form\Input\User\AddUserInput
41
     *
42
     * @param string      $namespace
43
     * @param string      $path
44
     * @param string      $node
45
     * @param string      $name
46
     * @param string|null $suffix
47
     *
48
     * @return string
49
     */
50
    public static function applyNamingConvention(string $namespace, string $path, ?string $node, string $name, ?string $suffix = null)
51
    {
52
        if (null === $node) {
53
            return sprintf('%s\%s\%s%s', $namespace, $path, ucfirst($name), $suffix);
54
        }
55
56
        return sprintf('%s\%s\%s\%s%s', $namespace, $path, $node, ucfirst($name), $suffix);
57
    }
58
59
    /**
60
     * Get default object name based in given class using naming convention
61
     *
62
     * @param string $class
63
     *
64
     * @return string
65
     */
66
    public static function getDefaultName(string $class): string
67
    {
68
        preg_match('/\w+$/', $class, $matches);
69
70
        return $matches[0] ?? '';
71
    }
72
73
    /**
74
     * Get object type using naming convention
75
     * if Query is placed under User\AllUsers namespace, then "User" is the object type
76
     *
77
     * Mutation\User\UpdateUser -> User
78
     * Query\User\Users -> User
79
     * Form\Input\User\AddUserInput -> User
80
     *
81
     * @param string $class
82
     *
83
     * @return string
84
     */
85
    public static function getNodeFromClass(string $class): string
86
    {
87
        preg_match('/(\w+)(\\\\w+)?\\\\(\w+)$/', $class, $matches);
88
89
        return $matches[1];
90
    }
91
}
92