Merge branch 'nomad-php-talk'

This commit is contained in:
Oliver Davies 2017-11-07 18:08:52 +00:00
commit fef291c1d8
5 changed files with 39 additions and 29 deletions

View file

@ -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>

View file

@ -20,7 +20,11 @@
{% endif %}
<small class="db black-70">
{% if row.talk.type %}
{{ row.talk.type }}
{% else %}
{{ row.event.type|default('Talk') }}
{% endif %}
</small>
</td>
{% endif %}

View file

@ -0,0 +1,11 @@
---
title: Using Laravel Collections outside Laravel
slides:
url: ~
embed: ~
tags: [nomad-php, lightning-talk, laravel, collections]
type: Lightning talk
events:
- { event: nomad-php, date: '2017-12-21', 'time': 19:00 CET }
---
Laravel Collections are a powerful object-orientated way of interacting with PHP arrays, but did you know that they can be used outside of Laravel, in any PHP project? This short talk shows how we can use Composer to include Laravel Collections within a non-Laravel project and put them to use within your own code.

View file

@ -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}
*/

View file

@ -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
@ -21,6 +22,9 @@ class FormatTalksTest extends TestCase
$this->extension = new FormatTalksExtension();
}
/**
* @covers FormatTalksExtension::format()
*/
public function testFormat()
{
$data = [
@ -79,7 +83,7 @@ class FormatTalksTest extends TestCase
}
/**
* Test getting all events.
* @covers FormatTalksExtension::getAll()
*/
public function testGetAll()
{
@ -99,16 +103,17 @@ 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)
);
}
/**
* Test getting only upcoming events.
* @covers FormatTalksExtension::getUpcoming()
*/
public function testUpcomingEventsFilter()
public function testGetUpcoming()
{
$eventA = ['date' => (new DateTime('+1 week'))->format('Y-m-d')];
$eventB = ['date' => (new DateTime('-2 weeks'))->format('Y-m-d')];
@ -128,16 +133,17 @@ 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)
);
}
/**
* Test getting only past events.
* @covers FormatTalksExtension::getPast()
*/
public function testPastFilter()
public function testGetPast()
{
$eventA = ['date' => (new DateTime('+1 week'))->format('Y-m-d')];
$eventB = ['date' => (new DateTime('-2 weeks'))->format('Y-m-d')];
@ -159,6 +165,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 +175,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();
}
}