Passed
Push — main ( 7541f8...c99ed6 )
by Daniel
11:56
created

SplitCamelCaseFormatter   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 15
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 6
c 0
b 0
f 0
dl 0
loc 15
rs 10
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A format() 0 9 2
A __construct() 0 3 1
1
<?php
2
declare(strict_types=1);
3
4
namespace RoadBunch\StringBean;
5
6
7
class SplitCamelCaseFormatter extends AbstractFormatter
8
{
9
    public function __construct(
10
        private readonly string $delimiter = ' ',
11
    ) {}
12
13
    public function format(string $subject): string
14
    {
15
        $regex = '/(?<=[a-z])(?=[A-Z])|(?<=[A-Z\.])(?=[A-Z][a-z])/x';
16
        $words = preg_split($regex, $subject, limit: -1, flags: PREG_SPLIT_NO_EMPTY);
17
18
        if (!empty($words)) {
19
            return implode($this->delimiter, $words);
20
        };
21
        return $subject;
22
    }
23
}
24