Completed
Push — master ( 87f113...1d200b )
by Milan
03:04
created

Formats::setDefaultFormat()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 3
nop 1
1
<?php
2
3
namespace h4kuna\Exchange\Currency;
4
5
use h4kuna\Exchange,
6
	h4kuna\Number;
7
8
class Formats
9
{
10
	/** @var Number\NumberFormatFactory */
11
	private $numberFormatFactory;
12
13
	/** @var Number\UnitFormatState[] */
14
	private $formats;
15
16
	/** @var array */
17
	private $rawFormats;
18
19
	/** @var Number\UnitFormatState */
20
	private $default;
21
22
	public function __construct(Number\NumberFormatFactory $numberFormatFactory)
23
	{
24
		$this->numberFormatFactory = $numberFormatFactory;
25
	}
26
27
	public function setDefaultFormat($setup)
28
	{
29
		if (is_array($setup)) {
30
			$setup = $this->numberFormatFactory->createUnit($setup);
0 ignored issues
show
Documentation introduced by
$setup is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
31
		} elseif (!$setup instanceof Number\UnitFormatState) {
32
			throw new Exchange\InvalidArgumentException('$setup must be array or ' . Number\UnitPersistentFormatState::class);
33
		}
34
35
		$this->default = $setup;
36
	}
37
38
	public function addFormat($code, array $setup)
39
	{
40
		$code = strtoupper($code);
41
		$this->rawFormats[$code] = $setup;
42
		unset($this->formats[$code]);
43
	}
44
45
	public function getFormat($code)
46
	{
47
		if (isset($this->formats[$code])) {
48
			return $this->formats[$code];
49
		} elseif (isset($this->rawFormats[$code])) {
50
			if (isset($this->rawFormats[$code]['unit'])) {
51
				$format = $this->numberFormatFactory->createUnitPersistent(NULL, $this->rawFormats[$code]);
52
			} else {
53
				$format = $this->numberFormatFactory->createUnit($this->rawFormats[$code]);
54
			}
55
			$this->formats[$code] = $format;
56
			unset($this->rawFormats[$code]);
57
		} else {
58
			$this->formats[$code] = $this->getDefaultFormat();
59
		}
60
		return $this->formats[$code];
61
	}
62
63
	private function getDefaultFormat()
64
	{
65
		if ($this->default === NULL) {
66
			$this->default = $this->numberFormatFactory->createUnit();
67
		}
68
		return $this->default;
69
	}
70
71
}