Skip to content

Commit

Permalink
used native PHP 8 functions
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Jul 28, 2021
1 parent b4098dc commit ced9636
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/Neon/Decoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ final class Decoder
*/
public function decode(string $input): mixed
{
if (substr($input, 0, 3) === "\u{FEFF}") { // BOM
if (str_starts_with($input, "\u{FEFF}")) { // BOM
$input = substr($input, 3);
}
$this->input = "\n" . str_replace("\r", '', $input); // \n forces indent detection
Expand Down
10 changes: 5 additions & 5 deletions src/Neon/Encoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ public function encode($var, int $flags = 0): string
foreach ($var as $k => $v) {
$v = $this->encode($v, self::BLOCK);
$s .= ($isList ? '-' : $this->encode($k) . ':')
. (strpos($v, "\n") === false
? ' ' . $v . "\n"
: "\n" . preg_replace('#^(?=.)#m', "\t", $v) . (substr($v, -2, 1) === "\n" ? '' : "\n"));
. (str_contains($v, "\n")
? "\n" . preg_replace('#^(?=.)#m', "\t", $v) . (substr($v, -2, 1) === "\n" ? '' : "\n")
: ' ' . $v . "\n");
}
return $s;

Expand All @@ -77,7 +77,7 @@ public function encode($var, int $flags = 0): string
if ($res === false) {
throw new Exception('Invalid UTF-8 sequence: ' . $var);
}
if (strpos($var, "\n") !== false) {
if (str_contains($var, "\n")) {
$res = preg_replace_callback(
'#[^\\\\]|\\\\(.)#s',
fn($m) => ['n' => "\n\t", 't' => "\t", '"' => '"'][$m[1] ?? ''] ?? $m[0],
Expand All @@ -89,7 +89,7 @@ public function encode($var, int $flags = 0): string

} elseif (is_float($var)) {
$var = json_encode($var);
return strpos($var, '.') === false ? $var . '.0' : $var;
return str_contains($var, '.') ? $var : $var . '.0';

} else {
return json_encode($var);
Expand Down

0 comments on commit ced9636

Please sign in to comment.