Set price after cutting out all unwanted chars.

This method converts (almost) every string into a price.

@param mixed  $price
@param string $format [optional] the type of $price, default ConvertPrice::EURO

@return $this
@throws InvalidArgumentException
970491
public function setPrice($price, $format = self::EURO)
    {
        $this->validatePriceFormat($format);

        // sanitize: price is null if value like -1.123E-11 provided
        if (preg_match('/^\-?\d+\.\d+E(\+|\-)\d+$/u', (string) $price)) {
            $this->price = 0;

            return $this;
        }

        if ($format == self::EURO) {
            // remove: all not allowed chars
            $price = preg_replace('/[^0-9,\-\.\+]/', '', $price);

            // sanitize: +/- at end of number
            $price = preg_replace('/^(.*)(\-|\+)$/', '$2$1', $price);

            // sanitize: , in price
            if (mb_strpos($price, ',') !== false) {
                if (preg_match('/,\./', $price)) {
                    $price = str_replace(',', '', $price);
                } else {
                    $price = str_replace('.', '', $price);
                    $price = str_replace(',', '.', $price);
                }
            }

            // convert: to internal int structure
            $price = (float) $price;
            $price = $price * 100;
        } else {
            $price = (float) $price;
        }

        $this->price = (int) round($price);

        return $this;
    }
741964
public function formatPrice($price) {
    // Fetch the config.
    $config = $this->configFactory->get('acm.currency');

    // Get currency with the correct locale.
    $locale = \Drupal::languageManager()->getCurrentLanguage()->getId();
    $currency = \Drupal::service('repository.currency');
    $numberFormat = \Drupal::service('repository.number_format');
    $currencyFormatter = new CurrencyFormatter($numberFormat, $currency, ['locale' => $locale]);

    $currencyCode = $currency->get($config->get('currency_code'))->getCurrencyCode();

    return $currencyFormatter->format($price, $currencyCode);
  }
1163438
public static function fromString(string $format)
    {
        if (preg_match('/^(([^-]|--)+)-(([^-]|--)+)-(([^-]|--)+)\.(svg|png|gif|jpg)$/', $format, $match) === false && (7 != count($match))) {
            throw new InvalidArgumentException('The given format string is invalid: '.$format);
        }

        $subject = $match[1];
        $status = $match[3];
        $color = $match[5];
        $format = $match[7];

        return new self($subject, $status, $color, $format);
    }
1302865
public function setPrice($price)
    {
        if ($price instanceof Amount) {
            $price = $price->getNumber();
        }
        $this->price = $price;

        return $this;
    }
100916
protected function setFormat(FormatInterface $format, bool $remove = null): FormatterInterface
    {
        $name = $format->getName();
        $formats = $this->getFormats();
        if (array_key_exists($name, $formats) === false) {
            throw new FormatNotSupported($format);
        }

        $code = $formats[$name];
        $format = $this->getFormat();
        if ($remove === true) {
            $this->format = array_diff($format, [$code]);
        } else {
            $this->format = array_merge($format, [$code]);
        }

        return $this;
    }
1296812
public function setFormat($format) {

        $this->checkInArray($format, GDAXConstants::$reportFormatValues);
        $this->format = $format;
        return $this;

    }
994203
public function format(string $format) : self
    {
        if (! in_array($format, self::VALID_FORMATS)) {
            throw new Exception('Invalid format provided to path.');
        }

        $this->format = $format;

        return $this;
    }
462599
public function findPriceByCurrency($currency): ?PriceInterface
    {
        if ($currency instanceof \Money\Currency) {
            $currency = $currency->getCode();
        } elseif ($currency instanceof CurrencyInterface) {
            $currency = $currency->getIsoCodeAlpha();
        }

        if (!is_string($currency)) {
            throw new \InvalidArgumentException('$currency has to be a string');
        }

        foreach ($this->prices as $price) {
            if ($price->getCurrency()->getIsoCodeAlpha() === $currency) {
                return $price;
            }
        }

        return null;
    }
1350928
public function setFormat($format)
    {
        if($format)
        {
            if(!isset(static::$_formats[$format])) {
                throw new UnexpectedValueException('Unregistered format: "' . $format . '" given.');
            }

            $this->setContentType(static::$_formats[$format][0]);
        }

        return $this;
    }
356630
public function format($format)
    {
        if(Format::exist($format))
            $this->format = Format::get($format);
        else
            Format::invalidType();

        return $this;
    }
1370889
public function formatPrice($price, $currency, $locale = null)
    {
        return $this->priceManager->getFormattedPrice($price, $currency, $locale);
    }
13221
public function setFormat($value = null)
    {
        if ($value != null) {
            $this->_params['format'] = $value;
        } else {
            unset($this->_params['format']);
        }
        return $this;
    }
1185995
public function setFormat(FormatInterface $format)
    {
        $this->formatId = $format->getName();

        return parent::setFormat($format);
    }
173372
public function registerFormat($format, $class)
    {
        if(!class_exists($class, true)) {
            throw new \InvalidArgumentException('Class : '.$class.' cannot does not exist.');
        }

        $this->_formats[$format] = $class;

        //In case the format is being re-registered clear the prototype
        if(isset($this->__prototypes[$format])) {
            unset($this->__prototypes[$format]);
        }

        return $this;
    }
632319
public function setFormat($format)
    {
        if (in_array($format, TextFormat::asArray()) === true) {
            $this->format = $format;
        } else {
            $msg = "The 'format' argument must be a value from the TextFormat enumeration, '" . gettype($format) . "' given.";
            throw new InvalidArgumentException($msg);
        }
    }
738631
public function setCharset($charset)
    {
        if (is_string($charset)) {
            if (in_array($charset, mb_list_encodings())) {
                $this->charset = $charset;
            } else {
                throw new \InvalidArgumentException(
                    "Invalid value '" . $charset . "' for argument 'charset' given. Charset not supported."
                );
            }
        } else {
            throw new \InvalidArgumentException(
                "Invalid type '" . gettype($charset). "' for argument 'charset' given."
            );
        }

        return $this;
    }
1322307
public function setFormat($format)
    {
        if (!is_null($format)) {
            $this->format = $format;
            $this->useFormat(false);
        }

        return $this;
    }