Sort talks as part of the FormatTalksBundle
This commit is contained in:
parent
dab9a0e9b3
commit
eb6a42bae7
|
@ -2,7 +2,7 @@
|
|||
<h2>Events</h2>
|
||||
|
||||
{% include "talks-table" with {
|
||||
talks: { talks: [page], event_data: site.events }|all_talks|reverse,
|
||||
talks: { talks: [page], event_data: site.events }|all_talks,
|
||||
talk_page: true
|
||||
} %}
|
||||
</div>
|
||||
|
|
|
@ -41,7 +41,7 @@ class FormatTalksExtension extends Twig_Extension
|
|||
*/
|
||||
public function getAll(array $data)
|
||||
{
|
||||
return $this->sort($this->format($data));
|
||||
return $this->format($data)->sortBy('event.date');
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -55,9 +55,9 @@ class FormatTalksExtension extends Twig_Extension
|
|||
*/
|
||||
public function getUpcoming(array $data)
|
||||
{
|
||||
return $this->sort($this->format($data)->filter(function ($talk) {
|
||||
return $this->format($data)->filter(function ($talk) {
|
||||
return $talk['event']['date'] >= $this->today;
|
||||
}));
|
||||
})->sortBy('event.date');
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -71,9 +71,9 @@ class FormatTalksExtension extends Twig_Extension
|
|||
*/
|
||||
public function getPast(array $data)
|
||||
{
|
||||
return $this->sort($this->format($data)->filter(function ($talk) {
|
||||
return $this->format($data)->filter(function ($talk) {
|
||||
return $talk['event']['date'] < $this->today;
|
||||
}));
|
||||
})->sortByDesc('event.date');
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -100,18 +100,6 @@ class FormatTalksExtension extends Twig_Extension
|
|||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Sort and return the talks.
|
||||
*
|
||||
* @param Collection $talks The talk data.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function sort(Collection $talks)
|
||||
{
|
||||
return $talks->sortByDesc('event.date')->all();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
|
|
|
@ -4,6 +4,7 @@ namespace FormatTalksBundle\Tests\Twig;
|
|||
|
||||
use DateTime;
|
||||
use FormatTalksBundle\Twig\FormatTalksExtension;
|
||||
use Illuminate\Support\Collection;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class FormatTalksTest extends TestCase
|
||||
|
@ -99,8 +100,9 @@ class FormatTalksTest extends TestCase
|
|||
|
||||
$this->assertCount(3, $results);
|
||||
|
||||
// Earliest events should be returned first.
|
||||
$this->assertEquals(
|
||||
[$eventA['date'], $eventC['date'], $eventB['date']],
|
||||
[$eventB['date'], $eventC['date'], $eventA['date']],
|
||||
$this->extractDates($results)
|
||||
);
|
||||
}
|
||||
|
@ -128,8 +130,9 @@ class FormatTalksTest extends TestCase
|
|||
|
||||
$this->assertCount(3, $results);
|
||||
|
||||
// Earliest events should be returned first.
|
||||
$this->assertEquals(
|
||||
[$eventE['date'], $eventA['date'], $eventC['date']],
|
||||
[$eventC['date'], $eventA['date'], $eventE['date']],
|
||||
$this->extractDates($results)
|
||||
);
|
||||
}
|
||||
|
@ -159,6 +162,7 @@ class FormatTalksTest extends TestCase
|
|||
|
||||
$this->assertCount(2, $results);
|
||||
|
||||
// Latest events should be returned first.
|
||||
$this->assertEquals(
|
||||
[$eventB['date'], $eventF['date']],
|
||||
$this->extractDates($results)
|
||||
|
@ -168,12 +172,12 @@ class FormatTalksTest extends TestCase
|
|||
/**
|
||||
* Extract the returned dates from the results.
|
||||
*
|
||||
* @param array $results The results returned from the filter.
|
||||
* @param Collection $results The results returned from the filter.
|
||||
*
|
||||
* @return array An array of dates.
|
||||
*/
|
||||
private function extractDates(array $results)
|
||||
private function extractDates(Collection $results)
|
||||
{
|
||||
return collect($results)->pluck('event.date')->all();
|
||||
return $results->pluck('event.date')->all();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue