Created
September 23, 2016 17:12
-
-
Save SerafimArts/df72795a47ac023f7b7f975a33b8b6d3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Illuminate\Contracts\Support\Renderable; | |
use Illuminate\Support\Str; | |
/** | |
* Class Message | |
*/ | |
class Message implements Renderable, \Countable, \IteratorAggregate | |
{ | |
/** | |
* @var string | |
*/ | |
private $content; | |
/** | |
* Message constructor. | |
* @param string $content | |
*/ | |
public function __construct(string $content) | |
{ | |
$this->content = $content; | |
} | |
/** | |
* @return \Generator|string[] | |
*/ | |
public function chars(): \Generator | |
{ | |
for ($i = 0, $len = $this->length(); $i < $len; ++$i) { | |
yield $i => mb_substr($this->content, $i, 1); | |
} | |
} | |
/** | |
* @return \Generator|string[] | |
*/ | |
public function bytes(): \Generator | |
{ | |
/** @noinspection ForeachInvariantsInspection */ | |
for ($i = 0, $len = $this->length(); $i < $len; ++$i) { | |
/** @noinspection OffsetOperationsInspection */ | |
yield $i => $this->content{$i}; // Using `bytes{N}` instead `bytes[N]` will be a little more faster | |
} | |
} | |
/** | |
* @param int $chars | |
* @return \Generator|Message[] | |
*/ | |
public function wrap(int $chars): \Generator | |
{ | |
$buffer = ''; | |
foreach ($this->chars() as $char) { | |
$buffer .= $char; | |
if ($buffer >= $chars) { | |
yield new Message($buffer); | |
$buffer = ''; | |
} | |
} | |
yield new Message($buffer); | |
} | |
/** | |
* @param \string[] ...$words | |
* @return bool | |
*/ | |
public function startsWith(string ...$words): bool | |
{ | |
return Str::startsWith($this->content, $words); | |
} | |
/** | |
* @param int $words | |
* @param string $endWith | |
* @return Message | |
*/ | |
public function wordsLimit(int $words, string $endWith = '...'): Message | |
{ | |
return new Message(Str::words($this->content, $words, $endWith)); | |
} | |
/** | |
* @param int $words | |
* @param string $endWith | |
* @return Message | |
*/ | |
public function charsLimit(int $words, string $endWith = '...'): Message | |
{ | |
return new Message(Str::limit($this->content, $words, $endWith)); | |
} | |
/** | |
* @return int | |
*/ | |
public function length(): int | |
{ | |
return Str::length($this->content); | |
} | |
/** | |
* @return Message | |
*/ | |
public function lowerCase(): Message | |
{ | |
return new Message(Str::lower($this->content)); | |
} | |
/** | |
* @return Message | |
*/ | |
public function upperCase(): Message | |
{ | |
return new Message(Str::upper($this->content)); | |
} | |
/** | |
* @param \string[] ...$words | |
* @return bool | |
*/ | |
public function endsWith(string ...$words): bool | |
{ | |
return Str::endsWith($this->content, $words); | |
} | |
/** | |
* @param \string[] ...$content | |
* @return bool | |
*/ | |
public function contains(string ...$content): bool | |
{ | |
return Str::contains($this->content, $content); | |
} | |
/** | |
* @param \string[] ...$patterns | |
* @return bool | |
*/ | |
public function match(string ...$patterns): bool | |
{ | |
foreach ($patterns as $pattern) { | |
if (!$this->like(sprintf('^%s$', $pattern))) { | |
return false; | |
} | |
} | |
return true; | |
} | |
/** | |
* @param \string[] ...$patterns | |
* @return bool | |
*/ | |
public function like(string ...$patterns): bool | |
{ | |
foreach ($patterns as $pattern) { | |
if (!preg_match(sprintf('/%s/isu', $pattern), $this->content)) { | |
return false; | |
} | |
} | |
return true; | |
} | |
/** | |
* @return \Generator|Message[] | |
*/ | |
public function sentences(): \Generator | |
{ | |
foreach (preg_split('/(\n|:[a-z_]+:|\.|\){2,})/iu', $this->content) as $text) { | |
if (trim($text)) { | |
yield new Message($text); | |
} | |
} | |
} | |
/** | |
* @return string | |
*/ | |
public function __toString(): string | |
{ | |
return (string)$this->render(); | |
} | |
/** | |
* @return string | |
*/ | |
public function render(): string | |
{ | |
return (string)$this->content; | |
} | |
/** | |
* @return int | |
*/ | |
public function count(): int | |
{ | |
return $this->length(); | |
} | |
/** | |
* @return \Generator | |
*/ | |
public function getIterator(): \Generator | |
{ | |
yield from $this->chars(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment