public static function parseApplePriceMatrix($dom, $currency, $directory = null)
    {
        if (is_string($dom)) {
            if (file_exists($dom) && is_file($dom)) {
                $file = $dom;
                $dom = new \DOMDocument();
                $dom->loadHTMLFile($file);
                unset ($file);
            } else {
                $content = $dom;
                $dom = new \DOMDocument();
                $dom->loadHTML($content);
                unset ($content);
            }
        }

        if (!$dom instanceof \DOMDocument) {
            throw new \InvalidArgumentException(sprintf(
                'The first argument must be a DOMDocument instance or path to ApplePriceMatrix file, "%s" given.',
                is_object($dom) ? get_class($dom) : gettype($dom)
            ));
        }

        $currency = strtoupper($currency);

        // Create XPath for search in DOM
        $xpath = new \DOMXPath($dom);

        $tierPrimary = static::parseApplePriceMatrixTier($xpath, 1, $currency);
        $tierAlternative = static::parseApplePriceMatrixTier($xpath, 2, $currency);

        $prices = $tierPrimary + $tierAlternative;
        ksort($prices);

        if ($directory) {
            if (!is_writable($directory)) {
                throw new \RuntimeException(sprintf(
                    'Could not write prices map to directory "%s". Directory is not writable.',
                    $directory
                ));
            }

            $file = $directory . '/' . $currency . '.php';

            file_put_contents($file, '<?php return ' . var_export($prices, 1) . ';');
        }

        return $prices;
    }