ShippingRateInputDraft::fieldDefinitions()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
/**
3
 * @author @jayS-de <[email protected]>
4
 */
5
6
namespace Commercetools\Core\Model\Cart;
7
8
use Commercetools\Core\Model\Common\Context;
9
use Commercetools\Core\Model\Common\JsonObject;
10
11
/**
12
 * @package Commercetools\Core\Model\Cart
13
 * @link http://dev.commercetools.com/http-api-projects-carts.html#shippingrateinput
14
 * @method string getType()
15
 * @method ShippingRateInputDraft setType(string $type = null)
16
 */
17
abstract class ShippingRateInputDraft extends JsonObject
18
{
19
    const INPUT_TYPE = '';
20
21
    /**
22
     * @inheritDoc
23
     */
24 4
    public function __construct(array $data = [], $context = null)
25
    {
26 4
        if (static::INPUT_TYPE != '' && !isset($data[static::TYPE])) {
0 ignored issues
show
introduced by
The condition static::INPUT_TYPE != '' is always false.
Loading history...
27
            $data[static::TYPE] = static::INPUT_TYPE;
28
        }
29 4
        parent::__construct($data, $context);
30 4
    }
31
32 2
    public function fieldDefinitions()
33
    {
34
        return [
35 2
            'type' => [static::TYPE => 'string'],
36
        ];
37
    }
38
39
    /**
40
     * @param array $data
41
     * @param Context|callable $context
42
     * @return static
43
     */
44
    public static function fromArray(array $data, $context = null)
45
    {
46
        if (get_called_class() == ShippingRateInput::class && isset($data[static::TYPE])) {
47
            $className = static::inputType($data[static::TYPE]);
48
            if (class_exists($className)) {
49
                return new $className($data, $context);
50
            }
51
        }
52
        return new static($data, $context);
53
    }
54
55
    protected static function inputType($type)
56
    {
57
        $types = [
58
            ClassificationShippingRateInput::INPUT_TYPE => ClassificationShippingRateInputDraft::class,
59
            ScoreShippingRateInput::INPUT_TYPE => ScoreShippingRateInputDraft::class,
60
        ];
61
        return isset($types[$type]) ? $types[$type] : ShippingRateInputDraft::class;
62
    }
63
64
    /**
65
     * @param string $type
66
     * @param Context|callable $context
67
     * @return static
68
     */
69
    protected static function ofType($type, $context = null)
70
    {
71
        $input = static::of($context);
72
        return $input->setType($type);
73
    }
74
}
75