Completed
Push — master ( fef923...c0df34 )
by Rafael
03:03
created

CamelizeMatcher::match()   B

Complexity

Conditions 6
Paths 2

Size

Total Lines 14
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 14
rs 8.8571
cc 6
eloc 9
nc 2
nop 2
1
<?php
2
3
/**
4
 * LICENSE: This file is subject to the terms and conditions defined in
5
 * file 'LICENSE', which is part of this source code package.
6
 *
7
 * @copyright 2016 Copyright(c) - All rights reserved.
8
 */
9
10
namespace Rafrsr\LibArray2Object\Matcher;
11
12
class CamelizeMatcher implements PropertyMatcherInterface
13
{
14
    /**
15
     * @inheritDoc
16
     */
17
    public function match(\ReflectionProperty $property, $givenName)
18
    {
19
        $propertyName = $property->getName();
20
        if ($propertyName === $givenName
21
            || $propertyName === $this->camelize($givenName) //ErrorCode = error_code
22
            || $propertyName === lcfirst($this->camelize($givenName)) // errorCode => error_code
23
            || $propertyName === strtolower($this->camelize($givenName)) // errorcode => error_code
24
            || strtolower($propertyName) === $givenName // errorCode => errorcode
25
        ) {
26
            return true;
27
        }
28
29
        return false;
30
    }
31
32
    /**
33
     * @param $name
34
     *
35
     * @return string
36
     */
37
    private function camelize($name)
38
    {
39
        return strtr(ucwords(strtr($name, ['_' => ' '])), [' ' => '']);
40
    }
41
}