|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of michaelbutler/phposh. |
|
5
|
|
|
* Source: https://github.com/michaelbutler/phposh |
|
6
|
|
|
* |
|
7
|
|
|
* (c) Michael Butler <[email protected]> |
|
8
|
|
|
* |
|
9
|
|
|
* This source file is subject to the MIT license that is bundled |
|
10
|
|
|
* with this source code in the file named LICENSE. |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace PHPosh\Provider\Poshmark; |
|
14
|
|
|
|
|
15
|
|
|
class Price |
|
16
|
|
|
{ |
|
17
|
|
|
/** @var array<string, string> Currency symbol to 3 char code map */ |
|
18
|
|
|
public const SYMBOL_MAP = [ |
|
19
|
|
|
'$' => 'USD', |
|
20
|
|
|
'£' => 'GBP', |
|
21
|
|
|
'€' => 'EUR', |
|
22
|
|
|
]; |
|
23
|
|
|
/** @var string Decimal amount, such as "4.95" */ |
|
24
|
|
|
private $amount = '0.00'; |
|
25
|
|
|
|
|
26
|
|
|
/** @var string Three char currency code, such as "USD" */ |
|
27
|
|
|
private $currency_code = 'USD'; |
|
28
|
|
|
|
|
29
|
3 |
|
public function __toString() |
|
30
|
|
|
{ |
|
31
|
3 |
|
return '' . $this->getCurrencySymbol() . $this->getAmount(); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @param null|float|string $priceString |
|
36
|
|
|
*/ |
|
37
|
3 |
|
public static function fromString($priceString): Price |
|
38
|
|
|
{ |
|
39
|
3 |
|
$priceString = trim(((string) $priceString) ?: '$0.00'); |
|
40
|
3 |
|
$price = new self(); |
|
41
|
3 |
|
$matches = []; |
|
42
|
|
|
// match number and symbol, "$123.23" or "$123" |
|
43
|
3 |
|
$result = preg_match('/([$£€]) *([0-9]+\.[0-9]+|[0-9]+)/u', $priceString, $matches); |
|
44
|
3 |
|
if ($result) { |
|
45
|
1 |
|
$value = $matches[2] ?? '0.00'; |
|
46
|
1 |
|
$price->setAmount($value); |
|
47
|
1 |
|
$symbol = $matches[1] ?? '$'; |
|
48
|
1 |
|
$code = self::SYMBOL_MAP[$symbol] ?? 'USD'; |
|
49
|
|
|
|
|
50
|
1 |
|
return $price->setCurrencyCode($code); |
|
51
|
|
|
} |
|
52
|
|
|
// Match second format type, "123.12 USD" or "123 USD" |
|
53
|
2 |
|
$result = preg_match('/([0-9]+\.[0-9]+|[0-9]+) ?([A-Za-z]{3})/u', $priceString, $matches); |
|
54
|
2 |
|
if ($result) { |
|
55
|
1 |
|
$price->setAmount($matches[1] ?? '0.00'); |
|
56
|
1 |
|
$price->setCurrencyCode($matches[2] ?? 'USD'); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
2 |
|
return $price; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
3 |
|
public function getAmount(): string |
|
63
|
|
|
{ |
|
64
|
3 |
|
return number_format($this->amount, 2, '.', ''); |
|
|
|
|
|
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
2 |
|
public function setAmount(string $amount): Price |
|
68
|
|
|
{ |
|
69
|
2 |
|
$this->amount = $amount; |
|
70
|
|
|
|
|
71
|
2 |
|
return $this; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
3 |
|
public function getCurrencyCode(): string |
|
75
|
|
|
{ |
|
76
|
3 |
|
return $this->currency_code; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
2 |
|
public function setCurrencyCode(string $currency_code): Price |
|
80
|
|
|
{ |
|
81
|
2 |
|
$this->currency_code = $currency_code; |
|
82
|
|
|
|
|
83
|
2 |
|
return $this; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
3 |
|
public function getCurrencySymbol(): string |
|
87
|
|
|
{ |
|
88
|
3 |
|
$map = array_flip(self::SYMBOL_MAP); |
|
89
|
|
|
|
|
90
|
3 |
|
return $map[$this->getCurrencyCode()] ?? '$'; |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|