<?php

/**
 * Smarty plugin.
 */

/**
 * Smarty {format_price} function plugin.
 *
 * @param int|array $price  integer or array containing price
 * @param string    $params String describing
 *                          parameters and default values:
 *                          "withVat" => true,
 *                          "ceil" => "default",
 *                          "format" => true,
 *                          "printcurrency" => true,
 *                          "printdealerdiscount" => false,
 *                          "dealerdiscount" => true,
 *
 * @return int|string
 */
function smarty_modifier_format_price($price, $params = '')
{
    if ($price instanceof \KupShop\KupShopBundle\Wrapper\PriceWrapper) {
        $price = $price->getObject();
    }

    // TODO remove: Fix float formated using czech locale
    if (is_string($price) && strpos($price, ',') !== false) {
        $price = str_replace(',', '.', $price);
    }

    $cond = [];
    $part = preg_split('/;|,/', $params);
    for ($i = 0; $i < count($part); $i++) {
        $pair = explode('=', $part[$i]);
        if ($pair[0] == '') {
            continue;
        }

        if ($pair[1] == 'true' || $pair[1] == 'yes') {
            $cond[$pair[0]] = true;
        } elseif ($pair[1] == 'false' || $pair[1] == 'no') {
            $cond[$pair[0]] = false;
        } else {
            $cond[$pair[0]] = (string) $pair[1];
        }
    }

    return printPrice($price, $cond);
}
