Share
Embed
using up Laravel Collection
nunulk
May 21, 2017
Transcript
15 slides
-
<?php namespace nunulk; use PHPBLT\LightningTalk; /** * @author
nunulk - freelance PHP programmer / Laravel lover * @description ຊ Laravel ʹࠝแ͞Ε͍ͯΔ Collection Ϋϥεʹ͍͓ͭͯ͠·͢ */ class MyTalk extends LightningTalk { public function content() { // ࠓ͢͜ͱ return Collection::make([ 1 => ‘Laravel Collection ͷศརͳϝιου’, 2 => ‘ͯ͢ͷAPIʹ͍ͭͯαϯϓϧίʔυΛॻ͘’, 3 => ‘͓·͚:ϦϑϨΫγϣϯͰϝιουΛऔಘ͢Δ’, ]); } } -
<?php namespace Tests\Unit; use Illuminate\Support\Collection; use Tests\TestCase; /** *
ศརͳϝιουͦͷ1 - pluck */ class CollectionTest extends TestCase { public function testPluck() { $items = new Collection([ [‘id' => 1, 'name' => ‘John Doe’, 'score' => 3], [‘id' => 2, 'name' => ‘Jane Doe’, 'score' => 4], ]); // ྻ͔ΒࢦఆͷΩʔͰ࿈ྻΛͭ͘Δ $pairs = $items->pluck(‘name’, ‘id’); $this->assertEquals([1 => ‘John Doe’, 2 => ‘Jane Doe’], $pairs->toArray()); } } -
<?php namespace Tests\Unit; use Illuminate\Support\Collection; use Tests\TestCase; /** *
ศརͳϝιουͦͷ2 - where */ class CollectionTest extends TestCase { public function testWhere() { $items = new Collection([ ['id' => 1, 'score' => 3], ['id' => 2, 'score' => 4], ['id' => 3, 'score' => 5], ]); // foreach ͱ if ͷΈ߹Θ͔ͤΒͷղ์ $filtered = $items->where('score', '>', 3); $this->assertEquals([2, 3], $filtered->pluck('id')->toArray()); } } -
<?php namespace Tests\Unit; use Illuminate\Support\Collection; use Tests\TestCase; /** *
ศརͳϝιουͦͷ3 - except */ class CollectionTest extends TestCase { public function testExcept() { $entity = new Collection([‘id' => 1, 'name' => ‘John Doe', 'score' => 3]); // foreach ͱ if ͷΈ߹Θ͔ͤΒͷղ์ $excepted = $entity->except([‘name’]); $this->assertEquals(['id' => 1, 'score' => 3], $excepted->toArray()); // key ͕ͳౖͯ͘ΒΕͳ͍Α $excepted = $entity->except(['missing attribute']); $this->assertEquals(['id' => 1, 'name' => 'John Doe', 'score' => 10], $excepted- >toArray()); } } -
<?php namespace Tests\Unit; use Illuminate\Support\Collection; use Tests\TestCase; /** *
ศརͳϝιουͦͷ4 - avg */ class CollectionTest extends TestCase { public function testAvg() { $items = new Collection([ [‘id’ => 1, ‘birthday' => ‘2000-05-01’], [‘id’ => 2, ‘birthday' => ‘2001-05-01’], [‘id’ => 3, ‘birthday' => ‘2002-05-01’], ]); Carbon::setTestNow('2017-05-01 00:00:00'); $avg = $items->avg(function ($item) { return (new Carbon($a['birthday']))->startOfDay()->age; }); $this->assertEquals(16, $avg); } } -
<?php namespace App\Console\Commands; use Illuminate\Console\Command; use Illuminate\Support\Collection; use App\Models\Method;
/** * Collection Ϋϥεͷެ։ϝιουΛ͑ͯΈͨ */ class ListMethodsCommand extends Command { public function handle() { $class = $this->argument('class'); $methods = Method::collectDefinitions($class); $methods->pluck(‘name’)->tap(function ($names) { echo ‘count: ‘ . $names->count() . PHP_EOL; echo ‘methods: ‘ . $names->implode(‘, ') . PHP_EOL; }); } } -
» php artisan Laravel Framework 5.4.23 […] » php artisan
app:list-methods "Illuminate\\Support\\Collection" count: 98 methods: __construct, make, times, all, avg, average, median, mode, collapse, contains, containsStrict, diff, diffKeys, each, every, except, filter, when, where, whereStrict, whereIn, whereInStrict, whereNotIn, whereNotInStrict, first, flatten, flip, forget, get, groupBy, keyBy, has, implode, intersect, isEmpty, isNotEmpty, keys, last, pluck, map, mapToGroups, mapWithKeys, flatMap, max, merge, combine, union, min, nth, only, forPage, partition, pipe, pop, prepend, push, pull, put, random, reduce, reject, reverse, search, shift, shuffle, slice, split, chunk, sort, sortBy, sortByDesc, splice, sum, take, tap, transform, unique, uniqueStrict, values, zip, toArray, jsonSerialize, toJson, getIterator, getCachingIterator, count, toBase, offsetExists, offsetGet, offsetSet, offsetUnset, __toString, proxy, __get, macro, hasMacro, __callStatic, __call -
<?php namespace Tests\Unit; use Illuminate\Support\Collection; use Tests\TestCase; /**
* Class CollectionTest * @package Tests\Unit */ class CollectionTest extends TestCase { public function testConstruct() { $emptyItems = new Collection(); $this->assertEquals([], $emptyItems->toArray()); $filledItems = new Collection([1, 2, 3]); $this->assertEquals([1, 2, 3], $filledItems->toArray()); } // (snip other 98 methods) } -
» ./vendor/bin/phpunit tests/Unit/CollectionTest.php PHPUnit 5.7.19 by Sebastian
Bergmann and contributors. 99 -_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-__,------, 0 -_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-__| /\_/\ 0 -_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_^|__( ^ .^) -_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_ "" "" Time: 1.82 seconds, Memory: 14.00MB OK (99 tests, 168 assertions) -
<?php namespace App\Models; use Illuminate\Support\Fluent; use Illuminate\Support\Collection; […]
class Method extends Fluent { public function collectDefinitions(string $class): Collection { if (!class_exists($class)) { return new Collection(); } return Collection::make((new ReflectionClass($class))->getMethods()) ->filter(function ($method) { return $method->isPublic(); }) ->map(function (ReflectionMethod $method) { return new Definition($method); }); } }