Completed
Push — master ( df309f...9e12f0 )
by Rafael
08:32
created

ClassUtils::relatedBundleNamespace()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 8
c 0
b 0
f 0
ccs 4
cts 4
cp 1
rs 10
cc 2
nc 2
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 19
    public static function relatedBundleNamespace($class)
26
    {
27
        //symfony4, app does not have bundle namespace
28 19
        if (preg_match('/^App\\\\/', $class)) {
29 1
            return 'App';
30
        }
31
32 19
        return preg_replace('~Bundle\\\\(?!.*Bundle\\\\)[\\\\\w+]+~', null, $class).'Bundle';
33
    }
34
35
    /**
36
     * Apply a naming convention based on namespace and path for given node
37
     *
38
     * e.g.
39
     * $namespace = AppBundle
40
     * $path = Form\Input
41
     * $node = User
42
     * $name = AddUser
43
     * $input = Input
44
     *
45
     * result: AppBundle\Form\Input\User\AddUserInput
46
     *
47
     * @param string      $namespace
48
     * @param string      $path
49
     * @param string      $node
50
     * @param string      $name
51
     * @param string|null $suffix
52
     *
53
     * @return string
54
     */
55 19
    public static function applyNamingConvention(string $namespace, string $path, ?string $node, string $name, ?string $suffix = null)
56
    {
57 19
        if (null === $node) {
58 3
            return sprintf('%s\%s\%s%s', $namespace, $path, ucfirst($name), $suffix);
59
        }
60
61 17
        return sprintf('%s\%s\%s\%s%s', $namespace, $path, $node, ucfirst($name), $suffix);
62
    }
63
64
    /**
65
     * Get default object name based in given class using naming convention
66
     *
67
     * @param string $class
68
     *
69
     * @return string
70
     */
71 4
    public static function getDefaultName(string $class): string
72
    {
73 4
        preg_match('/\w+$/', $class, $matches);
74
75 4
        return $matches[0] ?? '';
76
    }
77
78
    /**
79
     * Get object type using naming convention
80
     * if Query is placed under User\AllUsers namespace, then "User" is the object type
81
     *
82
     * Mutation\User\UpdateUser -> User
83
     * Query\User\Users -> User
84
     * Form\Input\User\AddUserInput -> User
85
     *
86
     * @param string $class
87
     *
88
     * @return string
89
     */
90 2
    public static function getNodeFromClass(string $class): string
91
    {
92 2
        preg_match('/(\w+)(\\\\w+)?\\\\(\w+)$/', $class, $matches);
93
94 2
        return $matches[1];
95
    }
96
}
97