Completed
Pull Request — develop (#28)
by Michael
04:20
created

Hash   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 7
Bugs 2 Features 0
Metric Value
wmc 7
c 7
b 2
f 0
lcom 1
cbo 3
dl 0
loc 54
ccs 18
cts 18
cp 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A fromConfig() 0 8 3
A __toString() 0 4 1
A jsonSerialize() 0 4 1
A setDefaultOptions() 0 6 1
1
<?php
2
3
namespace Crummy\Phlack\Common;
4
5
use Crummy\Phlack\Common\Exception\LogicException;
6
use Guzzle\Common\Collection as GuzzleCollection;
7
8
class Hash extends GuzzleCollection implements Encodable
9
{
10
    protected $required = [];
11
    protected $defaults = [];
12
    protected $optional = [];
13
14
    /**
15
     * @param array $data
16
     */
17 76
    public function __construct($data = [])
18
    {
19 76
        $resolver = new OptionsResolver();
20 76
        $this->setDefaultOptions($resolver);
21 76
        parent::__construct($resolver->resolve($data));
22 74
    }
23
24
    /**
25
     * {@inheritdoc}
26
     */
27 7
    public static function fromConfig(array $config = [], array $defaults = [], array $required = [])
28
    {
29 7
        if (!empty($defaults) || !empty($required)) {
30 2
            throw new LogicException('Setting options on fromConfig is not allowed.');
31
        }
32
33 5
        return new static($config);
34
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39 3
    public function __toString()
40
    {
41 3
        return json_encode($this);
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47 4
    public function jsonSerialize()
48
    {
49 4
        return $this->toArray();
50
    }
51
52
    /**
53
     * @param OptionsResolver $resolver
54
     */
55 76
    protected function setDefaultOptions(OptionsResolver $resolver)
56
    {
57 76
        $resolver->setRequired($this->required);
58 76
        $resolver->setDefined($this->optional);
59 76
        $resolver->setDefaults($this->defaults);
60 76
    }
61
}
62