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

Part::short()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 15
rs 9.4286
cc 3
eloc 9
nc 3
nop 0
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
}