Skip to content

Commit

Permalink
Parse\CharacterSequence - Refactor split() method into 3 separate met…
Browse files Browse the repository at this point in the history
…hods

  - one method for each previous option: splitByChar(), splitByCount() and splitByLength()
  • Loading branch information
cbornhoft committed Oct 26, 2020
1 parent d21d290 commit 9a5b2d4
Showing 1 changed file with 44 additions and 32 deletions.
76 changes: 44 additions & 32 deletions src/Parse/CharacterSequence.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,56 +4,68 @@
namespace Envms\Osseus\Parse;

/**
* Class CharacterSequence - A long winded way to say the reserved word "String"
* Class CharacterSequence - A long-winded way to say the reserved keyword "String"
* Every method within this class aims to be unicode-safe
*/
class CharacterSequence
{
public const SPLIT_EACH = 1;
public const SPLIT_SECTIONS = 2;
public const SPLIT_LENGTH = 3;

/**
* A unicode-safe method of splitting a string
* Splits a string by each character
*
* @example - The string 'abcde' becomes ['a', 'b', 'c', 'd', 'e']
*
* @param string $string
* @param int $type - Method by which to split the string. Can be one of SPLIT_EACH,
* SPLIT_SECTIONS or SPLIT_LENGTH
* @param int $count - How long each section should be (if the $sections argument is not set)
*
* @return array|bool
*/
public function split(string $string, int $type = self::SPLIT_EACH, int $count = 1)
public function splitByChar(string $string)
{
return preg_split("//u", $string, -1, PREG_SPLIT_NO_EMPTY);
}

/**
* Splits a string by a number of "sections" ($count)
*
* @example - If $count = 2, the string 'qwertyuiop' becomes ['qwert', 'yuiop']
*
* @param string $string
* @param int $count - How many sections there should be
*
* @return array
*/
public function splitByCount(string $string, int $count): array
{
$split = [];
$strlen = mb_strlen($string);
$length = ceil($strlen / $count);

// if only a string is passed, split the string character by character
if ($type === self::SPLIT_EACH) {
return preg_split("//u", $string, -1, PREG_SPLIT_NO_EMPTY);
for ($i = 0; $i < $count; $i++) {
$start = $i * $length;
$split[] = mb_substr($string, $start, $length);
}

if ($type === self::SPLIT_SECTIONS) {
// if the sections argument is set, overwrite the length argument with an equal split value
$length = ceil($strlen / $count);

for ($i = 0; $i < $count; $i++) {
$start = $i * $length;
$split[] = mb_substr($string, $start, $length);
}

return $split;
}
return $split;
}

if ($type === self::SPLIT_LENGTH) {
// split by the requested chunk size
for ($i = 0; $i < $strlen; $i += $count) {
$split[] = mb_substr($string, $i, $count);
}
/**
* Splits a string into parts equal in length
*
* @example - If $count = 2, the string 'qwertyuiop' becomes ['qw', 'er', 'ty', 'ui', 'op']
*
* @param string $string
* @param int $count - How long each section should be
*
* @return array
*/
public function splitByLength(string $string, int $count)
{
$split = [];
$strlen = mb_strlen($string);

return $split;
for ($i = 0; $i < $strlen; $i += $count) {
$split[] = mb_substr($string, $i, $count);
}

// invalid split type selected
return false;
return $split;
}
}

0 comments on commit 9a5b2d4

Please sign in to comment.