Slide 1

Slide 1 text

Laravel
 Collection Λ࢖͍౗͍ͨ͠ͱࢥͬͨ࿩ PHPBLT #7 by nunulk

Slide 2

Slide 2 text

‘Laravel Collection ͷศརͳϝιου’,
 2 => ‘͢΂ͯͷAPIʹ͍ͭͯαϯϓϧίʔυΛॻ͘’,
 3 => ‘͓·͚:ϦϑϨΫγϣϯͰϝιουΛऔಘ͢Δ’,
 ]);
 }
 }

Slide 3

Slide 3 text

// 1. Laravel Collection ͷศརͳϝιου
 $examples = $methods->random(5)->shuffle();

Slide 4

Slide 4 text

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());
 }
 }
 


Slide 5

Slide 5 text

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());
 }
 }


Slide 6

Slide 6 text

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());
 }
 }


Slide 7

Slide 7 text

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);
 }
 }


Slide 8

Slide 8 text

//2. ͢΂ͯͷAPIʹ͍ͭͯαϯϓϧίʔυΛॻ͘
 $examples = $I->writeExamples($methods->where(‘is_public’));

Slide 9

Slide 9 text

argument('class');
 $methods = Method::collectDefinitions($class);
 $methods->pluck(‘name’)->tap(function ($names) {
 echo ‘count: ‘ . $names->count() . PHP_EOL;
 echo ‘methods: ‘ . $names->implode(‘, ') . PHP_EOL;
 });
 }
 }


Slide 10

Slide 10 text

» 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

Slide 11

Slide 11 text

assertEquals([], $emptyItems->toArray());
 
 $filledItems = new Collection([1, 2, 3]);
 $this->assertEquals([1, 2, 3], $filledItems->toArray());
 }
 // (snip other 98 methods)
 }
 


Slide 12

Slide 12 text


 
 
 » ./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)

Slide 13

Slide 13 text

//3. ͓·͚ɿϦϑϨΫγϣϯͰϝιου৘ใΛऔಘ͢Δ
 Collection::make((new ReflectionClass($class))->getMethods());

Slide 14

Slide 14 text

getMethods())
 ->filter(function ($method) {
 return $method->isPublic();
 })
 ->map(function (ReflectionMethod $method) {
 return new Definition($method);
 });
 }
 }


Slide 15

Slide 15 text

/**
 * ͞Βʹ͓·͚ɿ͢΂ͯͷαϯϓϧίʔυ͸ҎԼ͔Β͝ཡ͍ͩ͘͞ɻ
 * @see http://qiita.com/nunulk/items/9e0c4a371e94d763aed6
 */