Completed
Pull Request — master (#18)
by Stéphane
66:12 queued 61:24
created

Configuration::arrayValue()   B

Complexity

Conditions 6
Paths 7

Size

Total Lines 19
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 19
rs 8.8571
cc 6
eloc 15
nc 7
nop 3
1
<?php
2
/**
3
 * This file is part of the bee4/transport package.
4
 * For the full copyright and license information, please view the LICENSE
5
 * file that was distributed with this source code.
6
 *
7
 * @copyright Bee4 2015
8
 * @author  Stephane HULARD <[email protected]>
9
 * @package Bee4\Transport\Configuration
10
 */
11
12
namespace Bee4\Transport\Configuration;
13
14
use Bee4\Transport\Collection;
15
16
/**
17
 * Configuration implementation
18
 * @package Bee4\Transport\Configuration
19
 */
20
class Configuration extends Collection
21
{
22
    /**
23
     * Default configuration values
24
     */
25
    const DEFAULTS = [
26
        'connect_timeout' => 0,
27
        'timeout' => 30,
28
        'verify' => false,
29
30
        'method' => 'GET',
31
        'url' => null,
32
        'body' => null,
33
        'upload' => false
34
    ];
35
36
    /**
37
     * Build a configuration instance
38
     * @param array $data
39
     */
40
    public function __construct(array $data = [])
41
    {
42
        parent::__construct(array_merge(
43
            self::DEFAULTS,
44
            $data
45
        ));
46
    }
47
48
    /**
49
     * Check if has body
50
     * @return boolean
51
     */
52
    public function hasBody()
53
    {
54
        return $this['body']!==false;
55
    }
56
57
    /**
58
     * Retrieve configuration property value
59
     * @param  string $name Property name
60
     * @return mixed
61
     */
62
    public function __get($name)
63
    {
64
        if (!$this->offsetExists($name)) {
65
            throw new \InvalidArgumentException('Invalid configuration key given !');
66
        }
67
68
        return $this[$name];
69
    }
70
71
    /**
72
     * Set a configuration value
73
     * @param string $name  Property name
74
     * @param mixed  $value Value to be set
75
     */
76
    public function __set($name, $value)
77
    {
78
        if (!$this->offsetExists($name) || is_array($this[$name])) {
79
            throw new \InvalidArgumentException('Invalid configuration key given !');
80
        }
81
82
        $this[$name] = $value;
83
    }
84
85
    /**
86
     * Allow to set an array value in the collection
87
     * @param  string $key      Key to retrieve the array in the collection
88
     * @param  string $arrayKey Array key to set
89
     * @param  mixed  $value    Value to be set or null
90
     * @return mixed
91
     */
92
    protected function arrayValue($key, $arrayKey, $value = null)
93
    {
94
        $array = $this[$key];
95
        $is = is_array($array);
96
        if (null !== $value) {
97
            if (!$is) {
98
                $array = array_key_exists($key, self::DEFAULTS)
99
                    ?self::DEFAULTS[$key]
100
                    :$this::DEFAULTS[$key];
101
            }
102
            $array[$arrayKey] = $value;
103
            $this[$key] = $array;
104
            return null;
105
        } else {
106
            return $is && isset($array[$arrayKey])
107
                ?$array[$arrayKey]
108
                :null;
109
        }
110
    }
111
}
112