Skip to content
Zach Leigh edited this page Apr 25, 2016 · 6 revisions

Contents

General

The LimelightResults object extends the abstract Collection class and has access to all its methods. Methods in this class were adapted from Laravel's Collection class. For more examples and information, visit the Laravel documentation.

All examples in this section will use the following text.

$results = $limelight->parse('ライムをかじるのは無謀。');

Method Index

Methods

all()

Get all words from the collection.

$all = $results->all();

// Array of all LimelightWord objects

chunk()

Break the collection of words into smaller collections of given size.

$chunks = $results->chunk(3);

$output = [];

foreach($chunks as $chunk) {
    $output[] = $chunk->pluck('word')->all();
}

// [['ライム', 'を', 'かじる'], ['の', 'は', '無謀'], ['。']]

convert()

Convert the collection items to hiragana or katakana.

$hiragana = $results->convert('hiragana');

$hiraganaOutput = $hiragana->pluck('reading')->all();

// ['らいむ', 'を', 'かじる', 'の', 'は', 'むぼう', '。']

$katakana = $hiragana->convert('katakana');

$katakanaOutput = $katakana->pluck('reading')->all();

// ['ライム', 'ヲ', 'カジル', 'ノ', 'ハ', 'ムボウ', '。']

count()

Return the number items in the collection.

$output = $result->count();

// 7

diff()

Compare the collection against the given collection.

$results2 = $limelight->parse('をかじるのは無謀。');

$difference = $results->diff($results2);

$output = $difference->pluck('word')->all();

// ['ライム']

every()

Create a new collection consisting of every n-th element.

$everyThird = $results->every(3);

$output = $everyThird->pluck('word')->all();

// ['ライム', 'の', '。']

Pass an optional offset as the second argument.

$everySecond = $results->every(2, 1);

$output = $everySecond->pluck('word')->all();

// ['を', 'の', '無謀']

except()

Get all items except for those with the specified keys.

$exceptFirst = $results->except(0);

$output = $exceptFirst->pluck('word')->all();

// ['を', 'かじる', 'の', 'は', '無謀', '。']

An array of keys may also be passed.

$allButLast = $results->except([0, 1, 2, 3, 4, 5]);

$output = $allButLast->pluck('word')->all();

// ['。']

filter()

Run a filter over each of the items in the collection.

$filtered = $results->filter(function ($item, $key) {
    return $item->word !== 'ライム';
});

$output = $filtered->pluck('word')->all();

// ['を', 'かじる', 'の', 'は', '無謀', '。']

Passing no argument removes any empty collection items.

$messy = $results->push([])->push([]);

$messyOutput = $messy->pluck('word')->all();

// ['ライム', 'を', 'かじる', 'の', 'は', '無謀', '。', NULL, NULL]

$filtered = $messy->filter();

$filteredOutput = $filtered->pluck('word')->all();

// ['ライム', 'を', 'かじる', 'の', 'は', '無謀', '。']

first()

Get the first item from the collection.

$first = $results->first();

// First LimelightWord object in collection

When passed a function containing a truth test, the first item that passes the test will be returned.

$first = $results->first(function ($item, $key) {
    return $item->word === 'を';
});

// First LimelightWord object that passed truth test

flatten()

Get a flattened array of the items in the collection.

$expanded = $results->splice(0, 2)->pluck('pluginData');

// [['Furigana' => 'ライム', 'Romaji' => 'raimu'], ['Furigana' => 'を', 'Romaji' => 'o']]

$flat = $expanded->flatten();

// ['ライム', 'raimu', 'を', 'o']

forget()

Remove an item from the collection by key.

$noThird = $results->forget(2);

$output = $noThird->pluck('word')->all();

// ['ライム', 'を', 'の', 'は', '無謀', '。']

groupBy()

Group the collection by a field.

$grouped = $results->groupBy('partOfSpeech');

$output = [];

foreach ($grouped as $key => $group) {
    $output[$key] = $group->pluck('word')->all();
}

// [
//     'noun' => ['ライム', 'の', '無謀'],
//     'postposition' => ['を', 'は'],
//     'verb' => ['かじる'],
//     'symbol => ['。']
// ]

A callback may also be passed.

$grouped = $results->groupBy(function ($item, $key) {
    return mb_strlen($item->word);
});

$output = [];

foreach ($grouped as $key => $group) {
   $output[$key] = $group->pluck('word')->all();
}

// [
//     '1' => ['を', 'の', 'は', '。'],
//     '2' => ['無謀'],
//     '3' => ['ライム', 'かじる']
// ]

implode()

Concatenate values of a given key as a string.

$output = $results->implode('reading');

// 'ライムヲカジルノハムボウ。'

intersect()

Intersect the collection with the given items.

$missingThree = $results->except([0, 1, 2]);

$intersected = $results->intersect($missingThree);

$output = $missingThree->pluck('word')->all();

// ['の', 'は', '無謀', '。']

isEmpty()

Determine if the collection is empty or not.

$results->isEmpty();

// false

keys()

Get the keys of the collection items.

$keys = $results->keys()->all();

// [0, 1, 2, 3, 4, 5, 6]

last()

Get the last item from the collection.

$last = $results->last();

// Last LimelightWord object in collection

When passed a function containing a truth test, the last item that passes the test will be returned.

$last = $results->last(function ($item, $key) {
    return $item->partOfSpeech === 'noun';
});

// Last LimelightWord object in collection that is a noun

map()

Run a map over each of the items.

$output = $results->map(function ($item, $key) {
    return $item->word;
})->all();

// ['ライム', 'を', 'かじる', 'の', 'は', '無謀', '。']

merge()

Merge the collection with the given items.

$resultsCopy = $results;

$merged = $results->merge($resultsCopy);

$output = $merged->pluck('word')->all();

// ['ライム', 'を', 'かじる', 'の', 'は', '無謀', '。', 'ライム', 'を', 'かじる', 'の', 'は', '無謀', '。']

only()

Get the items with the specified keys.

$onlyFirstTwo = $results->only([0, 1]);

$output = $onlyFirstTwo->pluck('word')->all();

// ['ライム', 'を']

pluck()

Get the values of a given key.

$output = $results->pluck('word')->all();

// ['ライム', 'を', 'かじる', 'の', 'は', '無謀', '。']

pop()

Get and remove the last item from the collection.

$last = $results->pop();

// Last LimelightWord object in collection

prepend()

Push an item onto the beginning of the collection.

$first = $results->first();

$doubleFirst = $results->prepend($first);

$output = $doubleFirst->pluck('word')->all();

// ['ライム', 'ライム', 'を', 'かじる', 'の', 'は', '無謀', '。']

pull()

Get and remove an item from the collection by key.

$second = $results->pull(1);

// Second LimelightWord object in collection

push()

Push an item onto the end of the collection.

$last = $results->last();

$doubleLast = $results->push($last);

$output = $doubleLast->pluck('word')->all();

// ['ライム', 'を', 'かじる', 'の', 'は', '無謀', '。', '。']

reject()

Create a collection of all elements that do not pass a given truth test.

$onlyVerbs = $results->reject(function ($value, $key) {
    return $value->partOfSpeech !== 'verb';
});

$output = $onlyVerbs->pluck('word')->all();

// ['かじる']

shift()

Get and remove the first item from the collection.

$last = $results->shift();

// Last LimelightWord object in collection

slice()

Slice the collection array.

$sliced = $results->slice(3);

$output = $sliced->pluck('word')->all();

// ['の', 'は', '無謀', '。']

Pass an optional second parameter to limit the size of the slice.

$sliced = $results->slice(3, 1);

$output = $sliced->pluck('word')->all();

// ['の']

splice()

Splice a portion of the collection array.

$results->splice(3);

$output = $results->pluck('word')->all();

// ['ライム', 'を', 'かじる']

Pass an optional second parameter to limit the size of the slice.

$results->splice(3, 3);

$output = $results->pluck('word')->all();

// ['ライム', 'を', 'かじる', '。']

Pass a replacement for the spliced portion as the third argument.

$first = $results->only(0);

$results->splice(3, 4, $first->all());

$output = $results->pluck('word')->all();

// ['ライム', 'を', 'かじる', 'ライム']

take()

Take the first n number of items using a positive number.

$firstTwo = $results->take(2);

$output = $firstTwo->pluck('word')->all();

// ['ライム', 'を']

Take the last n number of items using a negative number.

$firstTwo = $results->take(-2);

$output = $firstTwo->pluck('word')->all();

// ['無謀', '。']

toArray()

Get the collection of items as a plain array.

$array = $results->toArray();

// Array of all collection items
// Nested objects also in array form

toJson()

Get the collection of items as JSON.

$json = $results->toJson();

// Collection items as JSON string

transform()

Transform each item in the collection using a callback.

$results->transform(function ($item, $key) {
    return $item->word;
});

$output = $results->all();

// ['ライム', 'を', 'かじる', 'の', 'は', '無謀', '。']

unique()

Return only unique items from the collection array.

$copy = $results;

$merged = $results->merge($copy);

$doubleOutput = $merged->pluck('word')->all();

// ['ライム', 'を', 'かじる', 'の', 'は', '無謀', '。', 'ライム', 'を', 'かじる', 'の', 'は', '無謀', '。']

$unique = $merged->unique();

$uniqueOutput = $unique->pluck('word')->all();

// ['ライム', 'を', 'かじる', 'の', 'は', '無謀', '。']

A callback may also be used to determine uniqueness.

$unique = $results->unique(function ($item) {
    return $item->partOfSpeech;
});

$output = $unique->pluck('word')->all();

// ['ライム', 'を', 'かじる', '。']

values()

Reset the keys on the underlying array if keys are numeric.

$results->forget([0, 2, 4]);

$keys = $results->keys()->all();

// [1, 3, 5, 6]

$resetValues = $results->values();

$keys = $resetValues->keys()->all();

// [0, 1, 2, 3]

where()

Filter items by the given key value pair.

$words = $results->where('word', 'ライム');

$output = $words->pluck('word')->all();

// ['ライム']

zip()

Zip the collection together with one or more arrays.

$zipped = $results->only(0)->pluck('word')->zip(['value']);

$output = $zipped->flatten()->toArray();

// ['ライム', 'value']