json = $json; $this->strategy = $strategy; $this->errorCode = $errorCode; $this->errorMessage = $errorMessage; } /** The response body. Never false, never null. */ public function json(): string { return $this->json; } /** One of the STRATEGY_* constants. */ public function strategy(): string { return $this->strategy; } /** Whether anything at all had to be given up to produce json(). */ public function isDegraded(): bool { return $this->strategy !== self::STRATEGY_DIRECT; } /** * Whether the caller's payload reached the browser at all. * * Distinct from isDegraded(): a substituted or partial encode still carries * the table the user asked for, so the admin screen works and the event is * a warning. An error envelope does not, so the admin sees a real message * naming the real reason. */ public function carriesPayload(): bool { return $this->strategy !== self::STRATEGY_ERROR_ENVELOPE; } /** json_last_error() from the first failing attempt; JSON_ERROR_NONE when nothing failed. */ public function errorCode(): int { return $this->errorCode; } /** json_last_error_msg() from the first failing attempt; '' when nothing failed. */ public function errorMessage(): string { return $this->errorMessage; } /** * The fields a diagnostic record carries about this encode. * * @return array */ public function diagnosticFields(): array { return array( 'bytes' => strlen($this->json), 'hash' => md5($this->json), 'encode_strategy' => $this->strategy, 'json_last_error' => $this->errorCode, 'json_last_error_msg' => $this->errorMessage, ); } }