LocalePathsTrait::generateLocalePath()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 14
ccs 0
cts 8
cp 0
rs 10
cc 2
nc 2
nop 0
crap 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Zvermafia\Larastate\Traits;
6
7
use Exception;
8
9
trait LocalePathsTrait
10
{
11
    use HelpersTrait;
12
13
    /**
14
     * Get a locales path for an entity.
15
     *
16
     * @return string
17
     */
18
    protected function getLocalePath(): string
19
    {
20
        if (isset($this->locale_path) && is_string($this->locale_path)) {
21
            return $this->locale_path;
22
        }
23
24
        return $this->generateLocalePath();
25
    }
26
27
    /**
28
     * Generate a locales path by the current class name.
29
     *
30
     * @throws \Exception
31
     * @return string
32
     */
33
    protected function generateLocalePath(): string
34
    {
35
        $pattern = '/(?<entity_name>^[a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*)(?<postfix>State)$/';
36
        $matches = [];
37
38
        // if the class name matches the state class rule then the $matches[1] is value will be the entity name
39
        // else throw an exception
40
        if (! preg_match($pattern, $this->reflection->getShortName(), $matches)) {
41
            throw new Exception("The {$this->reflection->getShortName()} doesn't match the state class name rule.");
42
        }
43
44
        $entity_name = $this->makeSnakeCase($matches['entity_name']);
45
46
        return "entities/{$entity_name}";
47
    }
48
}
49