ArrayValue
defines interface of value object for indexed array.
<?php
use GW\Value\Wrap;
$array = Wrap::array(['a', 'b', 'c']);
$array[0]; // 'a'
$has = isset($array[3]); // false
$array[3]; // Exception
$array->count(); // 3
foreach ($array as $item) {
echo $item;
} // abc
<?php
/**
* @param callable(TValue $value):void $callback
* @phpstan-return ArrayValue<TValue>
*/
public function each(callable $callback): ArrayValue;
Call some callback on each item of ArrayValue
and return this ArrayValue
.
Items are not reassigned, so state ArrayValue
should not have changed.
<?php
use GW\Value\Wrap;
$array = Wrap::array(['a', 'b', 'c']);
$mapped = $array->each(function (string $letter): void {
echo $letter;
});
abc
<?php
/**
* @param (callable(TValue,TValue):int)|null $comparator
* @phpstan-return ArrayValue<TValue>
*/
public function unique(?callable $comparator = null): ArrayValue;
Filter ArrayValue
items removing duplicated items.
When $comparator
is not provided items are compared as strings.
<?php
use GW\Value\Wrap;
$names = Wrap::array(['John', 'Basil', 'John', 'Johny', 'Jon', 'Basile']);
echo 'unique names = ';
var_export($names->unique()->toArray());
echo PHP_EOL;
echo 'unique by callback = ';
var_export($names->unique(function(string $nameA, string $nameB): int {
return levenshtein($nameA, $nameB) > 2 ? 1 : 0;
})->toArray());
echo PHP_EOL;
unique names = array (
0 => 'John',
1 => 'Basil',
2 => 'Johny',
3 => 'Jon',
4 => 'Basile',
)
unique by callback = array (
0 => 'John',
1 => 'Basil',
)
<?php
/**
* @phpstan-return array<int, TValue>
*/
public function toArray(): array;
Return primitive array
from subject ArrayValue
.
<?php
/**
* @param callable(TValue $value):bool $filter
* @phpstan-return ArrayValue<TValue>
*/
public function filter(callable $filter): ArrayValue;
Create new ArrayValue
with items filtered by callback.
<?php
use GW\Value\Wrap;
$array = Wrap::array([1, 2, 3, 4]);
$even = $array->filter(function (int $number): bool {
return $number % 2 === 0;
});
var_export($even->toArray());
array (
0 => 2,
1 => 4,
)
<?php
/**
* @phpstan-return ArrayValue<TValue>
*/
public function filterEmpty(): ArrayValue;
Filter out empty items from ArrayValue
.
<?php
use GW\Value\Wrap;
$array = Wrap::array(['a', '', 'b', 'c']);
$notEmpty = $array->filterEmpty();
var_export($notEmpty->toArray());
array (
0 => 'a',
1 => 'b',
2 => 'c',
)
<?php
/**
* @template TNewValue
* @param callable(TValue $value):TNewValue $transformer
* @phpstan-return ArrayValue<TNewValue>
*/
public function map(callable $transformer): ArrayValue;
Create new ArrayValue
with items mapped by callback.
<?php
use GW\Value\Wrap;
$array = Wrap::array(['a', 'b', 'c']);
$mapped = $array->map(function (string $letter): string {
return 'new ' . $letter;
});
var_export($mapped->toArray());
array (
0 => 'new a',
1 => 'new b',
2 => 'new c',
)
<?php
/**
* @template TNewValue
* @param callable(TValue $value):iterable<TNewValue> $transformer
* @phpstan-return ArrayValue<TNewValue>
*/
public function flatMap(callable $transformer): ArrayValue;
<?php
/**
* @template TNewKey of int|string
* @param callable(TValue $value):TNewKey $reducer
* @phpstan-return AssocValue<TNewKey, ArrayValue<TValue>>
*/
public function groupBy(callable $reducer): AssocValue;
Group items by key extracted from value by $reducer
callback.
Result is AssocValue
containing association: ['key1' => [items reduced to key1], 'key2' => [items reduced to key2]]
.
<?php
use GW\Value\ArrayValue;
use GW\Value\Wrap;
$payments = Wrap::array([
['group' => 'food', 'amount' => 10],
['group' => 'drinks', 'amount' => 10],
['group' => 'food', 'amount' => 20],
['group' => 'travel', 'amount' => 500],
['group' => 'drinks', 'amount' => 20],
['group' => 'food', 'amount' => 50],
]);
$get = function (string $key): \Closure {
return function (array $payment) use ($key): string {
return $payment[$key];
};
};
echo 'grouped expenses:', PHP_EOL;
var_export(
$payments
->groupBy($get('group'))
->map(function (ArrayValue $group) use ($get): array {
return $group->map($get('amount'))->toArray();
})
->toAssocArray()
);
echo PHP_EOL, PHP_EOL;
$numbers = Wrap::array([1, 2, 3, 4, 3, 4, 5, 6, 7, 8, 9]);
$even = function (int $number): int {
return $number % 2;
};
echo 'even partition:', PHP_EOL;
var_export(
$numbers
->groupBy($even)
->map(function (ArrayValue $group): array {
return $group->toArray();
})
->toArray()
);
grouped expenses:
array (
'food' =>
array (
0 => '10',
1 => '20',
2 => '50',
),
'drinks' =>
array (
0 => '10',
1 => '20',
),
'travel' =>
array (
0 => '500',
),
)
even partition:
array (
0 =>
array (
0 => 1,
1 => 3,
2 => 3,
3 => 5,
4 => 7,
5 => 9,
),
1 =>
array (
0 => 2,
1 => 4,
2 => 4,
3 => 6,
4 => 8,
),
)
<?php
/**
* @phpstan-return ArrayValue<array<int, TValue>>
*/
public function chunk(int $size): ArrayValue;
<?php
use GW\Value\Wrap;
$array = Wrap::array([1, 2, 3, 4, 3, 4, 5, 6, 7, 8, 9]);
var_export($array->chunk(3)->toArray());
array (
0 =>
array (
0 => 1,
1 => 2,
2 => 3,
),
1 =>
array (
0 => 4,
1 => 3,
2 => 4,
),
2 =>
array (
0 => 5,
1 => 6,
2 => 7,
),
3 =>
array (
0 => 8,
1 => 9,
),
)
<?php
/**
* @param callable(TValue,TValue):int $comparator
* @phpstan-return ArrayValue<TValue>
*/
public function sort(callable $comparator): ArrayValue;
Create new ArrayValue
with items sorted by callback.
<?php
use GW\Value\Wrap;
use GW\Value\Sorts;
$array = Wrap::array(['c', 'a', 'b']);
$customSort = $array->sort(function (string $a, string $b): int {
return $a <=> $b;
});
$ascending = $array->sort(Sorts::asc());
$descending = $array->sort(Sorts::desc());
echo 'customSort = ';
var_export($customSort->toArray());
echo PHP_EOL;
echo 'ascending = ';
var_export($ascending->toArray());
echo PHP_EOL;
echo 'descending = ';
var_export($descending->toArray());
customSort = array (
0 => 'a',
1 => 'b',
2 => 'c',
)
ascending = array (
0 => 'a',
1 => 'b',
2 => 'c',
)
descending = array (
0 => 'c',
1 => 'b',
2 => 'a',
)
<?php
/**
* @phpstan-return ArrayValue<TValue>
*/
public function shuffle(): ArrayValue;
<?php
use GW\Value\Wrap;
$words = Wrap::array(['do', 'or', 'do', 'not', 'there', 'is', 'no', 'try']);
echo $words->shuffle()->implode(' ')->toString();
no or there do try do not is
<?php
/**
* @phpstan-return ArrayValue<TValue>
*/
public function reverse(): ArrayValue;
<?php
use GW\Value\Wrap;
$words = Wrap::array(['do', 'or', 'do', 'not', 'there', 'is', 'no', 'try']);
echo $words->reverse()->implode(' ')->toString();
try no is there not do or do
<?php
/**
* @phpstan-param TValue $value
* @phpstan-return ArrayValue<TValue>
*/
public function unshift($value): ArrayValue;
<?php
use GW\Value\Wrap;
$words = Wrap::array(['a', 'b', 'c']);
var_export($words->unshift('X')->toArray());
array (
0 => 'X',
1 => 'a',
2 => 'b',
3 => 'c',
)
<?php
/**
* @phpstan-param TValue $value
* @phpstan-return ArrayValue<TValue>
*/
public function shift(&$value = null): ArrayValue;
<?php
use GW\Value\Wrap;
$words = Wrap::array(['a', 'b', 'c']);
var_export($words->shift($x)->toArray());
echo PHP_EOL;
echo 'x: ' . $x;
array (
0 => 'b',
1 => 'c',
)
x: a
<?php
/**
* @phpstan-param TValue $value
* @phpstan-return ArrayValue<TValue>
*/
public function push($value): ArrayValue;
<?php
use GW\Value\Wrap;
$words = Wrap::array(['a', 'b', 'c']);
var_export($words->push('X')->toArray());
array (
0 => 'a',
1 => 'b',
2 => 'c',
3 => 'X',
)
<?php
/**
* @phpstan-param TValue|null $value
* @phpstan-return ArrayValue<TValue>
*/
public function pop(&$value = null): ArrayValue;
<?php
use GW\Value\Wrap;
$words = Wrap::array(['a', 'b', 'c']);
var_export($words->pop($x)->toArray());
echo PHP_EOL;
echo 'x: ' . $x;
array (
0 => 'a',
1 => 'b',
)
x: c
<?php
/**
* @param int $offset
*/
public function offsetExists($offset): bool;
<?php
/**
* @param int $offset
* @phpstan-return TValue
*/
public function offsetGet($offset);
<?php
/**
* @param int $offset
* @phpstan-param TValue $value
* @throws BadMethodCallException For immutable types.
*/
public function offsetSet($offset, $value): void;
<?php
/**
* @param int $offset
* @return void
* @throws BadMethodCallException For immutable types.
*/
public function offsetUnset($offset): void;
<?php
/**
* @phpstan-param ArrayValue<TValue> $other
* @phpstan-return ArrayValue<TValue>
*/
public function join(ArrayValue $other): ArrayValue;
<?php
use GW\Value\Wrap;
$one = Wrap::array(['a', 'b', 'c']);
$two = Wrap::array(['d', 'e', 'f']);
var_export($one->join($two)->toArray());
array (
0 => 'a',
1 => 'b',
2 => 'c',
3 => 'd',
4 => 'e',
5 => 'f',
)
<?php
/**
* @phpstan-return ArrayValue<TValue>
*/
public function slice(int $offset, ?int $length = null): ArrayValue;
<?php
use GW\Value\Wrap;
$letters = Wrap::array(['a', 'b', 'c', 'd', 'e', 'f', 'g']);
var_export($letters->slice(2, 4)->toArray());
echo PHP_EOL;
var_export($letters->slice(-1, 1)->toArray());
echo PHP_EOL;
var_export($letters->slice(0, 3)->toArray());
echo PHP_EOL;
var_export($letters->slice(0, 100)->toArray());
echo PHP_EOL;
array (
0 => 'c',
1 => 'd',
2 => 'e',
3 => 'f',
)
array (
0 => 'g',
)
array (
0 => 'a',
1 => 'b',
2 => 'c',
)
array (
0 => 'a',
1 => 'b',
2 => 'c',
3 => 'd',
4 => 'e',
5 => 'f',
6 => 'g',
)
<?php
/**
* @phpstan-return ArrayValue<TValue>
*/
public function skip(int $length): ArrayValue;
<?php
use GW\Value\Wrap;
$letters = Wrap::array(['a', 'b', 'c', 'd', 'e', 'f', 'g']);
var_export($letters->skip(2)->toArray());
echo PHP_EOL;
array (
0 => 'c',
1 => 'd',
2 => 'e',
3 => 'f',
4 => 'g',
)
<?php
/**
* @phpstan-return ArrayValue<TValue>
*/
public function take(int $length): ArrayValue;
<?php
use GW\Value\Wrap;
$letters = Wrap::array(['a', 'b', 'c', 'd', 'e', 'f', 'g']);
var_export($letters->skip(2)->take(4)->toArray());
echo PHP_EOL;
var_export($letters->take(3)->toArray());
echo PHP_EOL;
var_export($letters->take(100)->toArray());
echo PHP_EOL;
array (
0 => 'c',
1 => 'd',
2 => 'e',
3 => 'f',
)
array (
0 => 'a',
1 => 'b',
2 => 'c',
)
array (
0 => 'a',
1 => 'b',
2 => 'c',
3 => 'd',
4 => 'e',
5 => 'f',
6 => 'g',
)
<?php
/**
* @phpstan-param ArrayValue<TValue> $replacement
* @phpstan-return ArrayValue<TValue>
*/
public function splice(int $offset, int $length, ?ArrayValue $replacement = null): ArrayValue;
Remove or replace slice of ArrayValue
items.
<?php
use GW\Value\Wrap;
$letters = Wrap::array(['a', 'b', 'c', 'd', 'e', 'f', 'g']);
var_export($letters->splice(2, 4)->toArray());
echo PHP_EOL;
var_export($letters->splice(2, 4, Wrap::array(['x', 'y', 'z']))->toArray());
echo PHP_EOL;
var_export($letters->splice(-1, 1)->toArray());
echo PHP_EOL;
var_export($letters->splice(-1, 1, Wrap::array(['x', 'y']))->toArray());
echo PHP_EOL;
var_export($letters->splice(0, 3)->toArray());
echo PHP_EOL;
var_export($letters->splice(0, 100)->toArray());
echo PHP_EOL;
array (
0 => 'a',
1 => 'b',
2 => 'g',
)
array (
0 => 'a',
1 => 'b',
2 => 'x',
3 => 'y',
4 => 'z',
5 => 'g',
)
array (
0 => 'a',
1 => 'b',
2 => 'c',
3 => 'd',
4 => 'e',
5 => 'f',
)
array (
0 => 'a',
1 => 'b',
2 => 'c',
3 => 'd',
4 => 'e',
5 => 'f',
6 => 'x',
7 => 'y',
)
array (
0 => 'd',
1 => 'e',
2 => 'f',
3 => 'g',
)
array (
)
<?php
/**
* @phpstan-param ArrayValue<TValue> $other
* @param (callable(TValue,TValue):int)|null $comparator
* @phpstan-return ArrayValue<TValue>
*/
public function diff(ArrayValue $other, ?callable $comparator = null): ArrayValue;
<?php
use GW\Value\Wrap;
$one = Wrap::array(['a', 'b', 'c', 'd', 'e', 'f', 'g']);
$two = Wrap::array(['c', 'd', 'e', 'F']);
var_export($one->diff($two)->toArray());
echo PHP_EOL;
$lowercaseComparator = function(string $a, string $b): int {
return mb_strtolower($a) <=> mb_strtolower($b);
};
var_export($one->diff($two, $lowercaseComparator)->toArray());
array (
0 => 'a',
1 => 'b',
2 => 'f',
3 => 'g',
)
array (
0 => 'a',
1 => 'b',
2 => 'g',
)
<?php
/**
* @phpstan-param ArrayValue<TValue> $other
* @param (callable(TValue,TValue):int)|null $comparator
* @phpstan-return ArrayValue<TValue>
*/
public function intersect(ArrayValue $other, ?callable $comparator = null): ArrayValue;
<?php
use GW\Value\Wrap;
$one = Wrap::array(['a', 'b', 'c', 'd', 'e', 'f', 'g']);
$two = Wrap::array(['c', 'd', 'e', 'F']);
var_export($one->intersect($two)->toArray());
$lowercaseComparator = function(string $a, string $b): int {
return mb_strtolower($a) <=> mb_strtolower($b);
};
var_export($one->intersect($two, $lowercaseComparator)->toArray());
array (
0 => 'c',
1 => 'd',
2 => 'e',
)array (
0 => 'c',
1 => 'd',
2 => 'e',
3 => 'f',
)
<?php
/**
* @template TNewValue
* @param callable(TNewValue, TValue):TNewValue $transformer
* @phpstan-param TNewValue $start
* @phpstan-return TNewValue
*/
public function reduce(callable $transformer, $start);
<?php
use GW\Value\Wrap;
$prices = Wrap::array([10, 20, 50, 120]);
$summarize = function(int $sum, int $price): int {
return $sum + $price;
};
echo 'Sum: ' . $prices->reduce($summarize, 0);
echo PHP_EOL;
$list = function(string $list, int $price): string {
return $list . " €{$price},-";
};
echo 'Prices: ' . $prices->reduce($list, '');
echo PHP_EOL;
Sum: 200
Prices: €10,- €20,- €50,- €120,-
<?php
public function implode(string $glue): StringValue;
<?php
use GW\Value\Wrap;
$prices = Wrap::array(['a', 'b', 'c', 'd']);
echo $prices->implode(' / ')->toString();
a / b / c / d
<?php
/**
* @phpstan-return ArrayValue<TValue>
*/
public function notEmpty(): ArrayValue;
<?php
/**
* @phpstan-return AssocValue<int, TValue>
*/
public function toAssocValue(): AssocValue;
<?php
use GW\Value\Wrap;
$array = Wrap::array(['a', 'b', 'c', 'd', 'e', 'f', 'g']);
var_export(
$array->toAssocValue()
->mapKeys(function (int $oldKey, string $value): string {
return "{$oldKey}:{$value}";
})
->toAssocArray()
);
array (
'0:a' => 'a',
'1:b' => 'b',
'2:c' => 'c',
'3:d' => 'd',
'4:e' => 'e',
'5:f' => 'f',
'6:g' => 'g',
)
<?php
public function toStringsArray(): StringsArray;
<?php
public function isEmpty(): bool;
<?php
use GW\Value\Wrap;
echo "['a']: ";
var_export(Wrap::array(['a'])->isEmpty());
echo PHP_EOL;
echo "[]: ";
var_export(Wrap::array([])->isEmpty());
echo PHP_EOL;
['a']: false
[]: true
<?php
/**
* @phpstan-return TValue|null
*/
public function first();
<?php
use GW\Value\Wrap;
$array = Wrap::array(['a', 'b', 'c']);
echo 'array first: ' . $array->first();
array first: a
<?php
/**
* @phpstan-return TValue|null
*/
public function last();
<?php
use GW\Value\Wrap;
$array = Wrap::array(['a', 'b', 'c']);
echo 'array last: ' . $array->last();
array last: c
<?php
/**
* @param callable(TValue $value):bool $filter
* @phpstan-return TValue|null
*/
public function find(callable $filter);
<?php
/**
* @param callable(TValue $value): bool $filter
* @phpstan-return TValue|null
*/
public function findLast(callable $filter);
<?php
/**
* @phpstan-param TValue $element
*/
public function hasElement($element): bool;
<?php
/**
* @param callable(TValue $value):bool $filter
*/
public function any(callable $filter): bool;
<?php
/**
* @param callable(TValue $value):bool $filter
*/
public function every(callable $filter): bool;
<?php
public function count(): int;
<?php
use GW\Value\Wrap;
$array = Wrap::array(['a', 'b', 'c']);
echo 'count: ' . $array->count();
count: 3
(definition not available)
AssocValue
defines interface of value object for associative array.
<?php
use GW\Value\Wrap;
$array = Wrap::assocArray(['a' => 1, 'b' => 2, 'c' => 3]);
$array['a']; // 1
$array[0]; // Exception
$array['d']; // Exception
$has = isset($array['a']); // true
$has = isset($array['d']); // false
$array->count(); // 3
foreach ($array as $key => $value) {
echo "$key => $value ";
} // outputs: a => 1 b => 2 c => 3
<?php
/**
* @phpstan-param callable(TValue, TKey $key):void $callback
* @phpstan-return AssocValue<TKey, TValue>
*/
public function each(callable $callback): AssocValue;
<?php
/**
* @phpstan-param (callable(TValue,TValue):int)|null $comparator
* @phpstan-return AssocValue<TKey, TValue>
*/
public function unique(?callable $comparator = null): AssocValue;
<?php
/**
* @phpstan-param callable(TValue):bool $filter
* @phpstan-return AssocValue<TKey, TValue>
*/
public function filter(callable $filter): AssocValue;
<?php
/**
* @phpstan-return AssocValue<TKey, TValue>
*/
public function filterEmpty(): AssocValue;
<?php
/**
* @template TNewValue
* @param callable(TValue,TKey $key):TNewValue $transformer
* @phpstan-return AssocValue<TKey, TNewValue>
*/
public function map(callable $transformer): AssocValue;
<?php
/**
* @param callable(TValue,TValue):int $comparator
* @phpstan-return AssocValue<TKey, TValue>
*/
public function sort(callable $comparator): AssocValue;
<?php
/**
* @phpstan-return AssocValue<TKey, TValue>
*/
public function shuffle(): AssocValue;
<?php
/**
* @phpstan-return AssocValue<TKey, TValue>
*/
public function reverse(): AssocValue;
<?php
/**
* @phpstan-param TKey $offset
*/
public function offsetExists($offset): bool;
<?php
/**
* @phpstan-param TKey $offset
* @return ?TValue
*/
public function offsetGet($offset);
<?php
/**
* @phpstan-param TKey $offset
* @phpstan-param TValue $value
* @throws BadMethodCallException For immutable types.
*/
public function offsetSet($offset, $value): void;
<?php
/**
* @phpstan-param TKey $offset
* @throws BadMethodCallException For immutable types.
*/
public function offsetUnset($offset): void;
<?php
/**
* @phpstan-return array<TKey, TValue>
*/
public function toAssocArray(): array;
<?php
/**
* @phpstan-return ArrayValue<TKey>
*/
public function keys(): ArrayValue;
<?php
use GW\Value\Wrap;
$assoc = Wrap::assocArray(['0' => 'zero', '1' => 'one']);
$keys = $assoc
->map(fn(string $val, int $key): string => $val)
->keys()
->toArray();
var_export($keys);
array (
0 => 0,
1 => 1,
)
<?php
/**
* @phpstan-return ArrayValue<TValue>
*/
public function values(): ArrayValue;
<?php
/**
* @template TNewKey of int|string
* @phpstan-param callable(TKey $key, TValue $value): TNewKey $transformer
* @phpstan-return AssocValue<TNewKey, TValue>
*/
public function mapKeys(callable $transformer): AssocValue;
<?php
/**
* @phpstan-param callable(TKey $keyA, TKey $keyB): int $comparator
* @phpstan-return AssocValue<TKey, TValue>
*/
public function sortKeys(callable $comparator): AssocValue;
<?php
/**
* @phpstan-param TKey $key
* @phpstan-param TValue $value
* @phpstan-return AssocValue<TKey, TValue>
*/
public function with($key, $value): AssocValue;
<?php
/**
* @phpstan-param TKey ...$keys
* @phpstan-return AssocValue<TKey, TValue>
*/
public function without(...$keys): AssocValue;
<?php
/**
* @param TKey ...$keys
* @phpstan-return AssocValue<TKey, TValue>
*/
public function only(...$keys): AssocValue;
<?php
/**
* @phpstan-param TValue $value
* @phpstan-return AssocValue<TKey, TValue>
*/
public function withoutElement($value): AssocValue;
<?php
/**
* @deprecated use join() or replace() instead
* @phpstan-param AssocValue<TKey, TValue> $other
* @phpstan-return AssocValue<TKey, TValue>
*/
public function merge(AssocValue $other): AssocValue;
<?php
/**
* Joins other AssocValue by adding the keys from other not present in self
*
* @phpstan-param AssocValue<TKey, TValue> $other
* @phpstan-return AssocValue<TKey, TValue>
*/
public function join(AssocValue $other): AssocValue;
<?php
use GW\Value\Wrap;
$one = Wrap::assocArray(['a' => 1, 'b' => 2, 'c' => 3]);
$two = Wrap::assocArray(['c' => 5, 'd' => 4]);
var_export($one->join($two)->toAssocArray());
array (
'a' => 1,
'b' => 2,
'c' => 3,
'd' => 4,
)
<?php
/**
* Joins other AssocValue by replacing values in self of the same keys from other
*
* @phpstan-param AssocValue<TKey, TValue> $other
* @phpstan-return AssocValue<TKey, TValue>
*/
public function replace(AssocValue $other): AssocValue;
<?php
use GW\Value\Wrap;
$one = Wrap::assocArray(['a' => 1, 'b' => 2, 'c' => 3]);
$two = Wrap::assocArray(['c' => 5, 'd' => 4]);
var_export($one->replace($two)->toAssocArray());
array (
'a' => 1,
'b' => 2,
'c' => 5,
'd' => 4,
)
<?php
/**
* @template TNewValue
* @param callable(TNewValue $reduced, TValue $value, string $key):TNewValue $transformer
* @phpstan-param TNewValue $start
* @phpstan-return TNewValue
*/
public function reduce(callable $transformer, $start);
<?php
/**
* @phpstan-param TKey $key
* @phpstan-param ?TValue $default
* @phpstan-return ?TValue
*/
public function get($key, $default = null);
<?php
/**
* @phpstan-param TKey $key
*/
public function has($key): bool;
<?php
public function isEmpty(): bool;
<?php
/**
* @phpstan-return TValue|null
*/
public function first();
<?php
use GW\Value\Wrap;
$assoc = Wrap::assocArray(['a' => 1, 'b' => 2, 'c' => 3]);
echo 'assoc first: ' . $assoc->first();
assoc first: 1
<?php
/**
* @phpstan-return TValue|null
*/
public function last();
<?php
use GW\Value\Wrap;
$assoc = Wrap::assocArray(['a' => 1, 'b' => 2, 'c' => 3]);
echo 'assoc last: ' . $assoc->last();
assoc last: 3
<?php
/**
* @param callable(TValue $value):bool $filter
* @phpstan-return TValue|null
*/
public function find(callable $filter);
<?php
/**
* @param callable(TValue $value): bool $filter
* @phpstan-return TValue|null
*/
public function findLast(callable $filter);
<?php
/**
* @phpstan-param TValue $element
*/
public function hasElement($element): bool;
<?php
/**
* @param callable(TValue $value):bool $filter
*/
public function any(callable $filter): bool;
<?php
/**
* @param callable(TValue $value):bool $filter
*/
public function every(callable $filter): bool;
<?php
/**
* @phpstan-return array<int, TValue>
*/
public function toArray(): array;
<?php
public function count(): int;
(definition not available)
<?php
/**
* @param callable(string $value):(StringValue|string) $transformer
* @return StringValue
*/
public function transform(callable $transformer): StringValue;
<?php
/**
* @return StringValue
*/
public function stripTags(): StringValue;
<?php
use GW\Value\Wrap;
$html = Wrap::string('<p>Html is <strong>cool</strong> but not always...</p>');
echo $html->stripTags()->toString();
Html is cool but not always...
<?php
/**
* @return StringValue
* @param string|StringValue $characterMask
*/
public function trim($characterMask = self::TRIM_MASK): StringValue;
<?php
use GW\Value\Wrap;
$text = Wrap::string(' :.: I ♡ SPACE :.: ');
echo $text->trim()->toString() . PHP_EOL;
echo $text->trim(' .:')->toString() . PHP_EOL;
:.: I ♡ SPACE :.:
I ♡ SPACE
<?php
/**
* @return StringValue
* @param string|StringValue $characterMask
*/
public function trimRight($characterMask = self::TRIM_MASK): StringValue;
<?php
use GW\Value\Wrap;
$text = Wrap::string(' :.: I ♡ SPACE :.: ');
echo $text->trimRight()->toString() . PHP_EOL;
echo $text->trimRight(' .:')->toString() . PHP_EOL;
:.: I ♡ SPACE :.:
:.: I ♡ SPACE
<?php
/**
* @return StringValue
* @param string|StringValue $characterMask
*/
public function trimLeft($characterMask = self::TRIM_MASK): StringValue;
<?php
use GW\Value\Wrap;
$text = Wrap::string(' :.: I ♡ SPACE :.: ');
echo $text->trimLeft()->toString() . PHP_EOL;
echo $text->trimLeft(' .:')->toString() . PHP_EOL;
:.: I ♡ SPACE :.:
I ♡ SPACE :.:
<?php
/**
* @return StringValue
*/
public function lower(): StringValue;
<?php
use GW\Value\Wrap;
$text = Wrap::string('SOMETIMES I WANNA SCREAM!');
echo $text->lower()->toString() . PHP_EOL;
sometimes i wanna scream!
<?php
/**
* @return StringValue
*/
public function upper(): StringValue;
<?php
use GW\Value\Wrap;
$text = Wrap::string('it`s so quiet...');
echo $text->upper()->toString() . PHP_EOL;
IT`S SO QUIET...
<?php
/**
* @return StringValue
*/
public function lowerFirst(): StringValue;
<?php
use GW\Value\Wrap;
$text = Wrap::string('CamelCaseMethod()');
echo $text->lowerFirst()->toString() . PHP_EOL;
camelCaseMethod()
<?php
/**
* @return StringValue
*/
public function upperFirst(): StringValue;
<?php
use GW\Value\Wrap;
$text = Wrap::string('words don`t come easy');
echo $text->upperFirst()->toString() . PHP_EOL;
Words don`t come easy
<?php
/**
* @return StringValue
*/
public function upperWords(): StringValue;
<?php
use GW\Value\Wrap;
$html = Wrap::string('words don`t come easy');
echo $html->upperWords()->toString() . PHP_EOL;
Words Don`t Come Easy
<?php
/**
* @return StringValue
* @param string|StringValue $string
*/
public function padRight(int $length, $string = ' '): StringValue;
<?php
use GW\Value\Wrap;
$text = Wrap::string('cut here ☞');
echo $text->padRight(16, '-')->toString();
cut here ☞----
<?php
/**
* @return StringValue
* @param string|StringValue $string
*/
public function padLeft(int $length, $string = ' '): StringValue;
<?php
use GW\Value\Wrap;
$text = Wrap::string('☜ cut here');
echo $text->padLeft(16, '-')->toString();
----☜ cut here
<?php
/**
* @return StringValue
* @param string|StringValue $string
*/
public function padBoth(int $length, $string = ' '): StringValue;
<?php
use GW\Value\Wrap;
$text = Wrap::string('☜ cut here ☞');
echo $text->padBoth(24, '-')->toString();
----☜ cut here ☞----
<?php
/**
* @return StringValue
* @param string|StringValue $search
* @param string|StringValue $replace
*/
public function replace($search, $replace): StringValue;
<?php
use GW\Value\Wrap;
$text = Wrap::string('My favourite color is pink!');
echo $text->replace('pink', 'blue')->toString();
My favourite color is blue!
<?php
/**
* @return StringValue
* @param array<int,string>|ArrayValue<string> $search
* @param string|StringValue $replace
*/
public function replaceAll($search, $replace): StringValue;
<?php
use GW\Value\Wrap;
$text = Wrap::string('Your favourite colors are red and black');
echo $text->replaceAll(['red', 'black'], 'blue')->toString();
Your favourite colors are blue and blue
<?php
/**
* @return StringValue
* @param array<string,string>|AssocValue<string,string> $pairs
*/
public function replacePairs($pairs): StringValue;
<?php
use GW\Value\Wrap;
$text = Wrap::string('Your favourite colors are red and black');
echo $text->replacePairs(['red' => 'orange', 'black' => 'white'])->toString();
Your favourite colors are orange and white
<?php
/**
* @return StringValue
* @param string|StringValue $pattern
* @param string|StringValue $replacement
*/
public function replacePattern($pattern, $replacement): StringValue;
<?php
use GW\Value\Wrap;
$text = Wrap::string('You are looking good! Really!');
echo $text->replacePattern('/[aeiouy]/', '')->toString();
Y r lkng gd! Rll!
<?php
/**
* @return StringValue
* @param string|StringValue $pattern
*/
public function replacePatternCallback($pattern, callable $callback): StringValue;
<?php
use GW\Value\Wrap;
$text = Wrap::string('You are looking good! Really!');
$replacer = function(array $matches): string {
$vowel = $matches[0];
switch ($vowel) {
case 'a':
return 'o';
case 'o':
return 'a';
default:
return 'i';
}
};
echo $text->replacePatternCallback('/[aeiouy]/', $replacer)->toString();
Yai ori laaking gaad! Riolli!
<?php
/**
* @return StringValue
* @param string|StringValue $postfix
*/
public function truncate(int $length, $postfix = '...'): StringValue;
<?php
use GW\Value\Wrap;
echo Wrap::string('It`s Short')->truncate(10)->toString() . PHP_EOL;
echo Wrap::string('This one is too long!')->truncate(10)->toString() . PHP_EOL;
echo Wrap::string('This one is too long!')->truncate(10, '+')->toString() . PHP_EOL;
It`s Short
This one i...
This one i+
<?php
/**
* @return StringValue
*/
public function substring(int $start, ?int $length = null): StringValue;
<?php
/**
* @return StringValue
* @param string|StringValue $other
*/
public function postfix($other): StringValue;
<?php
/**
* @return StringValue
* @param string|StringValue $other
*/
public function prefix($other): StringValue;
<?php
public function length(): int;
<?php
/**
* @param string|StringValue $needle
*/
public function position($needle): ?int;
<?php
/**
* @param string|StringValue $needle
*/
public function positionLast($needle): ?int;
<?php
/**
* @param string|StringValue $pattern
* @return ArrayValue<array<int, string>>
*/
public function matchAllPatterns($pattern): ArrayValue;
<?php
/**
* @param string|StringValue $pattern
* @return StringsArray
*/
public function matchPatterns($pattern): StringsArray;
<?php
/**
* @param string|StringValue $pattern
*/
public function isMatching($pattern): bool;
<?php
/**
* @param string|StringValue $pattern
*/
public function startsWith($pattern): bool;
<?php
/**
* @param string|StringValue $pattern
*/
public function endsWith($pattern): bool;
<?php
/**
* @return StringsArray
* @param string|StringValue $pattern
*/
public function splitByPattern($pattern): StringsArray;
<?php
/**
* @return StringsArray
* @param string|StringValue $delimiter
*/
public function explode($delimiter): StringsArray;
<?php
/**
* @param string|StringValue $substring
*/
public function contains($substring): bool;
<?php
public function toString(): string;
<?php
public function __toString(): string;
<?php
public function isEmpty(): bool;
<?php
use GW\Value\Wrap;
echo "'a': ";
var_export(Wrap::string('a')->isEmpty());
echo PHP_EOL;
echo "'': ";
var_export(Wrap::string('')->isEmpty());
echo PHP_EOL;
'a': false
'': true
<?php
/**
* @param callable(StringValue $value): void $callback
*/
public function each(callable $callback): StringsArray;
<?php
/**
* @param callable(StringValue):bool $filter
*/
public function any(callable $filter): bool;
<?php
/**
* @param callable(StringValue):bool $filter
*/
public function every(callable $filter): bool;
<?php
/**
* @param (callable(StringValue, StringValue):int)|null $comparator
*/
public function unique(?callable $comparator = null): StringsArray;
<?php
/**
* @return array<int, StringValue>
*/
public function toArray(): array;
<?php
/**
* @return string[]
*/
public function toNativeStrings(): array;
<?php
/**
* @param callable(StringValue):bool $filter
*/
public function filter(callable $filter): StringsArray;
<?php
public function filterEmpty(): StringsArray;
<?php
/**
* @param callable(StringValue):StringValue $transformer
*/
public function map(callable $transformer): StringsArray;
<?php
/**
* @param callable(StringValue):iterable<StringValue> $transformer
*/
public function flatMap(callable $transformer): StringsArray;
<?php
/**
* @template TNewKey of int|string
* @param callable(StringValue):TNewKey $reducer
* @phpstan-return AssocValue<TNewKey, StringsArray>
*/
public function groupBy(callable $reducer): AssocValue;
<?php
use GW\Value\StringsArray;
use GW\Value\StringValue;
use GW\Value\Wrap;
$text = 'I would like to ask a question about the meaning of life';
$stopwords = ['i', 'to', 'a', 'the', 'of'];
$wordGroups = Wrap::string($text)
->lower()
->explode(' ')
->groupBy(function (StringValue $word) use ($stopwords): string {
return in_array($word->toString(), $stopwords, true) ? 'stopwords' : 'words';
});
/** @var StringsArray $stopwords */
$stopwords = $wordGroups->get('stopwords');
echo 'stopwords: ', $stopwords->implode(', ')->toString(), PHP_EOL;
/** @var StringsArray $words */
$words = $wordGroups->get('words');
echo 'words: ', $words->implode(', ')->toString(), PHP_EOL;
stopwords: i, to, a, the, of
words: would, like, ask, question, about, meaning, life
<?php
/**
* @param callable(StringValue,StringValue):int $comparator
*/
public function sort(callable $comparator): StringsArray;
<?php
public function shuffle(): StringsArray;
<?php
public function reverse(): StringsArray;
<?php
/**
* @param StringValue|string $value
*/
public function unshift($value): StringsArray;
<?php
/**
* @param StringValue|null $value
*/
public function shift(&$value = null): StringsArray;
<?php
/**
* @param StringValue|string $value
*/
public function push($value): StringsArray;
<?php
/**
* @param StringValue|null $value
*/
public function pop(&$value = null): StringsArray;
<?php
/**
* @param int $offset
*/
public function offsetExists($offset): bool;
<?php
/**
* @param int $offset
*/
public function offsetGet($offset): StringValue;
<?php
/**
* @param int $offset
* @param StringValue|string $value
* @throws BadMethodCallException For immutable types.
*/
public function offsetSet($offset, $value): void;
<?php
/**
* @param int $offset
* @throws BadMethodCallException For immutable types.
*/
public function offsetUnset($offset): void;
<?php
public function join(StringsArray $other): StringsArray;
<?php
public function slice(int $offset, ?int $length = null): StringsArray;
<?php
public function skip(int $length): StringsArray;
<?php
public function take(int $length): StringsArray;
<?php
public function splice(int $offset, int $length, ?StringsArray $replacement = null): StringsArray;
<?php
/**
* @param (callable(StringValue, StringValue):int)|null $comparator
*/
public function diff(StringsArray $other, ?callable $comparator = null): StringsArray;
<?php
/**
* @param (callable(StringValue, StringValue):int)|null $comparator
*/
public function intersect(StringsArray $other, ?callable $comparator = null): StringsArray;
<?php
/**
* @template TNewValue
* @phpstan-param callable(TNewValue, StringValue):TNewValue $transformer
* @phpstan-param TNewValue $start
* @phpstan-return TNewValue
*/
public function reduce(callable $transformer, $start);
<?php
public function implode(string $glue): StringValue;
<?php
/**
* @return StringsArray
*/
public function notEmpty(): StringsArray;
<?php
/**
* @return StringValue|null
*/
public function first(): ?StringValue;
<?php
/**
* @return StringValue|null
*/
public function last(): ?StringValue;
<?php
/**
* @param callable(StringValue):bool $filter
*/
public function find(callable $filter): ?StringValue;
<?php
/**
* @param callable(StringValue):bool $filter
*/
public function findLast(callable $filter): ?StringValue;
<?php
/**
* @param callable(string):(StringValue|string) $transformer
*/
public function transform(callable $transformer): StringsArray;
<?php
public function stripTags(): StringsArray;
<?php
use GW\Value\Wrap;
$text = Wrap::stringsArray(['<h1>Story</h1>', '<h2>Chapter 1</h2>', '<p>Once upon a time...</p>']);
var_export($text->stripTags()->toArray());
array (
0 =>
GW\Value\PlainString::__set_state(array(
'value' => 'Story',
)),
1 =>
GW\Value\PlainString::__set_state(array(
'value' => 'Chapter 1',
)),
2 =>
GW\Value\PlainString::__set_state(array(
'value' => 'Once upon a time...',
)),
)
<?php
/**
* @param string|StringValue $characterMask
*/
public function trim($characterMask = self::TRIM_MASK): StringsArray;
<?php
/**
* @param string|StringValue $characterMask
*/
public function trimRight($characterMask = self::TRIM_MASK): StringsArray;
<?php
/**
* @param string|StringValue $characterMask
*/
public function trimLeft($characterMask = self::TRIM_MASK): StringsArray;
<?php
public function lower(): StringsArray;
<?php
use GW\Value\Wrap;
$text = Wrap::stringsArray(['SOMETIMES', 'I', 'WANNA', 'SCREAM!']);
var_export($text->lower()->toArray());
array (
0 =>
GW\Value\PlainString::__set_state(array(
'value' => 'sometimes',
)),
1 =>
GW\Value\PlainString::__set_state(array(
'value' => 'i',
)),
2 =>
GW\Value\PlainString::__set_state(array(
'value' => 'wanna',
)),
3 =>
GW\Value\PlainString::__set_state(array(
'value' => 'scream!',
)),
)
<?php
public function upper(): StringsArray;
<?php
use GW\Value\Wrap;
$text = Wrap::stringsArray(['it`s so quiet', 'and peaceful']);
var_export($text->upper()->toArray());
array (
0 =>
GW\Value\PlainString::__set_state(array(
'value' => 'IT`S SO QUIET',
)),
1 =>
GW\Value\PlainString::__set_state(array(
'value' => 'AND PEACEFUL',
)),
)
<?php
public function lowerFirst(): StringsArray;
<?php
use GW\Value\Wrap;
$text = Wrap::stringsArray(['CamelCaseMethod', 'AnotherCamel']);
var_export($text->lowerFirst()->toArray());
array (
0 =>
GW\Value\PlainString::__set_state(array(
'value' => 'camelCaseMethod',
)),
1 =>
GW\Value\PlainString::__set_state(array(
'value' => 'anotherCamel',
)),
)
<?php
public function upperFirst(): StringsArray;
<?php
use GW\Value\Wrap;
$text = Wrap::stringsArray(['it`s so quiet', 'and peaceful']);
var_export($text->upperFirst()->toString());
'It`s so quiet And peaceful'
<?php
public function upperWords(): StringsArray;
<?php
use GW\Value\Wrap;
$text = Wrap::stringsArray(['it`s so quiet', 'and peaceful']);
var_export($text->upperWords()->toString());
'It`s So Quiet And Peaceful'
<?php
/**
* @param string|StringValue $string
*/
public function padRight(int $length, $string = ' '): StringsArray;
<?php
/**
* @param string|StringValue $string
*/
public function padLeft(int $length, $string = ' '): StringsArray;
<?php
use GW\Value\Wrap;
$text = Wrap::stringsArray(['one', 'two', 'three']);
var_export($text->padLeft(16, '-')->toArray());
array (
0 =>
GW\Value\PlainString::__set_state(array(
'value' => '-------------one',
)),
1 =>
GW\Value\PlainString::__set_state(array(
'value' => '-------------two',
)),
2 =>
GW\Value\PlainString::__set_state(array(
'value' => '-----------three',
)),
)
<?php
/**
* @param string|StringValue $string
*/
public function padBoth(int $length, $string = ' '): StringsArray;
<?php
use GW\Value\Wrap;
$text = Wrap::stringsArray(['one', 'two', 'three']);
var_export($text->padBoth(24, '-')->toArray());
array (
0 =>
GW\Value\PlainString::__set_state(array(
'value' => '----------one-----------',
)),
1 =>
GW\Value\PlainString::__set_state(array(
'value' => '----------two-----------',
)),
2 =>
GW\Value\PlainString::__set_state(array(
'value' => '---------three----------',
)),
)
<?php
/**
* @param string|StringValue $search
* @param string|StringValue $replace
*/
public function replace($search, $replace): StringsArray;
<?php
use GW\Value\Wrap;
$text = Wrap::stringsArray(['One.', 'Two.', 'Three.']);
var_export($text->replace('.', '!!!')->toArray());
array (
0 =>
GW\Value\PlainString::__set_state(array(
'value' => 'One!!!',
)),
1 =>
GW\Value\PlainString::__set_state(array(
'value' => 'Two!!!',
)),
2 =>
GW\Value\PlainString::__set_state(array(
'value' => 'Three!!!',
)),
)
<?php
/**
* @return StringsArray
* @param array<int,string>|ArrayValue<string> $search
* @param string|StringValue $replace
*/
public function replaceAll($search, $replace): StringsArray;
<?php
use GW\Value\Wrap;
$text = Wrap::stringsArray(['One?', 'Two!', 'Three!?']);
var_export($text->replaceAll(['?', '!'], '.')->toArray());
array (
0 =>
GW\Value\PlainString::__set_state(array(
'value' => 'One.',
)),
1 =>
GW\Value\PlainString::__set_state(array(
'value' => 'Two.',
)),
2 =>
GW\Value\PlainString::__set_state(array(
'value' => 'Three..',
)),
)
<?php
/**
* @return StringsArray
* @param array<string,string>|AssocValue<string,string> $pairs
*/
public function replacePairs($pairs): StringsArray;
<?php
use GW\Value\Wrap;
$text = Wrap::stringsArray(['One?', 'Two!', 'Three!?']);
var_export($text->replacePairs(['One' => 'Eleven', 'Two' => 'Twelve'])->toArray());
array (
0 =>
GW\Value\PlainString::__set_state(array(
'value' => 'Eleven?',
)),
1 =>
GW\Value\PlainString::__set_state(array(
'value' => 'Twelve!',
)),
2 =>
GW\Value\PlainString::__set_state(array(
'value' => 'Three!?',
)),
)
<?php
/**
* @param string|StringValue $pattern
* @param string|StringValue $replacement
*/
public function replacePattern($pattern, $replacement): StringsArray;
<?php
use GW\Value\Wrap;
$text = Wrap::stringsArray(['Please', 'censor', 'all', 'vowels!']);
var_export($text->replacePattern('/[aeiouy]/', '*')->toArray());
array (
0 =>
GW\Value\PlainString::__set_state(array(
'value' => 'Pl**s*',
)),
1 =>
GW\Value\PlainString::__set_state(array(
'value' => 'c*ns*r',
)),
2 =>
GW\Value\PlainString::__set_state(array(
'value' => '*ll',
)),
3 =>
GW\Value\PlainString::__set_state(array(
'value' => 'v*w*ls!',
)),
)
<?php
/**
* @param string|StringValue $pattern
*/
public function replacePatternCallback($pattern, callable $callback): StringsArray;
<?php
use GW\Value\Wrap;
$text = Wrap::stringsArray(['Please', 'censor', 'all', 'vowels!']);
$replacer = function (array $match): string {
$letter = $match[0];
return '(' . $letter . ')';
};
var_export($text->replacePatternCallback('/[aeiouy]/', $replacer)->toArray());
array (
0 =>
GW\Value\PlainString::__set_state(array(
'value' => 'Pl(e)(a)s(e)',
)),
1 =>
GW\Value\PlainString::__set_state(array(
'value' => 'c(e)ns(o)r',
)),
2 =>
GW\Value\PlainString::__set_state(array(
'value' => '(a)ll',
)),
3 =>
GW\Value\PlainString::__set_state(array(
'value' => 'v(o)w(e)ls!',
)),
)
<?php
/**
* @param string|StringValue $postfix
*/
public function truncate(int $length, $postfix = '...'): StringsArray;
<?php
use GW\Value\Wrap;
$text = Wrap::stringsArray(['short', 'quite long', 'very very long']);
var_export($text->truncate(5, '~~')->toArray());
array (
0 =>
GW\Value\PlainString::__set_state(array(
'value' => 'short',
)),
1 =>
GW\Value\PlainString::__set_state(array(
'value' => 'quite~~',
)),
2 =>
GW\Value\PlainString::__set_state(array(
'value' => 'very ~~',
)),
)
<?php
public function substring(int $start, ?int $length = null): StringsArray;
<?php
/**
* @param string|StringValue $other
*/
public function postfix($other): StringsArray;
<?php
/**
* @param string|StringValue $other
*/
public function prefix($other): StringsArray;
<?php
/**
* @return ArrayValue<StringValue>
*/
public function toArrayValue(): ArrayValue;
<?php
use GW\Value\StringValue;
use GW\Value\Wrap;
$men = ['Jack', 'John'];
$women = ['Mary', 'Tia'];
$array = Wrap::stringsArray(['John Black', 'Mary White', 'Jack Sparrow', 'Tia Dalma', 'Conchita Wurst']);
var_export(
$array->toArrayValue()
->map(function (StringValue $fullName) use ($women, $men): array {
[$name, $surname] = explode(' ', $fullName->toString());
$sex = in_array($name, $men, true) ? 'male' : (in_array($name, $women, true) ? 'female' : 'other');
return ['name' => $name, 'surname' => $surname, 'sex' => $sex];
})
->toArray()
);
array (
0 =>
array (
'name' => 'John',
'surname' => 'Black',
'sex' => 'male',
),
1 =>
array (
'name' => 'Mary',
'surname' => 'White',
'sex' => 'female',
),
2 =>
array (
'name' => 'Jack',
'surname' => 'Sparrow',
'sex' => 'male',
),
3 =>
array (
'name' => 'Tia',
'surname' => 'Dalma',
'sex' => 'female',
),
4 =>
array (
'name' => 'Conchita',
'surname' => 'Wurst',
'sex' => 'other',
),
)
<?php
/**
* @return AssocValue<int, StringValue>
*/
public function toAssocValue(): AssocValue;
<?php
use GW\Value\StringValue;
use GW\Value\Wrap;
$array = Wrap::stringsArray(['John Black', 'Mary White', 'Jack Sparrow', 'Tia Dalma', 'Conchita Wurst']);
var_export(
$array->toAssocValue()
->map(function (StringValue $person): string {
return $person->toString();
})
->mapKeys(function (int $oldKey, string $person): string {
return $person;
})
->toAssocArray()
);
array (
'John Black' => 'John Black',
'Mary White' => 'Mary White',
'Jack Sparrow' => 'Jack Sparrow',
'Tia Dalma' => 'Tia Dalma',
'Conchita Wurst' => 'Conchita Wurst',
)
<?php
/**
* @phpstan-return ArrayValue<array<int, StringValue>>
*/
public function chunk(int $size): ArrayValue;
<?php
public function toStringsArray(): StringsArray;
<?php
public function isEmpty(): bool;
(definition not available)
<?php
public function length(): int;
<?php
/**
* @param string|StringValue $needle
*/
public function position($needle): ?int;
<?php
/**
* @param string|StringValue $needle
*/
public function positionLast($needle): ?int;
<?php
/**
* @param string|StringValue $pattern
* @return ArrayValue<array<int, string>>
*/
public function matchAllPatterns($pattern): ArrayValue;
<?php
/**
* @param string|StringValue $pattern
* @return StringsArray
*/
public function matchPatterns($pattern): StringsArray;
<?php
/**
* @param string|StringValue $pattern
*/
public function isMatching($pattern): bool;
<?php
/**
* @param string|StringValue $pattern
*/
public function startsWith($pattern): bool;
<?php
/**
* @param string|StringValue $pattern
*/
public function endsWith($pattern): bool;
<?php
/**
* @return StringsArray
* @param string|StringValue $pattern
*/
public function splitByPattern($pattern): StringsArray;
<?php
/**
* @return StringsArray
* @param string|StringValue $delimiter
*/
public function explode($delimiter): StringsArray;
<?php
/**
* @param string|StringValue $substring
*/
public function contains($substring): bool;
<?php
public function toString(): string;
<?php
public function __toString(): string;
<?php
/**
* @phpstan-param callable(TValue):void $callback
* @phpstan-return IterableValue<TKey, TValue>
*/
public function each(callable $callback): IterableValue;
<?php
use GW\Value\Wrap;
$array = Wrap::iterable(['a', 'b', 'c']);
$mapped = $array->each(function (string $letter): void {
echo $letter;
});
abc
<?php
/**
* @phpstan-param callable(TValue):bool $filter
* @phpstan-return IterableValue<TKey, TValue>
*/
public function filter(callable $filter): IterableValue;
<?php
use GW\Value\Wrap;
$range = static function (int $start, int $end): iterable {
for ($i = $start; $i <= $end; $i++) {
yield $i;
}
};
$array = Wrap::iterable($range(1, 4));
$even = $array->filter(function (int $number): bool {
return $number % 2 === 0;
});
var_export($even->toArray());
array (
0 => 2,
1 => 4,
)
<?php
/**
* @phpstan-return IterableValue<TKey, TValue>
*/
public function filterEmpty(): IterableValue;
<?php
use GW\Value\Wrap;
$array = Wrap::iterable(['a', '', 'b', 'c']);
$notEmpty = $array->filterEmpty();
var_export($notEmpty->toArray());
array (
0 => 'a',
1 => 'b',
2 => 'c',
)
<?php
/**
* @template TNewValue
* @param callable(TValue,TKey $key):TNewValue $transformer
* @phpstan-return IterableValue<TKey, TNewValue>
*/
public function map(callable $transformer): IterableValue;
<?php
use GW\Value\Wrap;
$array = Wrap::iterable(['a', 'b', 'c']);
$mapped = $array->map(function (string $letter): string {
return 'new ' . $letter;
});
var_export($mapped->toArray());
array (
0 => 'new a',
1 => 'new b',
2 => 'new c',
)
<?php
/**
* @phpstan-template TNewValue
* @phpstan-param callable(TValue):iterable<TNewValue> $transformer
* @phpstan-return IterableValue<TKey, TNewValue>
*/
public function flatMap(callable $transformer): IterableValue;
<?php
/**
* @phpstan-param iterable<TKey, TValue> $other
* @phpstan-return IterableValue<TKey, TValue>
*/
public function join(iterable $other): IterableValue;
<?php
use GW\Value\Wrap;
$range = function (int $start, int $end): iterable {
for ($i = $start; $i <= $end; $i++) {
yield $i;
}
};
$one = Wrap::iterable($range(1, 3));
$two = Wrap::iterable($range(8, 10));
var_export($one->join($two)->join($range(11, 14))->toArray());
array (
0 => 1,
1 => 2,
2 => 3,
3 => 8,
4 => 9,
5 => 10,
6 => 11,
7 => 12,
8 => 13,
9 => 14,
)
<?php
/**
* @phpstan-return IterableValue<TKey, TValue>
*/
public function slice(int $offset, ?int $length = null): IterableValue;
<?php
use GW\Value\Wrap;
$range = function (int $start, int $end): iterable {
for ($i = $start; $i <= $end; $i++) {
yield $i;
}
};
var_export(Wrap::iterable($range(0, PHP_INT_MAX))->slice(2, 4)->toArray());
echo PHP_EOL;
var_export(Wrap::iterable($range(0, PHP_INT_MAX))->slice(0, 3)->toArray());
echo PHP_EOL;
var_export(Wrap::iterable($range(0, PHP_INT_MAX))->slice(5000, 2)->toArray());
echo PHP_EOL;
array (
0 => 2,
1 => 3,
2 => 4,
3 => 5,
)
array (
0 => 0,
1 => 1,
2 => 2,
)
array (
0 => 5000,
1 => 5001,
)
<?php
/**
* @phpstan-return IterableValue<TKey, TValue>
*/
public function skip(int $length): IterableValue;
<?php
/**
* @phpstan-return IterableValue<TKey, TValue>
*/
public function take(int $length): IterableValue;
<?php
use GW\Value\Wrap;
$range = function (int $start, int $end): iterable {
for ($i = $start; $i <= $end; $i++) {
yield $i;
}
};
var_export(Wrap::iterable($range(0, PHP_INT_MAX))->skip(2)->take(4)->toArray());
echo PHP_EOL;
var_export(Wrap::iterable($range(0, PHP_INT_MAX))->take(3)->toArray());
echo PHP_EOL;
var_export(Wrap::iterable($range(0, PHP_INT_MAX))->skip(5000)->take(2)->toArray());
echo PHP_EOL;
array (
0 => 2,
1 => 3,
2 => 4,
3 => 5,
)
array (
0 => 0,
1 => 1,
2 => 2,
)
array (
0 => 5000,
1 => 5001,
)
<?php
/**
* @phpstan-param (callable(TValue,TValue):int) | null $comparator
* @phpstan-return IterableValue<TKey, TValue>
*/
public function unique(?callable $comparator = null): IterableValue;
<?php
/**
* @template TNewValue
* @phpstan-param callable(TNewValue,TValue):TNewValue $transformer
* @phpstan-param TNewValue $start
* @phpstan-return TNewValue
*/
public function reduce(callable $transformer, $start);
<?php
use GW\Value\Wrap;
$prices = Wrap::iterable([10, 20, 50, 120]);
$summarize = function(int $sum, int $price): int {
return $sum + $price;
};
echo 'Sum: ' . $prices->reduce($summarize, 0);
echo PHP_EOL;
$list = function(string $list, int $price): string {
return $list . " €{$price},-";
};
echo 'Prices: ' . $prices->reduce($list, '');
echo PHP_EOL;
Sum: 200
Prices: €10,- €20,- €50,- €120,-
<?php
/**
* @phpstan-return IterableValue<TKey, TValue>
*/
public function notEmpty(): IterableValue;
<?php
/**
* @phpstan-param TValue $value
* @phpstan-return IterableValue<TKey, TValue>
*/
public function unshift($value): IterableValue;
<?php
/**
* @phpstan-param TValue $value
* @phpstan-return IterableValue<TKey, TValue>
*/
public function push($value): IterableValue;
<?php
/**
* @phpstan-param ArrayValue<TValue> $other
* @phpstan-param (callable(TValue,TValue):int) | null $comparator
* @phpstan-return IterableValue<TKey, TValue>
*/
public function diff(ArrayValue $other, ?callable $comparator = null): IterableValue;
<?php
/**
* @phpstan-param ArrayValue<TValue> $other
* @phpstan-param (callable(TValue,TValue):int) | null $comparator
* @phpstan-return IterableValue<TKey, TValue>
*/
public function intersect(ArrayValue $other, ?callable $comparator = null): IterableValue;
<?php
/**
* @phpstan-return ?TValue
*/
public function first();
<?php
use GW\Value\Wrap;
$range = function (int $start, int $end): iterable {
for ($i = $start; $i <= $end; $i++) {
yield $i;
}
};
$array = Wrap::iterable($range(1, PHP_INT_MAX));
echo 'first: ' . $array->first();
first: 1
<?php
/**
* @phpstan-return ?TValue
*/
public function last();
<?php
/**
* @phpstan-return ArrayValue<TValue>
*/
public function toArrayValue(): ArrayValue;
<?php
/**
* @phpstan-return array<int|string, TValue>
*/
public function toAssocArray(): array;
<?php
/**
* @phpstan-return AssocValue<int|string, TValue>
*/
public function toAssocValue(): AssocValue;
<?php
/**
* @phpstan-return TValue[]
*/
public function toArray(): array;
<?php
/**
* @phpstan-return IterableValue<int, array<int, TValue>>
*/
public function chunk(int $size): IterableValue;
<?php
use GW\Value\Wrap;
$array = Wrap::iterable([1, 2, 3, 4, 3, 4, 5, 6, 7, 8, 9]);
var_export($array->chunk(3)->toArray());
array (
0 =>
array (
0 => 1,
1 => 2,
2 => 3,
),
1 =>
array (
0 => 4,
1 => 3,
2 => 4,
),
2 =>
array (
0 => 5,
1 => 6,
2 => 7,
),
3 =>
array (
0 => 8,
1 => 9,
),
)
<?php
/**
* @phpstan-return IterableValue<TKey, TValue>
*/
public function flatten(): IterableValue;
<?php
/**
* @param callable(TValue):bool $filter
*/
public function any(callable $filter): bool;
<?php
/**
* @param callable(TValue):bool $filter
*/
public function every(callable $filter): bool;
<?php
/**
* @param callable(TValue):bool $filter
* @phpstan-return ?TValue
*/
public function find(callable $filter);
<?php
/**
* @param callable(TValue):bool $filter
* @phpstan-return ?TValue
*/
public function findLast(callable $filter);
<?php
/**
* @phpstan-return IterableValue<int, TKey>
*/
public function keys(): IterableValue;
<?php
use GW\Value\Wrap;
$assoc = Wrap::iterable(['0' => 'zero', '1' => 'one']);
$keys = $assoc
->map(fn(string $val, int $key): string => $val)
->keys()
->toArray();
var_export($keys);
$pairs = [['0', 'zero'], ['1', 'one'], ['1', 'one one']];
$iterator = function () use ($pairs) {
foreach ($pairs as [$key, $item]) {
yield $key => $item;
}
};
$assoc = Wrap::iterable($iterator());
$keys = $assoc
->map(fn(string $val, string $key): string => $val)
->keys()
->toArray();
var_export($keys);
array (
0 => 0,
1 => 1,
)array (
0 => '0',
1 => '1',
2 => '1',
)
(definition not available)