Completed
Pull Request — master (#237)
by Kristof
05:43
created

PriceDescriptionParser::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 2
1
<?php
2
/**
3
 * @file
4
 */
5
6
namespace CultuurNet\UDB3\Cdb;
7
8
use CommerceGuys\Intl\Currency\CurrencyRepositoryInterface;
9
use CommerceGuys\Intl\Formatter\NumberFormatter;
10
use CommerceGuys\Intl\NumberFormat\NumberFormatRepositoryInterface;
11
12
/**
13
 * Parses a cdbxml <pricedescription> string into name value pairs.
14
 */
15
class PriceDescriptionParser
16
{
17
    /**
18
     * @var NumberFormatRepositoryInterface
19
     */
20
    private $numberFormatRepository;
21
22
    /**
23
     * @var CurrencyRepositoryInterface
24
     */
25
    private $currencyRepository;
26
27
    public function __construct(
28
        NumberFormatRepositoryInterface $numberFormatRepository,
29
        CurrencyRepositoryInterface $currencyRepository
30
    ) {
31
        $this->numberFormatRepository = $numberFormatRepository;
32
        $this->currencyRepository = $currencyRepository;
33
    }
34
35
    /**
36
     * @param string $description
37
     *
38
     * @return array
39
     *   An array of price name value pairs.
40
     */
41
    public function parse($description)
42
    {
43
        $prices = array();
44
45
        $possiblePriceDescriptions = preg_split('/\s*;\s*/', $description);
46
47
        $namePattern = '[\w\s]+';
48
        $valuePattern = '\€?\s*[\d,]+\s*\€?';
49
50
        $pricePattern =
51
          "/(?<name>{$namePattern}):\s*(?<value>{$valuePattern})/u";
52
53
        $numberFormat = $this->numberFormatRepository->get('nl-BE');
54
        $currencyFormatter = new NumberFormatter(
55
            $numberFormat,
56
            NumberFormatter::CURRENCY
57
        );
58
        $currency = $this->currencyRepository->get('EUR');
59
60
        foreach ($possiblePriceDescriptions as $possiblePriceDescription) {
61
            $possiblePriceDescription = trim($possiblePriceDescription);
62
            $matches = [];
63
64
            $priceDescriptionIsValid = preg_match(
65
                $pricePattern,
66
                $possiblePriceDescription,
67
                $matches
68
            );
69
70
            if ($priceDescriptionIsValid) {
71
                $priceName = trim($matches['name']);
72
                $priceValue = trim($matches['value']);
73
74
                $priceValue = $currencyFormatter->parseCurrency(
75
                    $priceValue,
76
                    $currency
77
                );
78
79
                if (false === $priceValue) {
80
                    continue;
81
                }
82
83
                if (!isset($prices[$priceName])) {
84
                    $prices[$priceName] = floatval($priceValue);
85
                }
86
            }
87
        }
88
89
        return $prices;
90
    }
91
}
92