Completed
Push — master ( 1492b1...144a66 )
by Florian
11s
created

CurrencyPair::toHash()   A

Complexity

Conditions 1
Paths 1

Duplication

Lines 0
Ratio 0 %

Size

Total Lines 4
Code Lines 2

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 1
eloc 2
c 1
b 1
f 0
nc 1
nop 0
dl 0
loc 4
rs 10
1
<?php
2
3
/*
4
 * This file is part of Swap.
5
 *
6
 * (c) Florian Voutzinos <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Swap\Model;
13
14
/**
15
 * Represents a currency pair.
16
 *
17
 * @author Florian Voutzinos <[email protected]>
18
 */
19
final class CurrencyPair
20
{
21
    private $baseCurrency;
22
    private $quoteCurrency;
23
24
    /**
25
     * Creates a new currency pair.
26
     *
27
     * @param string $baseCurrency  The base currency ISO 4217 code
28
     * @param string $quoteCurrency The quote currency ISO 4217 code
29
     */
30
    public function __construct($baseCurrency, $quoteCurrency)
31
    {
32
        $this->baseCurrency = $baseCurrency;
33
        $this->quoteCurrency = $quoteCurrency;
34
    }
35
36
    /**
37
     * Creates a currency pair from a string.
38
     *
39
     * @param string $string A string in the form EUR/USD
40
     *
41
     * @throws \InvalidArgumentException
42
     *
43
     * @return CurrencyPair
44
     */
45
    public static function createFromString($string)
46
    {
47
        $parts = explode('/', $string);
48
49
        if (!isset($parts[0]) || 3 !== strlen($parts[0]) || !isset($parts[1]) || 3 !== strlen($parts[1])) {
50
            throw new \InvalidArgumentException('The currency pair must be in the form "EUR/USD".');
51
        }
52
53
        return new self($parts[0], $parts[1]);
54
    }
55
56
    /**
57
     * Gets the base currency.
58
     *
59
     * @return string
60
     */
61
    public function getBaseCurrency()
62
    {
63
        return $this->baseCurrency;
64
    }
65
66
    /**
67
     * Gets the quote currency.
68
     *
69
     * @return string
70
     */
71
    public function getQuoteCurrency()
72
    {
73
        return $this->quoteCurrency;
74
    }
75
76
    /**
77
     * Check if the pair is identical.
78
     *
79
     * @return bool
80
     */
81
    public function isIdentical()
82
    {
83
        return $this->baseCurrency === $this->quoteCurrency;
84
    }
85
86
    /**
87
     * Returns a string representation of the pair.
88
     *
89
     * @return string
90
     */
91
    public function toString()
92
    {
93
        return sprintf('%s/%s', $this->baseCurrency, $this->quoteCurrency);
94
    }
95
96
    /**
97
     * Returns the hashed representation of the pair.
98
     *
99
     * @return string
100
     */
101
    public function toHash()
102
    {
103
        return md5($this->toString());
104
    }
105
106
    /**
107
     * Returns a string representation of the pair.
108
     *
109
     * @return string
110
     */
111
    public function __toString()
112
    {
113
        return $this->toString();
114
    }
115
}
116