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

ApiError::fieldDefinitions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 7
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
crap 1
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