Completed
Push — develop ( 6886d0...76ffc7 )
by Jens
10:14
created

ApiError::camelize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 9

Duplication

Lines 15
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 1 Features 1
Metric Value
c 1
b 1
f 1
dl 15
loc 15
ccs 0
cts 0
cp 0
rs 9.4285
cc 1
eloc 9
nc 1
nop 1
crap 2
1
<?php
2
/**
3
 * @author @jayS-de <[email protected]>
4
 */
5
6
namespace Commercetools\Core\Error;
7
8
use Commercetools\Core\Model\Common\Context;
9
use Commercetools\Core\Model\Common\JsonObject;
10
11
/**
12
 * @package Commercetools\Core\Error
13
 *
14
 * @method string getCode()
15
 * @method ApiError setCode(string $code = null)
16
 * @method string getMessage()
17
 * @method ApiError setMessage(string $message = null)
18
 */
19
class ApiError extends JsonObject
20
{
21 15
    public function fieldDefinitions()
22
    {
23
        return [
24 15
            'code' => [static::TYPE => 'string'],
25 15
            'message' => [static::TYPE => 'string']
26
        ];
27
    }
28
29
    /**
30
     * @param array $data
31
     * @param Context|callable $context
32
     * @return static
33
     */
34 17
    public static function fromArray(array $data, $context = null)
35
    {
36 17
        if (isset($data['code'])) {
37 17
            $className = sprintf('\Commercetools\Core\Error\%sError', self::camelize($data['code']));
38 17
            if (class_exists($className)) {
39 17
                return new $className($data, $context);
40
            }
41
        }
42
        return new static($data, $context);
43
    }
44
45 View Code Duplication
    protected static function camelize($name)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
46
    {
47
        return lcfirst(
48
            implode(
49
                '',
50
                array_map(
51
                    'ucfirst',
52
                    array_map(
53
                        'strtolower',
54
                        explode('_', $name)
55
                    )
56
                )
57
            )
58
        );
59
    }
60
}
61