Test Failed
Push — master ( fe7900...79d767 )
by Attila
11:12
created

is_a_concord_module_class()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * Contains the Concord helper functions
7
 *
8
 * @copyright   Copyright (c) 2016 Attila Fulop
9
 * @author      Attila Fulop
10
 * @license     MIT
11
 * @since       2016-09-25
12
 *
13
 */
14
use Illuminate\Support\Arr;
15
use Illuminate\Support\Str;
16
use Konekt\Concord\Contracts\Concord;
17
use Konekt\Concord\Contracts\Module;
18
19
/**
20
 * Converts a fully qualified classname to a string (backslashes to dots, parts to snake case)
21
 *
22
 * Eg.: '\App\Services\BamBamService' -> 'app.services.bam_bam_service'
23
 *
24
 * @param string    $classname
25
 *
26
 * @return string
27
 */
28
function classpath_to_slug($classname)
29
{
30
    $parts = explode('\\', $classname);
31
    // Remove first part if empty ie. begins with \
32
    if (empty($parts[0])) {
33
        $parts = Arr::except($parts, 0);
34
    }
35
    // Remove last part if empty ie. ends to \
36
    if (empty($parts[count($parts) - 1])) {
37
        $parts = Arr::except($parts, count($parts) - 1);
38
    }
39
40
    array_walk($parts, function (&$part) {
41
        $part = Str::snake($part);
42
    });
43
44
    return implode('.', $parts);
45
}
46
47
/**
48
 * Counterpart of classpath_to_str, that converts the string back to a fully qualified classname
49
 *
50
 * Eg.: 'app.services.bam_bam_service' -> '\App\Services\BamBamService'
51
 *
52
 * @see classpath_to_str()
53
 *
54
 * @param string    $str
55
 *
56
 * @return string
57
 */
58
function slug_to_classpath($str)
59
{
60
    $parts = explode('.', $str);
61
62
    array_walk($parts, function (&$part) {
63
        $part = Str::studly($part);
64
    });
65
66
    return implode('\\', $parts);
67
}
68
69
/**
70
 * Shortcut function for returning helper instances by their name
71
 *
72
 * @param string    $name       The name of the helper
73
 * @param array     $arguments  Optional arguments to pass to the helper class
74
 *
75
 * @return object|null
76
 */
77
function helper($name, $arguments = [])
78
{
79
    return concord()->helper($name, $arguments);
80
}
81
82
/**
83
 * Returns the concord instance
84
 *
85
 * @return Concord
86
 */
87
function concord()
88
{
89
    return app('concord');
0 ignored issues
show
Bug Best Practice introduced by
The expression return app('concord') also could return the type Illuminate\Contracts\Foundation\Application which is incompatible with the documented return type Konekt\Concord\Contracts\Concord.
Loading history...
90
}
91
92
/**
93
 * Returns the classname shortened (no namespace, base class name only, snake_case
94
 *
95
 * @param string $classname
96
 *
97
 * @return string
98
 */
99
function shorten($classname)
100
{
101
    return Str::snake(class_basename($classname));
102
}
103
104
/**
105
 * Shorthand function for returning an enum object by it's short name
106
 *
107
 * @param string    $shortname  The short name of the enum
108
 * @param mixed     $value      The value to create the enum with
109
 *
110
 * @return \Konekt\Enum\Enum
111
 */
112
function enum($shortname, $value = null)
113
{
114
    $abstract = concord()->short($shortname);
115
    if ($abstract && $class = concord()->enum($abstract)) {
116
        return new $class($value);
117
    }
118
}
119
120
function is_a_concord_module_class(string $class): bool
121
{
122
    return in_array(Module::class, class_implements($class));
123
}
124
125
function is_not_a_concord_module_class(string $class): bool
126
{
127
    return !is_a_concord_module_class($class);
128
}
129