Completed
Push — master ( 3071de...f5f5ef )
by Bernhard
02:13
created

VersionFieldVersioner::parseVersion()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 11
ccs 6
cts 6
cp 1
rs 9.4285
cc 2
eloc 6
nc 2
nop 1
crap 2
1
<?php
2
3
/*
4
 * This file is part of the webmozart/json package.
5
 *
6
 * (c) Bernhard Schussek <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Webmozart\Json\Versioning;
13
14
use stdClass;
15
16
/**
17
 * Expects the version to be set in the "version" field of a JSON object.
18
 *
19
 * @since  1.3
20
 *
21
 * @author Bernhard Schussek <[email protected]>
22
 */
23
class VersionFieldVersioner implements JsonVersioner
24
{
25
    /**
26
     * @var string
27
     */
28
    private $fieldName;
29
30
    /**
31
     * Creates a new versioner.
32
     *
33
     * @param string $fieldName The name of the version field.
34
     */
35 6
    public function __construct($fieldName = 'version')
36
    {
37 6
        $this->fieldName = (string) $fieldName;
38 6
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43 3
    public function parseVersion(stdClass $jsonData)
44
    {
45 3
        if (!isset($jsonData->{$this->fieldName})) {
46 1
            throw new CannotParseVersionException(sprintf(
47 1
                'Cannot find "%s" property in JSON object.',
48 1
                $this->fieldName
49
            ));
50
        }
51
52 2
        return $jsonData->{$this->fieldName};
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58 3
    public function updateVersion(stdClass $jsonData, $version)
59
    {
60 3
        $jsonData->{$this->fieldName} = $version;
61 3
    }
62
}
63