diff --git a/src/Illuminate/Support/Numberable.php b/src/Illuminate/Support/Numberable.php new file mode 100644 index 000000000000..02f087580433 --- /dev/null +++ b/src/Illuminate/Support/Numberable.php @@ -0,0 +1,61 @@ +value = $value; + } + + /** + * Get the raw numeric value. + * + * @return int|float + */ + public function value() + { + return $this->value; + } + + /** + * Proxy dynamic properties onto methods. + * + * @param string $key + * @return mixed + */ + public function __get($key) + { + return $this->{$key}(); + } + + /** + * Get the raw string value. + * + * @return string + */ + public function __toString() + { + return (string) $this->value; + } +} diff --git a/tests/Support/SupportNumberableTest.php b/tests/Support/SupportNumberableTest.php new file mode 100644 index 000000000000..f51eae8071b2 --- /dev/null +++ b/tests/Support/SupportNumberableTest.php @@ -0,0 +1,39 @@ +assertSame(0, $this->numberable()->value()); + $this->assertSame(1, $this->numberable(1)->value()); + $this->assertSame(1.1, $this->numberable(1.1)->value()); + } + + public function testMagicGet() + { + $this->assertSame(0, $this->numberable()->value); + $this->assertSame(1, $this->numberable(1)->value); + $this->assertSame(1.1, $this->numberable(1.1)->value); + } + + public function testStringCast() + { + $this->assertSame('0', (string) $this->numberable()); + $this->assertSame('1', (string) $this->numberable(1)); + $this->assertSame('1.1', (string) $this->numberable(1.1)); + } +}