Completed
Push — master ( 494091...32c874 )
by David
27s
created

PluginCapitalize::process()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 12
nc 4
nop 2
dl 0
loc 18
rs 8.8571
c 0
b 0
f 0
1
<?php
2
/**
3
 * Copyright (c) 2013-2017
4
 *
5
 * @category  Library
6
 * @package   Dwoo\Plugins\Functions
7
 * @author    Jordi Boggiano <[email protected]>
8
 * @author    David Sanchez <[email protected]>
9
 * @copyright 2008-2013 Jordi Boggiano
10
 * @copyright 2013-2017 David Sanchez
11
 * @license   http://dwoo.org/LICENSE Modified BSD License
12
 * @version   1.3.2
13
 * @date      2017-01-06
14
 * @link      http://dwoo.org/
15
 */
16
17
namespace Dwoo\Plugins\Functions;
18
19
use Dwoo\Plugin;
20
21
/**
22
 * Capitalizes the first letter of each word
23
 * <pre>
24
 *  * value : the string to capitalize
25
 *  * numwords : if true, the words containing numbers are capitalized as well
26
 * </pre>
27
 * This software is provided 'as-is', without any express or implied warranty.
28
 * In no event will the authors be held liable for any damages arising from the use of this software.
29
 */
30
class PluginCapitalize extends Plugin
31
{
32
    /**
33
     * @param string $value
34
     * @param bool   $numwords
35
     *
36
     * @return string
37
     */
38
    public function process($value, $numwords = false)
39
    {
40
        if ($numwords || preg_match('#^[^0-9]+$#', $value)) {
41
            return mb_convert_case((string)$value, MB_CASE_TITLE, $this->core->getCharset());
42
        } else {
43
            $bits = explode(' ', (string)$value);
44
            $out  = '';
45
            while (list(, $v) = each($bits)) {
46
                if (preg_match('#^[^0-9]+$#', $v)) {
47
                    $out .= ' ' . mb_convert_case($v, MB_CASE_TITLE, $this->core->getCharset());
48
                } else {
49
                    $out .= ' ' . $v;
50
                }
51
            }
52
53
            return substr($out, 1);
54
        }
55
    }
56
}
57