Completed
Push — v0.7 ( 21297d...0d3226 )
by Nate
02:43
created

Context::getAttributes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 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
abstract class Context
11
{
12
    /**
13
     * User defined attributes
14
     *
15
     * @var mixed[]
16
     */
17
    protected $attributes = [];
18
19
    /**
20
     * If the default scalar type adapters should be enabled
21
     *
22
     * @var bool
23
     */
24
    protected $enableScalarAdapters = false;
25
26
    /**
27
     * Get an array of user defined attributes
28
     *
29
     * @return array
30
     */
31 2
    public function getAttributes(): array
32
    {
33 2
        return $this->attributes;
34
    }
35
36
    /**
37
     * Returns the attribute value for a given key or null if it's missing
38
     *
39
     * @param string $key
40
     * @return mixed
41
     */
42 2
    public function getAttribute(string $key)
43
    {
44 2
        return $this->attributes[$key] ?? null;
45
    }
46
47
    /**
48
     * Add a custom attribute
49
     *
50
     * @param string $key
51
     * @param $value
52
     * @return Context
53
     */
54 2
    public function setAttribute(string $key, $value): Context
55
    {
56 2
        $this->attributes[$key] = $value;
57
58 2
        return $this;
59
    }
60
61
    /**
62
     * If scalar values should get sent through the type adapter
63
     * system or if they should just be read/written in place.
64
     *
65
     * @return bool
66
     */
67 5
    public function enableScalarAdapters(): bool
68
    {
69 5
        return $this->enableScalarAdapters;
70
    }
71
72
    /**
73
     * Defaults to true. Set to false if scalar values should be read/written
74
     * through a type adapter.
75
     *
76
     * @param bool $enable
77
     * @return Context
78
     */
79 2
    public function setEnableScalarAdapters(bool $enable): Context
80
    {
81
82 2
        $this->enableScalarAdapters = $enable;
83
84 2
        return $this;
85
    }
86
}
87