Completed
Push — master ( 7cdf63...105f03 )
by Sam
04:17
created

ArrayResponse::transformPropertyName()   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 0
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Pheanstalk\Response;
4
5
use Pheanstalk\Contract\ResponseInterface;
6
7
/**
8
 * A response with an ArrayObject interface to key => value data.
9
 *
10
 * @author  Paul Annesley
11
 * @package Pheanstalk
12
 * @license http://www.opensource.org/licenses/mit-license.php
13
 */
14
class ArrayResponse extends \ArrayObject implements ResponseInterface
15
{
16
    private $name;
17
18
    /**
19
     * @param string $name
20
     * @param array  $data
21
     */
22 46
    public function __construct(string $name, array $data)
23
    {
24 46
        $this->name = $name;
25 46
        parent::__construct($data);
26
    }
27
28 44
    public function getResponseName(): string
29
    {
30 44
        return $this->name;
31
    }
32
33
    /**
34
     * Object property access to ArrayObject data.
35
     */
36 4
    public function __get($property)
37
    {
38 4
        $key = $this->transformPropertyName($property);
39
40 4
        return $this[$key] ?? null;
41
    }
42
43
    /**
44
     * Object property access to ArrayObject data.
45
     */
46 1
    public function __isset($property)
47
    {
48 1
        $key = $this->transformPropertyName($property);
49
50 1
        return isset($this[$key]);
51
    }
52
53
    // ----------------------------------------
54
55
    /**
56
     * Tranform underscored property name to hyphenated array key.
57
     */
58 4
    private function transformPropertyName(string $propertyName): string
59
    {
60 4
        return str_replace('_', '-', $propertyName);
61
    }
62
}
63