function _makeMarkdown() { $template = (include __DIR__ . '/templates.php'); $catTemplate = _::template($template['markdown']['category']); $funcTemplate = _::template($template['markdown']['function']); $content = ""; $methodsByCategory = _::groupBy(_getMethods(), function ($method) { return $method->category; }); $formatMenuEntry = function ($name) { return _anchor($name); }; foreach ($methodsByCategory as $category => $methods) { $categoryContent = ""; foreach ($methods as $function) { $categoryContent .= $funcTemplate(compact('function')); } $content .= $catTemplate(['category' => $category, 'content' => $categoryContent, 'functions' => _::chain($methods)->pluck('name')->map($formatMenuEntry)->value()]); } $installation = ""; $usage = ""; $menu = _::template($template['markdown']['menu'], ['categories' => _::map($methodsByCategory, function ($methods) { return _::pluck($methods, 'name'); })]); return _::template($template['markdown']['main'], compact('menu', 'content')); }
function _groupBy($list, $iterator, $context = NULL) { return Underscore::groupBy($list, $iterator, $context); }
/** * @dataProvider peopleDataProvider * @tags collections */ public function testGroupBy($people, $type, $meta) { $profession = function ($v) { return _::get($v, 'profession'); }; $groupedPeople = ['actor' => [_::get($people, 'jnicholson'), _::get($people, 'mfreeman'), _::get($people, 'ldicaprio'), _::get($people, 'nportman')], 'producer' => [_::get($people, 'rscott')]]; // it should group items into 2 dimentionnals arrays using a criteria function $this->array(_::groupBy($people, $profession))->isEqualTo($groupedPeople); // it should accept a property name instead of a closure $this->array(_::groupBy($people, 'profession'))->isEqualTo($groupedPeople); // it should be possible to specify a context for criteria function $profession = function ($v) { return _::get($v, $this->property); }; $context = (object) ['property' => 'profession']; $this->array(_::groupBy($people, $profession, $context))->isEqualTo($groupedPeople); // it should works with an inexistent property name by put all results under NULL index $this->array(_::groupBy($people, 'inexistantProperty'))->keys->isEqualTo([null]); // it should not preserve original list keys $this->array(_::groupBy(['a' => ['v' => 1], 'b' => ['v' => 2], 'c' => ['v' => 1]], 'v'))->isEqualTo([1 => [['v' => 1], ['v' => 1]], 2 => [['v' => 2]]]); }