Completed
Push — master ( d893b1...ec35b3 )
by Tristan
03:23
created

Formatter::processString()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4286
cc 3
eloc 4
nc 2
nop 3
1
<?php
2
3
namespace Enzyme\Name;
4
5
class Formatter implements FormatInterface
6
{
7
    /**
8
     * A local reference to the name being formatted.
9
     * @var NameInterface
10
     */
11
    protected $name;
12
13
    /**
14
     * Create a new formatter given the specified name
15
     *
16
     * @param NameInterface $name The name to format.
17
     */
18
    public function __construct(NameInterface $name)
19
    {
20
        $this->name = $name;
21
    }
22
23
    /**
24
     * A static method for quickly formatting a name.
25
     *
26
     * @param NameInterface $name   The name to format.
27
     * @param string        $format The format to follow.
28
     *
29
     * @return string
30
     */
31
    public static function nameLike(NameInterface $name, $format)
32
    {
33
        $fmt = new static($name);
34
35
        return $fmt->like($format);
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    public function like($format)
42
    {
43
        $fmt_parts = explode(' ', $format);
44
        $final_fmt = [];
45
46
        foreach ($fmt_parts as $part) {
47
            $formatted_string = $this->format($part);
48
49
            if($formatted_string !== null) {
50
                $final_fmt[] = $formatted_string;
51
            }
52
        }
53
54
        return implode(' ', $final_fmt);
55
    }
56
57
    /**
58
     * Format and return the given string chunk otherise
59
     * return null.
60
     *
61
     * @param string $string The string to format.
62
     *
63
     * @return string | null
64
     */
65
    protected function format($string)
66
    {
67
        if(strlen(trim($string)) < 1) {
68
            return null;
69
        }
70
71
        $short = true;
72
        switch (substr($string, 0, 2)) {
73
            case 'Fi':
74
                return $this->processString('First', $this->name->first(), $string);
75
76
            case 'F.':
77
                return $this->processString('F.', $this->name->first($short), $string);
78
79
            case 'La':
80
                return $this->processString('Last', $this->name->last(), $string);
81
82
            case 'L.':
83
                return $this->processString('L.', $this->name->last($short), $string);
84
85
            case 'Mi':
86
                return $this->processString('Middle', $this->name->middle(), $string);
87
88
            case 'M.':
89
                return $this->processString('M.', $this->name->middle($short), $string);
90
91
            default:
92
                return trim($string);
93
        }
94
    }
95
96
    /**
97
     * Tries to process the given string chunk, otherwise
98
     * returns null.
99
     *
100
     * @param string $needle The format specifier.
101
     * @param string $name   The name part.
102
     * @param string $string The full string to format.
103
     *
104
     * @return string | null
105
     */
106
    protected function processString($needle, $name, $string)
107
    {
108
        if($name === null || strlen($name) < 1) {
109
            return null;
110
        }
111
112
        return str_replace($needle, $name, $string);
113
    }
114
}
115