precision, $currency->decimal_separator, $currency->thousand_separator ); $symbol = ''.$currency->symbol.''; $currency_with_symbol = $currency->swap_currency_symbol ? $format_money.$symbol : $symbol.$format_money; // The sign is decided on the formatted digits, not on the raw input, so an // amount that rounds away at the currency's precision (a stray cent on a // zero-precision currency) renders as zero instead of "-0". $is_negative = $money < 0 && preg_match('/[1-9]/', $format_money) === 1; return $is_negative ? '-'.$currency_with_symbol : $currency_with_symbol; } /** * @param $string * @return string */ function clean_slug($model, $title, $id = 0) { // Normalize the title $slug = Str::upper('CUSTOM_'.$model.'_'.Str::slug($title, '_')); // Get any that could possibly be related. // This cuts the queries down by doing it once. $allSlugs = getRelatedSlugs($model, $slug, $id); // If we haven't used it before then we are all good. if (! $allSlugs->contains('slug', $slug)) { return $slug; } // Just append numbers like a savage until we find not used. for ($i = 1; $i <= 10; $i++) { $newSlug = $slug.'_'.$i; if (! $allSlugs->contains('slug', $newSlug)) { return $newSlug; } } throw new Exception('Can not create a unique slug'); } function getRelatedSlugs($type, $slug, $id = 0) { return CustomField::select('slug')->where('slug', 'like', $slug.'%') ->where('model_type', $type) ->where('id', '<>', $id) ->get(); } function respondJson($error, $message) { return response()->json([ 'error' => $error, 'message' => $message, ], 422); }