Page Menu
Home
DevCentral
Search
Configure Global Search
Log In
Files
F3915875
D1635.id4175.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
6 KB
Referenced Files
None
Subscribers
None
D1635.id4175.diff
View Options
diff --git a/src/Collections/ArrayUtilities.php b/src/Collections/ArrayUtilities.php
new file mode 100644
--- /dev/null
+++ b/src/Collections/ArrayUtilities.php
@@ -0,0 +1,33 @@
+<?php
+declare(strict_types=1);
+
+namespace Keruald\OmniTools\Collections;
+
+use Closure;
+
+class ArrayUtilities {
+
+ ///
+ /// Methods to transform every member of an array
+ ///
+
+ /**
+ * @return int[]
+ */
+ public static function toIntegers (array $array) : array {
+ $newArray = $array;
+ array_walk($newArray, self::toIntegerCallback());
+ return $newArray;
+ }
+
+ ///
+ /// Helpers to get callbacks for array_walk methods
+ ///
+
+ public static function toIntegerCallback () : Closure {
+ return function (&$item) {
+ $item = (int)$item;
+ };
+ }
+
+}
diff --git a/src/Collections/OmniArray.php b/src/Collections/OmniArray.php
new file mode 100644
--- /dev/null
+++ b/src/Collections/OmniArray.php
@@ -0,0 +1,55 @@
+<?php
+declare(strict_types=1);
+
+namespace Keruald\OmniTools\Collections;
+
+class OmniArray {
+
+ /**
+ * @var array
+ */
+ private $items = [];
+
+ ///
+ /// Constructors
+ ///
+
+ public function __construct (?iterable $items) {
+ if ($items === null) {
+ return;
+ }
+
+ if (is_array($items)) {
+ $this->items = $items;
+
+ return;
+ }
+
+ foreach ($items as $item) {
+ $this->items[] = $item;
+ }
+ }
+
+ public static function explode (string $delimiter, string $string, int $limit = PHP_INT_MAX) : self {
+ return new OmniArray(explode($delimiter, $string, $limit));
+ }
+
+ ///
+ /// Transformation methods
+ ///
+
+ public function toIntegers () : self {
+ array_walk($this->items, ArrayUtilities::toIntegerCallback());
+
+ return $this;
+ }
+
+ ///
+ /// Getters methods
+ ///
+
+ public function toArray () : array {
+ return $this->items;
+ }
+
+}
diff --git a/src/DateTime/DateStamp.php b/src/DateTime/DateStamp.php
new file mode 100644
--- /dev/null
+++ b/src/DateTime/DateStamp.php
@@ -0,0 +1,89 @@
+<?php
+declare(strict_types=1);
+
+namespace Keruald\OmniTools\DateTime;
+
+use Keruald\OmniTools\Collections\OmniArray;
+
+use DateTime;
+use InvalidArgumentException;
+
+class DateStamp {
+
+ ///
+ /// Private members
+ ///
+
+ /**
+ * @var int
+ */
+ private $year;
+
+ /**
+ * @var int
+ */
+ private $month;
+
+ /**
+ * @var int
+ */
+ private $day;
+
+ ///
+ /// Constructors
+ ///
+
+ public function __construct (int $year, int $month, int $day) {
+ $this->year = $year;
+ $this->month = $month;
+ $this->day = $day;
+ }
+
+ public static function fromUnixTime (?int $unixtime = null) : self {
+ $dateStamp = date('Y-m-d', $unixtime ?? time());
+ return self::parse($dateStamp);
+ }
+
+ public static function parse (string $date) : self {
+ if (preg_match("/^[0-9]{4}\-[0-1][0-9]\-[0-3][0-9]$/", $date)) {
+ // YYYY-MM-DD
+ $args = OmniArray::explode("-", $date)
+ ->toIntegers()
+ ->toArray();
+
+ return new DateStamp(...$args);
+ }
+
+ if (preg_match("/^[0-9]{4}[0-1][0-9][0-3][0-9]$/", $date)) {
+ // YYYYMMDD
+ return new DateStamp(
+ (int)substr($date, 0, 4), // YYYY
+ (int)substr($date, 4, 2), // MM
+ (int)substr($date, 6, 2) // DD
+ );
+ }
+
+ throw new InvalidArgumentException("YYYYMMDD or YYYY-MM-DD format expected, $date received.");
+ }
+
+ ///
+ /// Convert methods
+ ///
+
+ public function toUnixTime () : int {
+ return mktime(0, 0, 0, $this->month, $this->day, $this->year);
+ }
+
+ public function toDateTime () : DateTime {
+ return new DateTime($this->__toString());
+ }
+
+ public function toShortString () : string {
+ return date('Ymd', $this->toUnixTime());
+ }
+
+ public function __toString () : string {
+ return date('Y-m-d', $this->toUnixTime());
+ }
+
+}
diff --git a/tests/DateTime/DateStampTest.php b/tests/DateTime/DateStampTest.php
new file mode 100644
--- /dev/null
+++ b/tests/DateTime/DateStampTest.php
@@ -0,0 +1,91 @@
+<?php
+declare(strict_types=1);
+
+namespace Keruald\OmniTools\Tests\DateTime;
+
+use Keruald\OmniTools\DateTime\DateStamp;
+use PHPUnit\Framework\TestCase;
+
+use DateTime;
+
+class DateStampTest extends TestCase {
+
+ ///
+ /// Private members
+ ///
+
+ /**
+ * @var DateStamp
+ */
+ private $dateStamp;
+
+ ///
+ /// Fixtures
+ ///
+
+ protected function setUp () : void {
+ $this->dateStamp = new DateStamp(2010, 11, 25); // 25 November 2010
+ }
+
+ ///
+ /// Tests
+ ///
+
+ public function testToUnixTime () : void {
+ $this->assertEquals(1290643200, $this->dateStamp->toUnixTime());
+ }
+
+ public function testToDateTime () : void {
+ $expectedDateTime = new DateTime("2010-11-25");
+
+ $this->assertEquals($expectedDateTime, $this->dateStamp->toDateTime());
+ }
+
+ public function testToShortString () : void {
+ $this->assertEquals("20101125", $this->dateStamp->toShortString());
+ }
+
+ public function testToString () : void {
+ $this->assertEquals("2010-11-25", (string)$this->dateStamp);
+ }
+
+ public function testFromUnixTime () : void {
+ $this->assertEquals(
+ $this->dateStamp,
+ DateStamp::fromUnixTime(1290643200)
+ );
+ }
+
+ public function testParse () : void {
+ $this->assertEquals(
+ $this->dateStamp,
+ DateStamp::parse("2010-11-25")
+ );
+
+ $this->assertEquals(
+ $this->dateStamp,
+ DateStamp::parse("20101125")
+ );
+ }
+
+ /**
+ * @dataProvider provideInvalidDateStamps
+ */
+ public function testParseWithInvalidFormat ($dateStamp) : void {
+ $this->expectException("InvalidArgumentException");
+ DateStamp::parse($dateStamp);
+ }
+
+ ///
+ /// Data provider
+ ///
+
+ public function provideInvalidDateStamps () : iterable {
+ yield ["10-11-25"];
+ yield ["2010-41-25"];
+ yield ["2010-11-99"];
+ yield ["20104125"];
+ yield ["20101199"];
+ }
+
+}
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Fri, Dec 20, 22:52 (16 h, 46 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2308285
Default Alt Text
D1635.id4175.diff (6 KB)
Attached To
Mode
D1635: Allow to represent a YYYY-MM-DD date stamp
Attached
Detach File
Event Timeline
Log In to Comment