Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Float should not parse to string but to a custom float object #13

Open
mathiasverraes opened this issue Sep 27, 2020 · 1 comment
Open

Comments

@mathiasverraes
Copy link
Collaborator

No description provided.

@mallardduck
Copy link
Contributor

@mathiasverraes - Wondering what your thoughts were on this one? I'd be happy to contribute a PR for this feature and update tests too. Doing a quick implementation of things while it can work, one short coming I found is mapping a custom object to a float becomes harder.

Currently floatval can be used with map, but with a custom object that callback breaks. Seems like a short coming of PHP as it doesn't provide a Floatable contract similar to Stringable. Certainly not something I've considered until now, but it would be pretty cool if PHP added similar *able interfaces for other primitives. Seems like a neat half-way between not having them (now) and past suggestions for class like primitives that failed.

Anyway, like I said I'm really interested in supporting parisca and helping out here. So I'm curious if you have some more input here. This would be the gist of the concept I have so far:

class FloatObject implements \Stringable
{
    /**
     * @var float
     */
    private float $value;

    /**
     * @param float $value
     */
    private function __construct(float $value)
    {
        $this->value = $value;
    }

    /**
     * @param string|int|float $value
     *
     * @return FloatObject
     */
    public static function make($value): FloatObject
    {
        if (false === filter_var($value, FILTER_VALIDATE_FLOAT)) {
            throw new \RuntimeException("The input value cannot be properly parsed to a float.");
        }
        return new static((float) $value);
    }


    public function toFloat(): float
    {
        return $this->value;
    }

    public function toInteger(): int
    {
        return (int) round($this->value);
    }

    public function __toString(): string
    {
        return (string) $this->value;
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants