Completed
Pull Request — develop (#33)
by Michael
02:17
created

Hash::setDefaultOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 4
Bugs 1 Features 0
Metric Value
c 4
b 1
f 0
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
crap 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 75
    public function __construct($data = [])
18
    {
19 75
        $resolver = new OptionsResolver();
20 75
        $this->setDefaultOptions($resolver);
21 75
        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 75
    protected function setDefaultOptions(OptionsResolver $resolver)
56
    {
57 75
        $resolver->setRequired($this->required);
58 75
        $resolver->setDefined($this->optional);
59 75
        $resolver->setDefaults($this->defaults);
60 75
    }
61
}
62