Context   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 10
c 1
b 0
f 0
dl 0
loc 74
ccs 12
cts 12
cp 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A enableScalarAdapters() 0 3 1
A setAttribute() 0 5 1
A getAttribute() 0 3 1
A getAttributes() 0 3 1
A setEnableScalarAdapters() 0 5 1
1
<?php
2
/*
3
 * Copyright (c) Nate Brunette.
4
 * Distributed under the MIT License (http://opensource.org/licenses/MIT)
5
 */
6
declare(strict_types=1);
7
8
namespace Tebru\Gson\Context;
9
10
/**
11
 * Class Context
12
 *
13
 * @author Nate Brunette <[email protected]>
14
 */
15
abstract class Context
16
{
17
    /**
18
     * User defined attributes
19
     *
20
     * @var mixed[]
21
     */
22
    protected $attributes = [];
23
24
    /**
25
     * If the default scalar type adapters should be enabled
26
     *
27
     * @var bool
28
     */
29
    protected $enableScalarAdapters = true;
30
31
    /**
32
     * Get an array of user defined attributes
33
     *
34
     * @return array
35
     */
36 2
    public function getAttributes(): array
37
    {
38 2
        return $this->attributes;
39
    }
40
41
    /**
42
     * Returns the attribute value for a given key or null if it's missing
43
     *
44
     * @param string $key
45
     * @return mixed
46
     */
47 2
    public function getAttribute(string $key)
48
    {
49 2
        return $this->attributes[$key] ?? null;
50
    }
51
52
    /**
53
     * Add a custom attribute
54
     *
55
     * @param string $key
56
     * @param $value
57
     * @return Context
58
     */
59 2
    public function setAttribute(string $key, $value): Context
60
    {
61 2
        $this->attributes[$key] = $value;
62
63 2
        return $this;
64
    }
65
66
    /**
67
     * If scalar values should get sent through the type adapter
68
     * system or if they should just be read/written in place.
69
     *
70
     * @return bool
71
     */
72 5
    public function enableScalarAdapters(): bool
73
    {
74 5
        return $this->enableScalarAdapters;
75
    }
76
77
    /**
78
     * Defaults to true. Set to false if scalar values should be read/written
79
     * through a type adapter.
80
     *
81
     * @param bool $enable
82
     * @return Context
83
     */
84 2
    public function setEnableScalarAdapters(bool $enable): Context
85
    {
86 2
        $this->enableScalarAdapters = $enable;
87
88 2
        return $this;
89
    }
90
}
91