function splitBy($string, $separator = ' ', $limit = null) { expectsAll(string, [$string, $separator]); expectsOptional(int, $string); return explode($separator, $string, $limit); }
/** * Flattens multidimensional list * * @param array|\Traversable $sequence * @param int|null $depth * @return array */ function flatten($sequence, $depth = null) { args\expects(args\traversable, $sequence); args\expectsOptional(args\int, $depth); if (null === $depth) { $result = array(); array_walk_recursive(ds\traversableToArray($sequence), function ($item, $key) use(&$result) { if ($item instanceof \Traversable) { $result = array_merge($result, flatten(ds\traversableToArray($item))); } else { $result[] = $item; } }); return $result; } $result = array(); foreach ($sequence as $value) { if ($depth && (is_array($value) || $value instanceof \Traversable)) { foreach ($depth > 1 ? flatten($value, $depth - 1) : $value as $subValue) { $result[] = $subValue; } } else { $result[] = $value; } } return $result; }
function expectsOptionalIntNegativeTest($arg1) { expectsOptional([int], $arg1); }