Giter Club home page Giter Club logo

Comments (13)

mattias-persson avatar mattias-persson commented on September 26, 2024 2

Awesome! Hopefully I can start working on a PR for it this weekend 👍

from laravel-shopr.

Reached avatar Reached commented on September 26, 2024 1

Yea, will definitely do that, will report back once I get to it 👍

I like the API!

from laravel-shopr.

mattias-persson avatar mattias-persson commented on September 26, 2024 1

@Reached Thanks for taking the time to test it out! It really helps. I just have to solve a few problems that came up when running the test suite in Travis, then I'll merge this 👍

from laravel-shopr.

mattias-persson avatar mattias-persson commented on September 26, 2024

@Reached Ah thanks so much, that makes me super happy to hear! ❤️

I think this is a great idea, and I've been meaning to make the money formatting a bit more customizable. I'd love to give as much flexibility as possible without bloating the config file though.

What about providing your own MoneyFormatter-class? Something like this:

'currency' => 'DKK',

// The default formatter which you could switch to your own.
'money_formatter' => Happypixels\Shopr\Money\Formatter::class,

And then in the formatter class we could make it super easy to provide whatever details you want and just skip the ones you don't want to modify:

class CustomFormatter extends Happypixels\Shopr\Money\Formatter
{
    // Define the options you want to change. Either like this, or with single line methods. Not sure which approach is the best.
    public static $symbol = 'kr';
    public static $symbolBefore = '$'; // This would be weird, I know. But possible. ('$213kr')
    public static $decimalMark = ','; 

    // We could even allow formatting the amount without any restrictions.
    // Defining this method on your own formatter would be completely optional, your other settings 
    // would still be used when formatting amounts.
    public function format($amount)
    {
        if($amount > 1000){
            return 'Too much money, can\'t handle it.';
        }

        return $amount.' kr';
    }
}

This would create a setup similar to the Nova-resources. What do you think? Would this cover your needs and feel manageable?

from laravel-shopr.

Reached avatar Reached commented on September 26, 2024

I think that is a great solution, extendable and doesn't bloat the config as you pointed out 👌 !

from laravel-shopr.

Reached avatar Reached commented on September 26, 2024

Awesome! Let me know if I can help! :)

from laravel-shopr.

mattias-persson avatar mattias-persson commented on September 26, 2024

@Reached Would you be able to test this out before I merge it? I believe I've tested it pretty well but always good to have another pair of eyes on it.
Using the feature/custom-money-formatter branch, you can now add your own formatter and enable it using the 'money_formatter'-key in the config as we said.
You can either customize 5 different options like this:

<?php

namespace App;

use Happypixels\Shopr\Money\Formatter;

class CustomFormatter extends Formatter
{
    public $symbol = ' kr';
    public $symbolBefore = null; // You don't even need this in your class if you don't want to modify it.
    public $thousandSeparator = '.';
    public $decimalCount = 2;
    public $decimalSeparator = ',';
}

Or format the amount freely using the format-method:

<?php

namespace App;

use Happypixels\Shopr\Money\Formatter;

class CustomFormatter extends Formatter
{
    public function format($amount)
    {
        return $amount.' kr';
    }
}

Let me know if you have any questions!

from laravel-shopr.

Reached avatar Reached commented on September 26, 2024

Works great when I just return the amount from the format function, but when using the public properties it seems that setting the decimal count to 0 does not remove the decimals, however setting it to 1 does indeed remove one decimal.

Also it seems that there is no obvious way to move the symbol to after the amount without using the public function format($amount) it seems? :)

from laravel-shopr.

mattias-persson avatar mattias-persson commented on September 26, 2024

Ah, I'll take a look on the decimal issue.

Well, I believe the NumberFormatter class automatically puts the symbol on the "correct side" of the amount, depending on your currency code and app locale. Maybe we should add $symbolAfter, and force that to be after like we do with $symbolBefore. And if you want to let PHP decide which side the symbol should be on, you could just use the $symbol property. Would that make sense?

Thanks for your report!

from laravel-shopr.

mattias-persson avatar mattias-persson commented on September 26, 2024

Actually, how about just a $symbol and a $symbolPosition? Feels like that would make the API a bit less confusing.

from laravel-shopr.

mattias-persson avatar mattias-persson commented on September 26, 2024

Ok @Reached, I fixed the decimal issue, removed $symbolBefore and replaced it with $symbolPosition. Set this to either 'before' or 'after' to modify it. If left blank, PHP will auto-position the symbol depending on your currency code and app locale. Let me know if it's working as you'd expect!

from laravel-shopr.

Reached avatar Reached commented on September 26, 2024

Finally got around to test it, and it seems to work perfectly! Good job :) 👍

from laravel-shopr.

Reached avatar Reached commented on September 26, 2024

No problem!

from laravel-shopr.

Related Issues (15)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.