<commit_before><?php
namespace Rolice\LaravelCurrency\Http\Controllers;

use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\Lang;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Validator;
use Rolice\LaravelCurrency\Exceptions\Exception;
use Rolice\LaravelCurrency\Models\Currency;

class CurrencyController extends Controller
{

    public function index()
    {
        return Currency::all();
    }

    public function convert()
    {
        $validator = Validator::make(Input::all(), [
            'from' => 'required|min:3|max:3',
            'to' => 'required|min:3|max:3',
        ]);

        if ($validator->fails()) {
            return response()->json([$validator->errors()])->setStatusCode(422, 'Validation Failed');
        }

        $from = Currency::find(Input::get('from'));
        $to = Currency::find(Input::get('to'));
        $precision = (int) Config::get('laravel-currency.laravel-currency.precision');
        $multiplier = pow(10, (int) Config::get('laravel-currency.laravel-currency.precision'));
        $amount = (float)Input::get('amount') ?: 1;
        $amount = round($amount * $multiplier);

        if ($from && ($from->base == Input::get('to') || $from->base == $to->code)) {
            return response()->json(round(($amount / $from->price), $precision));
        }

        if ($to && ($to->base == Input::get('from') || $to->base == $from->code)) {
            return response()->json(round($amount * $to->price / pow($multiplier, 2), $precision));
        }

        if (!$from || !$to) {
            return response()->json(['error' => Lang::get('currency.error.not_found')])->setStatusCode(404, 'Currency Not Found');
        }

        if ($from->base !== $to->base) {
            throw new Exception('Cannot convert currencies with different bases.');
        }

        return response()->json(round(($amount / $from->price) * $to->price  / $multiplier, $precision));
    }<commit_msg>convert() : Return amount if from = to<commit_after><?php
namespace Rolice\LaravelCurrency\Http\Controllers;

use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\Lang;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Validator;
use Rolice\LaravelCurrency\Exceptions\Exception;
use Rolice\LaravelCurrency\Models\Currency;

class CurrencyController extends Controller
{

    public function index()
    {
        return Currency::all();
    }

    public function convert()
    {
        $validator = Validator::make(Input::all(), [
            'from' => 'required|min:3|max:3',
            'to' => 'required|min:3|max:3',
        ]);

        if ($validator->fails()) {
            return response()->json([$validator->errors()])->setStatusCode(422, 'Validation Failed');
        }

        $from = Currency::find(Input::get('from'));
        $to = Currency::find(Input::get('to'));
        $precision = (int) Config::get('laravel-currency.laravel-currency.precision');
        $multiplier = pow(10, (int) Config::get('laravel-currency.laravel-currency.precision'));
        $amount = (float)Input::get('amount') ?: 1;

        if ($from == $to) {
            return response()->json($amount);
        }

        $amount = round($amount * $multiplier);

        if ($from && ($from->base == Input::get('to') || $from->base == $to->code)) {
            return response()->json(round(($amount / $from->price), $precision));
        }

        if ($to && ($to->base == Input::get('from') || $to->base == $from->code)) {
            return response()->json(round($amount * $to->price / pow($multiplier, 2), $precision));
        }

        if (!$from || !$to) {
            return response()->json(['error' => Lang::get('currency.error.not_found')])->setStatusCode(404, 'Currency Not Found');
        }

        if ($from->base !== $to->base) {
            throw new Exception('Cannot convert currencies with different bases.');
        }

        return response()->json(round(($amount / $from->price) * $to->price  / $multiplier, $precision));
    }
{'fraction_non_alphanumeric': 0.18706081899685, 'fraction_numerical': 0.006784589290041192, 'mean_word_length': 3.0273170731707317, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 52, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2968883', 'n_tokens_mistral': 1225, 'n_tokens_neox': 1223, 'n_words': 222}