Completed
Push — master ( 8ed474...52c8eb )
by Tristan
03:25
created

Part   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 5
c 2
b 0
f 0
lcom 1
cbo 1
dl 0
loc 55
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A long() 0 4 1
A short() 0 15 3
1
<?php
2
3
namespace Enzyme\Name;
4
5
use Stringy\Stringy as S;
6
7
class Part
8
{
9
    /**
10
     * A store of the name segments.
11
     *
12
     * @var array
13
     */
14
    protected $segments = [];
15
16
    /**
17
     * Creates a new part given the string part.
18
     *
19
     * @param string $string The name part.
20
     */
21
    public function __construct($string)
22
    {
23
        $parts = S::create($string)->split(' ');
24
        $segments = array_filter($parts, function($part) {
25
            return S::create($part)->trim()->length() > 0;
26
        });
27
28
        $this->segments = $segments;
29
    }
30
31
    /**
32
     * Returns the long (original) part of this name.
33
     *
34
     * @return string
35
     */
36
    public function long()
37
    {
38
        return implode(' ', $this->segments);
39
    }
40
41
    /**
42
     * Returns the shortened version of this name.
43
     *
44
     * @return string
45
     */
46
    public function short()
47
    {
48
        $shortened_parts = [];
49
        foreach ($this->segments as $part) {
50
            $string = S::create($part);
51
52
            if ($string->contains('.')) {
53
                $shortened_parts[] = $string;
54
            } else {
55
                $shortened_parts[] = $string->truncate(2, '.');
56
            }
57
        }
58
59
        return implode(' ', $shortened_parts);
60
    }
61
}