Skip to content

Commit 8c492d9

Browse files
committed
Extract process key method
1 parent 927b365 commit 8c492d9

File tree

2 files changed

+28
-16
lines changed

2 files changed

+28
-16
lines changed

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Copyright (c) 2106 - 2017 Riikka Kalliomäki
1+
Copyright (c) 2016 - 2017 Riikka Kalliomäki
22

33
Permission is hereby granted, free of charge, to any person obtaining a copy of
44
this software and associated documentation files (the "Software"), to deal in

src/AbstractJsonEncoder.php

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -185,23 +185,9 @@ public function next()
185185
private function processStack(\Generator $generator, $isObject)
186186
{
187187
if ($isObject) {
188-
$key = $generator->key();
189-
190-
if (!is_int($key) && !is_string($key)) {
191-
$this->addError('Only string or integer keys are supported');
188+
if (!$this->processKey($generator->key())) {
192189
return;
193190
}
194-
195-
if (!$this->first) {
196-
$this->outputLine(',', JsonToken::T_COMMA);
197-
}
198-
199-
$this->outputJson((string) $key, JsonToken::T_NAME);
200-
$this->output(':', JsonToken::T_COLON);
201-
202-
if ($this->options & JSON_PRETTY_PRINT) {
203-
$this->output(' ', JsonToken::T_WHITESPACE);
204-
}
205191
} elseif (!$this->first) {
206192
$this->outputLine(',', JsonToken::T_COMMA);
207193
}
@@ -210,6 +196,32 @@ private function processStack(\Generator $generator, $isObject)
210196
$this->processValue($generator->current());
211197
}
212198

199+
/**
200+
* Handles the given value key into JSON.
201+
* @param mixed $key The key to process
202+
* @return bool True if the key is valid, false if not
203+
*/
204+
private function processKey($key)
205+
{
206+
if (!is_int($key) && !is_string($key)) {
207+
$this->addError('Only string or integer keys are supported');
208+
return false;
209+
}
210+
211+
if (!$this->first) {
212+
$this->outputLine(',', JsonToken::T_COMMA);
213+
}
214+
215+
$this->outputJson((string) $key, JsonToken::T_NAME);
216+
$this->output(':', JsonToken::T_COLON);
217+
218+
if ($this->options & JSON_PRETTY_PRINT) {
219+
$this->output(' ', JsonToken::T_WHITESPACE);
220+
}
221+
222+
return true;
223+
}
224+
213225
/**
214226
* Handles the given JSON value appropriately depending on it's type.
215227
* @param mixed $value The value that should be encoded as JSON

0 commit comments

Comments
 (0)