Skip to content
Browse files

Implemented string formatting system (fixes #13, started on #14)

  • Loading branch information...
1 parent 22d61b8 commit 58a76c03ea2bc23e8fa1b42b63c0e4239f592e4e @jmalloc jmalloc committed Jan 22, 2013
View
3 composer.json
@@ -17,7 +17,8 @@
"require-dev": {
"icecave/testing": "2.*",
"icecave/woodhouse": "*",
- "eloquent/typhoon": "0.7.*"
+ "eloquent/typhoon": "0.7.*",
+ "eloquent/liberator": "1.*"
},
"autoload": {
"psr-0": {
View
93 composer.lock
@@ -1,5 +1,5 @@
{
- "hash": "7c5c8b6723a0d9c3dbe24b072c49a72c",
+ "hash": "b87025ef2118f4afe6944024a7621cf6",
"packages": [
],
@@ -255,6 +255,97 @@
]
},
{
+ "name": "eloquent/liberator",
+ "version": "1.1.0",
+ "source": {
+ "type": "git",
+ "url": "https://github.com/eloquent/liberator.git",
+ "reference": "1.1.0"
+ },
+ "dist": {
+ "type": "zip",
+ "url": "https://github.com/eloquent/liberator/zipball/1.1.0",
+ "reference": "1.1.0",
+ "shasum": ""
+ },
+ "require": {
+ "php": ">=5.3.0",
+ "eloquent/pops": "3.*"
+ },
+ "time": "2012-08-02 06:30:51",
+ "type": "library",
+ "autoload": {
+ "psr-0": {
+ "Eloquent\\Liberator": "src"
+ }
+ },
+ "notification-url": "http://packagist.org/downloads/",
+ "license": [
+ "MIT"
+ ],
+ "authors": [
+ {
+ "name": "Erin Millard",
+ "email": "ezzatron@gmail.com",
+ "homepage": "http://ezzatron.com/"
+ }
+ ],
+ "description": "A proxy for circumventing PHP access modifier restrictions.",
+ "homepage": "https://github.com/eloquent/liberator",
+ "keywords": [
+ "reflection",
+ "object",
+ "proxy",
+ "access",
+ "modifier",
+ "private",
+ "protected"
+ ]
+ },
+ {
+ "name": "eloquent/pops",
+ "version": "3.1.0",
+ "source": {
+ "type": "git",
+ "url": "git://github.com/eloquent/pops.git",
+ "reference": "3.1.0"
+ },
+ "dist": {
+ "type": "zip",
+ "url": "https://github.com/eloquent/pops/zipball/3.1.0",
+ "reference": "3.1.0",
+ "shasum": ""
+ },
+ "require": {
+ "php": ">=5.3.0"
+ },
+ "time": "2012-08-02 06:17:58",
+ "type": "library",
+ "autoload": {
+ "psr-0": {
+ "Eloquent\\Pops": "src"
+ }
+ },
+ "notification-url": "http://packagist.org/downloads/",
+ "license": [
+ "MIT"
+ ],
+ "authors": [
+ {
+ "name": "Erin Millard",
+ "email": "ezzatron@gmail.com",
+ "homepage": "http://ezzatron.com/"
+ }
+ ],
+ "description": "PHP object proxy system.",
+ "homepage": "https://github.com/eloquent/pops",
+ "keywords": [
+ "object",
+ "proxy",
+ "escaping"
+ ]
+ },
+ {
"name": "eloquent/typhax",
"version": "0.8.2",
"source": {
View
4 lib/Icecave/Chrono/CommonInterface.php
@@ -1,10 +1,12 @@
<?php
namespace Icecave\Chrono;
+use Icecave\Chrono\Format\FormattableInterface;
+
/**
* Base interface for all measures of dates and/or times.
*/
-interface CommonInterface extends Iso8601Interface
+interface CommonInterface extends Iso8601Interface, FormattableInterface
{
/**
* Convert this date/time to a different timezone.
View
17 lib/Icecave/Chrono/Date.php
@@ -1,6 +1,8 @@
<?php
namespace Icecave\Chrono;
+use Icecave\Chrono\Format\DefaultFormatter;
+use Icecave\Chrono\Format\FormatterInterface;
use Icecave\Chrono\Support\Normalizer;
use Icecave\Chrono\TypeCheck\TypeCheck;
@@ -189,6 +191,21 @@ public function unixTime()
}
/**
+ * @param string $formatSpecifier The format of the output string.
+ * @param FormatterInterface|null $formatter The formatter to use, or null to use the default.
+ *
+ * @return string The formatted string.
+ */
+ public function format($formatSpecifier, FormatterInterface $formatter = null)
+ {
+ if (null === $formatter) {
+ $formatter = DefaultFormatter::instance();
+ }
+
+ return $formatter->formatDate($this, $formatSpecifier);
+ }
+
+ /**
* @return string A string representing this object in an ISO compatible format (YYYY-MM-DD).
*/
public function isoString()
View
17 lib/Icecave/Chrono/DateTime.php
@@ -1,6 +1,8 @@
<?php
namespace Icecave\Chrono;
+use Icecave\Chrono\Format\DefaultFormatter;
+use Icecave\Chrono\Format\FormatterInterface;
use Icecave\Chrono\Support\Normalizer;
use Icecave\Chrono\TypeCheck\TypeCheck;
@@ -234,6 +236,21 @@ public function unixTime()
}
/**
+ * @param string $formatSpecifier The format of the output string.
+ * @param FormatterInterface|null $formatter The formatter to use, or null to use the default.
+ *
+ * @return string The formatted string.
+ */
+ public function format($formatSpecifier, FormatterInterface $formatter = null)
+ {
+ if (null === $formatter) {
+ $formatter = DefaultFormatter::instance();
+ }
+
+ return $formatter->formatDateTime($this, $formatSpecifier);
+ }
+
+ /**
* @return string A string representing this object in an ISO compatible format (YYYY-MM-DD).
*/
public function isoString()
View
71 lib/Icecave/Chrono/Format/DefaultFormatter.php
@@ -0,0 +1,71 @@
+<?php
+namespace Icecave\Chrono\Format;
+
+use Icecave\Chrono\Date;
+use Icecave\Chrono\Time;
+use Icecave\Chrono\DateTime;
+use Icecave\Chrono\TimeZone;
+
+/**
+ * A string formatter that accets {@see date()} compatible format specifiers.
+ */
+class DefaultFormatter implements FormatterInterface
+{
+ /**
+ * @param Date $date The date to format.
+ * @param string $formatSpecifier The format of the output string.
+ *
+ * @return string The formatted date.
+ */
+ public function formatDate(Date $date, $formatSpecifier)
+ {
+ throw new \Exception('Not implemented.');
+ }
+
+ /**
+ * @param Time $Time The time to format.
+ * @param string $formatSpecifier The format of the output string.
+ *
+ * @return string The formatted time.
+ */
+ public function formatTime(Time $Time, $formatSpecifier)
+ {
+ throw new \Exception('Not implemented.');
+ }
+
+ /**
+ * @param DateTime $dateTime The date/time to format.
+ * @param string $formatSpecifier The format of the output string.
+ *
+ * @return string The formatted date/time.
+ */
+ public function formatDateTime(DateTime $dateTime, $formatSpecifier)
+ {
+ throw new \Exception('Not implemented.');
+ }
+
+ /**
+ * @param TimeZone $timeZone The timezone to format.
+ * @param string $formatSpecifier The format of the output string.
+ *
+ * @return string The formatted timezone.
+ */
+ public function formatTimeZone(TimeZone $timeZone, $formatSpecifier)
+ {
+ throw new \Exception('Not implemented.');
+ }
+
+ /**
+ * @return DefaultFormatter The global instance of the default formatter.
+ */
+ public static function instance()
+ {
+ if (null === self::$instance) {
+ self::$instance = new self;
+ }
+
+ return self::$instance;
+ }
+
+ private static $instance;
+}
View
16 lib/Icecave/Chrono/Format/FormattableInterface.php
@@ -0,0 +1,16 @@
+<?php
+namespace Icecave\Chrono\Format;
+
+/**
+ * Interface for objects that can be formatted by a formatter.
+ */
+interface FormattableInterface
+{
+ /**
+ * @param string $formatSpecifier The format of the output string.
+ * @param FormatterInterface|null $formatter The formatter to use, or null to use the default.
+ *
+ * @return string The formatted string.
+ */
+ public function format($formatSpecifier, FormatterInterface $formatter = null);
+}
View
45 lib/Icecave/Chrono/Format/FormatterInterface.php
@@ -0,0 +1,45 @@
+<?php
+namespace Icecave\Chrono\Format;
+
+use Icecave\Chrono\Date;
+use Icecave\Chrono\Time;
+use Icecave\Chrono\DateTime;
+use Icecave\Chrono\TimeZone;
+
+/**
+ * Base interface for string formatters.
+ */
+interface FormatterInterface
+{
+ /**
+ * @param Date $date The date to format.
+ * @param string $formatSpecifier The format of the output string.
+ *
+ * @return string The formatted date.
+ */
+ public function formatDate(Date $date, $formatSpecifier);
+
+ /**
+ * @param Time $Time The time to format.
+ * @param string $formatSpecifier The format of the output string.
+ *
+ * @return string The formatted time.
+ */
+ public function formatTime(Time $Time, $formatSpecifier);
+
+ /**
+ * @param DateTime $dateTime The date/time to format.
+ * @param string $formatSpecifier The format of the output string.
+ *
+ * @return string The formatted date/time.
+ */
+ public function formatDateTime(DateTime $dateTime, $formatSpecifier);
+
+ /**
+ * @param TimeZone $timeZone The timezone to format.
+ * @param string $formatSpecifier The format of the output string.
+ *
+ * @return string The formatted timezone.
+ */
+ public function formatTimeZone(TimeZone $timeZone, $formatSpecifier);
+}
View
17 lib/Icecave/Chrono/Time.php
@@ -1,6 +1,8 @@
<?php
namespace Icecave\Chrono;
+use Icecave\Chrono\Format\DefaultFormatter;
+use Icecave\Chrono\Format\FormatterInterface;
use Icecave\Chrono\Support\Normalizer;
use Icecave\Chrono\TypeCheck\TypeCheck;
@@ -158,6 +160,21 @@ public function compare(Time $time)
}
}
+ /**
+ * @param string $formatSpecifier The format of the output string.
+ * @param FormatterInterface|null $formatter The formatter to use, or null to use the default.
+ *
+ * @return string The formatted string.
+ */
+ public function format($formatSpecifier, FormatterInterface $formatter = null)
+ {
+ if (null === $formatter) {
+ $formatter = DefaultFormatter::instance();
+ }
+
+ return $formatter->formatTime($this, $formatSpecifier);
+ }
+
/**
* @return string A string representing this object in an ISO compatible format (HH:MM:SS[+-]HH:MM).
*/
View
20 lib/Icecave/Chrono/TimeZone.php
@@ -1,9 +1,12 @@
<?php
namespace Icecave\Chrono;
+use Icecave\Chrono\Format\DefaultFormatter;
+use Icecave\Chrono\Format\FormatterInterface;
+use Icecave\Chrono\Format\FormattableInterface;
use Icecave\Chrono\TypeCheck\TypeCheck;
-class TimeZone implements Iso8601Interface
+class TimeZone implements Iso8601Interface, FormattableInterface
{
/**
* @param integer $offset The offset from UTC in seconds.
@@ -64,6 +67,21 @@ public function compare(TimeZone $timeZone)
}
/**
+ * @param string $formatSpecifier The format of the output string.
+ * @param FormatterInterface|null $formatter The formatter to use, or null to use the default.
+ *
+ * @return string The formatted string.
+ */
+ public function format($formatSpecifier, FormatterInterface $formatter = null)
+ {
+ if (null === $formatter) {
+ $formatter = DefaultFormatter::instance();
+ }
+
+ return $formatter->formatTimeZone($this, $formatSpecifier);
+ }
+
+ /**
* @return string A string representing this object in an ISO compatible format ([+-]HH:MM).
*/
public function isoString()
View
20 test/suite/Icecave/Chrono/DateTest.php
@@ -1,6 +1,7 @@
<?php
namespace Icecave\Chrono;
+use Eloquent\Liberator\Liberator;
use Phake;
use PHPUnit_Framework_TestCase;
@@ -140,6 +141,25 @@ public function testUnixTimeWithTimeZone()
$this->assertSame(1359640800, $date->unixTime());
}
+ public function testFormat()
+ {
+ $formatter = Phake::mock(__NAMESPACE__ . '\Format\FormatterInterface');
+ Liberator::liberateClass(__NAMESPACE__ . '\Format\DefaultFormatter')->instance = $formatter;
+
+ Phake::when($formatter)
+ ->formatDate(Phake::anyParameters())
+ ->thenReturn('<1st>')
+ ->thenReturn('<2nd>');
+
+ $result = $this->_date->format('Y-m-d');
+ $this->assertSame('<1st>', $result);
+
+ $result = $this->_date->format('Y-m-d', $formatter);
+ $this->assertSame('<2nd>', $result);
+
+ Phake::verify($formatter, Phake::times(2))->formatDate($this->_date, 'Y-m-d');
+ }
+
public function testIsoString()
{
$this->assertEquals('2013-02-01', $this->_date->isoString());
View
20 test/suite/Icecave/Chrono/DateTimeTest.php
@@ -1,6 +1,7 @@
<?php
namespace Icecave\Chrono;
+use Eloquent\Liberator\Liberator;
use Phake;
use PHPUnit_Framework_TestCase;
@@ -169,6 +170,25 @@ public function testUnixTimeWithTimeZone()
$this->assertSame(1359678030, $dateTime->unixTime());
}
+ public function testFormat()
+ {
+ $formatter = Phake::mock(__NAMESPACE__ . '\Format\FormatterInterface');
+ Liberator::liberateClass(__NAMESPACE__ . '\Format\DefaultFormatter')->instance = $formatter;
+
+ Phake::when($formatter)
+ ->formatDateTime(Phake::anyParameters())
+ ->thenReturn('<1st>')
+ ->thenReturn('<2nd>');
+
+ $result = $this->_dateTime->format('Y-m-d H:i:s');
+ $this->assertSame('<1st>', $result);
+
+ $result = $this->_dateTime->format('Y-m-d H:i:s', $formatter);
+ $this->assertSame('<2nd>', $result);
+
+ Phake::verify($formatter, Phake::times(2))->formatDateTime($this->_dateTime, 'Y-m-d H:i:s');
+ }
+
public function testIsoString()
{
$this->assertEquals('2013-02-01 10:20:30+00:00', $this->_dateTime->isoString());
View
21 test/suite/Icecave/Chrono/TimeTest.php
@@ -1,6 +1,8 @@
<?php
namespace Icecave\Chrono;
+use Eloquent\Liberator\Liberator;
+use Phake;
use PHPUnit_Framework_TestCase;
class TimeTest extends PHPUnit_Framework_TestCase
@@ -116,6 +118,25 @@ public function testCompareTimeZone()
$this->assertGreaterThan(0, $time->compare($this->_time));
}
+ public function testFormat()
+ {
+ $formatter = Phake::mock(__NAMESPACE__ . '\Format\FormatterInterface');
+ Liberator::liberateClass(__NAMESPACE__ . '\Format\DefaultFormatter')->instance = $formatter;
+
+ Phake::when($formatter)
+ ->formatTime(Phake::anyParameters())
+ ->thenReturn('<1st>')
+ ->thenReturn('<2nd>');
+
+ $result = $this->_time->format('H:i:s');
+ $this->assertSame('<1st>', $result);
+
+ $result = $this->_time->format('H:i:s', $formatter);
+ $this->assertSame('<2nd>', $result);
+
+ Phake::verify($formatter, Phake::times(2))->formatTime($this->_time, 'H:i:s');
+ }
+
public function testIsoString()
{
$this->assertEquals('10:20:30+00:00', $this->_time->isoString());
View
21 test/suite/Icecave/Chrono/TimeZoneTest.php
@@ -1,6 +1,8 @@
<?php
namespace Icecave\Chrono;
+use Eloquent\Liberator\Liberator;
+use Phake;
use PHPUnit_Framework_TestCase;
class TimeZoneTest extends PHPUnit_Framework_TestCase
@@ -91,6 +93,25 @@ public function testCompareGreater()
$this->assertLessThan(0, $b->compare($a));
}
+ public function testFormat()
+ {
+ $formatter = Phake::mock(__NAMESPACE__ . '\Format\FormatterInterface');
+ Liberator::liberateClass(__NAMESPACE__ . '\Format\DefaultFormatter')->instance = $formatter;
+
+ Phake::when($formatter)
+ ->formatTimeZone(Phake::anyParameters())
+ ->thenReturn('<1st>')
+ ->thenReturn('<2nd>');
+
+ $result = $this->_timeZone->format('H:m');
+ $this->assertSame('<1st>', $result);
+
+ $result = $this->_timeZone->format('H:m', $formatter);
+ $this->assertSame('<2nd>', $result);
+
+ Phake::verify($formatter, Phake::times(2))->formatTimeZone($this->_timeZone, 'H:m');
+ }
+
public function testIsoString()
{
$this->assertEquals('+10:00', $this->_timeZone->isoString());

0 comments on commit 58a76c0

Please sign in to comment.
Something went wrong with that request. Please try again.