Completed
Push — master ( 4b89b9...dd9abc )
by Jens
10:35
created

ApiError::fromArray()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.0416

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 10
ccs 5
cts 6
cp 0.8333
rs 9.4285
cc 3
eloc 6
nc 3
nop 2
crap 3.0416
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 18
    public function fieldDefinitions()
22
    {
23
        return [
24 18
            'code' => [static::TYPE => 'string'],
25 18
            'message' => [static::TYPE => 'string']
26
        ];
27
    }
28
29
    /**
30
     * @param array $data
31
     * @param Context|callable $context
32
     * @return static
33
     */
34 20
    public static function fromArray(array $data, $context = null)
35
    {
36 20
        if (isset($data['code'])) {
37 20
            $className = sprintf('\Commercetools\Core\Error\%sError', self::pascalcase($data['code']));
38 20
            if (class_exists($className)) {
39 20
                return new $className($data, $context);
40
            }
41
        }
42
        return new static($data, $context);
43
    }
44
45 20
    protected static function pascalcase($name)
46
    {
47 20
        return ucfirst(
48
            implode(
49 20
                '',
50
                array_map(
51 20
                    'ucfirst',
52 20
                    explode('_', $name)
53
                )
54
            )
55
        );
56
    }
57
}
58