Skip to content

Commit

Permalink
Merge pull request #7 from cultuurnet/feature/III-2034
Browse files Browse the repository at this point in the history
III-2034: Handle periods indexed as timestamps
  • Loading branch information
Luc Wollants committed May 10, 2017
2 parents 973d54e + bc93320 commit 32538bb
Show file tree
Hide file tree
Showing 7 changed files with 569 additions and 28 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"require": {
"php": ">=5.6.0",
"ext-intl": "*",
"cultuurnet/cdb": "~2.0"
"cultuurnet/cdb": "~2.2.0"
},
"autoload": {
"psr-4": {
Expand Down
67 changes: 59 additions & 8 deletions src/Timestamps/LargeTimestampsHTMLFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ public function __construct()
);
}

/**
* @param \CultureFeed_Cdb_Data_Calendar_TimestampList $timestampList
* @return string
*/
public function format(
\CultureFeed_Cdb_Data_Calendar_TimestampList $timestampList
) {
Expand All @@ -73,8 +77,13 @@ public function format(
}
}

public function formatSingleTimestamp($timestamp)
{
/**
* @param \CultureFeed_Cdb_Data_Calendar_Timestamp $timestamp
* @return string
*/
private function formatSingleTimestamp(
\CultureFeed_Cdb_Data_Calendar_Timestamp $timestamp
) {
$date = $timestamp->getDate();
$intlDate = $this->fmt->format(strtotime($date));
$intlWeekDay = $this->fmtWeekDayLong->format(strtotime($date));
Expand Down Expand Up @@ -118,20 +127,56 @@ public function formatSingleTimestamp($timestamp)
return $output;
}

public function formatMultipleTimestamps($timestampList, $timestamps_count)
{
/**
* @param \CultureFeed_Cdb_Data_Calendar_TimestampList $timestampList
* @param int $timestamps_count
* @return string
*/
private function formatMultipleTimestamps(
\CultureFeed_Cdb_Data_Calendar_TimestampList $timestampList,
$timestamps_count
) {
$today = strtotime(date('Y-m-d') . ' 00:00:00');

$output = '<ul class="list-unstyled">';

// keep track of the active period and when the last one started, zero means none.
$activePeriodIndex = 0;
$lastPeriodStartDate = null;
$lastPeriodStartTime = null;

for ($i = 0; $i < $timestamps_count; $i++) {
/** @var \CultureFeed_Cdb_Data_Calendar_Timestamp $timestamp */
$timestamp = $timestampList->current();
$date = $timestamp->getDate();
$intlDate = $this->fmt->format(strtotime($date));
$intlWeekDay = $this->fmtWeekDayShort->format(strtotime($date));
$endDate = $timestamp->getEndDate();
$seconds = intval(substr($timestamp->getStartTime(), 6, 2));
$endTime = $timestamp->getEndTime();
$startTime = $timestamp->getStartTime();
$intlStartTime = $this->fmtTime->format(strtotime($startTime));
$endTime = $timestamp->getEndTime();

if ($seconds > 0 && $seconds !== $activePeriodIndex) {
$lastPeriodStartDate = $date;
$lastPeriodStartTime = $intlStartTime;
}

$activePeriodIndex = $seconds;

if ($activePeriodIndex > 0) {
if (empty($endTime)) {
$timestampList->next();
continue;
} else {
$date = $lastPeriodStartDate;
$intlStartTime = $lastPeriodStartTime;
}
}

$intlDate = $this->fmt->format(strtotime($date));
$intlWeekDay = $this->fmtWeekDayShort->format(strtotime($date));

$intlEndDate = $this->fmt->format(strtotime($endDate));
$intlEndWeekDay = $this->fmtWeekDayShort->format(strtotime($endDate));
$intlEndTime = $this->fmtTime->format(strtotime($endTime));

if (strtotime($date) >= $today) {
Expand Down Expand Up @@ -160,7 +205,13 @@ public function formatMultipleTimestamps($timestampList, $timestamps_count)
$output .= ' ';
$output .= '<span class="cf-to cf-meta">tot</span>';
$output .= ' ';
$output .= '<time itemprop="endDate" datetime="' . $date . 'T' . $intlEndTime . '">';
$output .= '<time itemprop="endDate" datetime="' . $endDate . 'T' . $intlEndTime . '">';
if ($activePeriodIndex > 0) {
$output .= '<span class="cf-weekday cf-meta">' . $intlEndWeekDay . '</span>';
$output .= ' ';
$output .= '<span class="cf-date">' . $intlEndDate . '</span>';
$output .= ' ';
}
$output .= '<span class="cf-time">' . $intlEndTime . '</span>';
$output .= '</time>';
}
Expand Down
88 changes: 69 additions & 19 deletions src/Timestamps/LargeTimestampsPlainTextFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

namespace CultuurNet\CalendarSummary\Timestamps;

use CultureFeed_Cdb_Data_Calendar_Timestamp;
use CultureFeed_Cdb_Data_Calendar_TimestampList;
use IntlDateFormatter;

class LargeTimestampsPlainTextFormatter
Expand Down Expand Up @@ -59,8 +61,12 @@ public function __construct()
);
}

/**
* @param CultureFeed_Cdb_Data_Calendar_TimestampList $timestampList
* @return string
*/
public function format(
\CultureFeed_Cdb_Data_Calendar_TimestampList $timestampList
CultureFeed_Cdb_Data_Calendar_TimestampList $timestampList
) {
$timestamps_count = iterator_count($timestampList);
$timestampList->rewind();
Expand All @@ -73,8 +79,13 @@ public function format(
}
}

public function formatSingleTimestamp($timestamp)
{
/**
* @param CultureFeed_Cdb_Data_Calendar_Timestamp $timestamp
* @return string
*/
private function formatSingleTimestamp(
CultureFeed_Cdb_Data_Calendar_Timestamp $timestamp
) {
$date = $timestamp->getDate();
$intlDate = $this->fmt->format(strtotime($date));
$intlWeekDay = $this->fmtWeekDayLong->format(strtotime($date));
Expand All @@ -97,34 +108,73 @@ public function formatSingleTimestamp($timestamp)
return $output;
}

public function formatMultipleTimestamps($timestampList, $timestamps_count)
{
/**
* @param CultureFeed_Cdb_Data_Calendar_TimestampList $timestampList
* @param int $timestamps_count
* @return string
*/
private function formatMultipleTimestamps(
CultureFeed_Cdb_Data_Calendar_TimestampList $timestampList,
$timestamps_count
) {
$today = strtotime(date('Y-m-d') . ' 00:00:00');
$output = '';

// keep track of the active period and when the last one started, zero means none.
$activePeriodIndex = 0;
$lastPeriodStartDate = null;
$lastPeriodStartTime = null;

for ($i = 0; $i < $timestamps_count; $i++) {
/** @var CultureFeed_Cdb_Data_Calendar_Timestamp $timestamp */
$timestamp = $timestampList->current();
$date = $timestamp->getDate();
$intlDate = $this->fmt->format(strtotime($date));
$intlWeekDay = $this->fmtWeekDayShort->format(strtotime($date));
$endDate = $timestamp->getEndDate();
$seconds = intval(substr($timestamp->getStartTime(), 6, 2));
$endTime = $timestamp->getEndTime();
$startTime = $timestamp->getStartTime();
$intlStartTime = $this->fmtTime->format(strtotime($startTime));
$endTime = $timestamp->getEndTime();

if ($seconds > 0 && $seconds !== $activePeriodIndex) {
$lastPeriodStartDate = $date;
$lastPeriodStartTime = $intlStartTime;
}

$activePeriodIndex = $seconds;

if ($activePeriodIndex > 0) {
if (empty($endTime)) {
$timestampList->next();
continue;
} else {
$date = $lastPeriodStartDate;
$intlStartTime = $lastPeriodStartTime;
}
}

$intlDate = $this->fmt->format(strtotime($date));
$intlWeekDay = $this->fmtWeekDayShort->format(strtotime($date));

$intlEndDate = $this->fmt->format(strtotime($endDate));
$intlEndWeekDay = $this->fmtWeekDayShort->format(strtotime($endDate));
$intlEndTime = $this->fmtTime->format(strtotime($endTime));

if (strtotime($date) >= $today) {
$output .= $intlWeekDay . ' ' . $intlDate . PHP_EOL;
if (!empty($endTime)) {
$output .= 'van ';
$output = empty($output) ? $output : $output . PHP_EOL;
if ($activePeriodIndex === 0) {
$output .= $intlWeekDay . ' ' . $intlDate . PHP_EOL;
if (!empty($endTime)) {
$output .= 'van ';
} else {
$output .= 'om ';
}
$output .= $intlStartTime;
if (!empty($endTime)) {
$output .= ' tot ' . $intlEndTime;
}
} else {
$output .= 'om ';
}
$output .= $intlStartTime;
if (!empty($endTime)) {
$output .= ' tot ' . $intlEndTime;
}
if ($i != $timestamps_count-1) {
$output .= PHP_EOL;
$output .= 'Van ' . $intlWeekDay . ' ' . $intlDate . ' ' . $intlStartTime . PHP_EOL;
$output .= 'tot ' . $intlEndWeekDay . ' ' . $intlEndDate . ' ' . $intlEndTime;
}
}

Expand Down
Loading

0 comments on commit 32538bb

Please sign in to comment.