Skip to content

Commit

Permalink
refactor and fix methods in StringObject classes
Browse files Browse the repository at this point in the history
  • Loading branch information
deminy committed Feb 17, 2024
1 parent 3f62e91 commit 031eba5
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
Fixed:

* Fix accessing undefined properties in method \Swoole\NameResolver::checkResponse(). ([commit](https://github.com/swoole/library/commit/7a6396e45f4d4517a049584a746285d6501cf71d))
* Fix the implementation of method `\Swoole\MultibyteStringObject::chunk()`.

Changed:

* Refactor: Rename parameter in method `\Swoole\Database\PDOStatementProxy::setFetchMode()` for consistency.
* Refactor: Rename parameter in method `\Swoole\MultibyteStringObject::substr()` for consistency.
* Refactor: Enhance method `\Swoole\FastCGI\Message::withBody()` with explicit parameter type.
* Refactor: Rename parameter and default value of method `\Swoole\StringObject::chunkSplit()` for consistency.
* Refactor: Rename parameter in method `\Swoole\StringObject::chunk()` for consistency.
* FastCGI: Make constructor argument required for records. ([commit](https://github.com/swoole/library/commit/497bb74eaad51f661c91bc936f976b8660ce716c))

## 5.1.2 (2024-01-24)
Expand Down
7 changes: 4 additions & 3 deletions src/core/MultibyteStringObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,11 @@ public function substr(int $start, ?int $length = null, ?string $encoding = null
}

/**
* @todo This method is not implemented correctly.
* {@inheritDoc}
* @see https://www.php.net/mb_str_split
*/
public function chunk(int $splitLength = 1, ?int $limit = null): ArrayObject
public function chunk(int $length = 1): ArrayObject
{
return static::detectArrayType(mb_split($this->string, ...func_get_args()));
return static::detectArrayType(mb_str_split($this->string, $length));
}
}
21 changes: 17 additions & 4 deletions src/core/StringObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,14 +167,27 @@ public function char(int $index): string
return $this->string[$index];
}

public function chunkSplit(int $chunkLength = 76, string $chunkEnd = ''): static
/**
* Get a new string object by splitting the string of current object into smaller chunks.
*
* @param int $length The chunk length.
* @param string $separator The line ending sequence.
* @see https://www.php.net/chunk_split
*/
public function chunkSplit(int $length = 76, string $separator = "\r\n"): static
{
return new static(chunk_split($this->string, ...func_get_args())); // @phpstan-ignore new.static
return new static(chunk_split($this->string, $length, $separator)); // @phpstan-ignore new.static
}

public function chunk(int $splitLength = 1): ArrayObject
/**
* Convert a string to an array object of class \Swoole\ArrayObject.
*
* @param int $length Maximum length of the chunk.
* @see https://www.php.net/str_split
*/
public function chunk(int $length = 1): ArrayObject
{
return static::detectArrayType(str_split($this->string, ...func_get_args()));
return static::detectArrayType(str_split($this->string, $length));
}

public function toString(): string
Expand Down

0 comments on commit 031eba5

Please sign in to comment.