GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — ModularTemplate/src/classes/JSONResponse.php at master · FedgeNo/ModularTemplate

Expand file tree

/

JSONResponse.php

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
92 lines (76 loc) · 2.64 KB
/

JSONResponse.php

Copy path

File metadata and controls

92 lines (76 loc) · 2.64 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<?php
declare(strict_types=1);
class JSONResponse
{
public int $statusCode = 200;
public mixed $response = null;
public ?string $error = null;
/**
* Which inputs were refused and why, keyed by field name.
*
* A bare message leaves somebody hunting for which of five boxes it is
* about, which is bad enough looking at the screen and hopeless not
* looking at it. Named, the client can put each message under the box it
* belongs to - and say all of them at once, so nobody submits five times
* to be told five things.
*
* @var array<string, string>|null
*/
public ?array $fields = null;
public static function success(mixed $response = null, int $status_code = 200): self
{
$json = new self();
$json -> response = $response;
$json -> statusCode = $status_code;
return $json;
}
public static function error(string $error, int $status_code = 400): self
{
$json = new self();
$json -> error = $error;
$json -> statusCode = $status_code;
return $json;
}
/**
* A refusal that knows which inputs caused it.
*
* The summary is what a client with no form to mark up falls back to, and
* what anything older than this keeps showing - so an endpoint can start
* naming fields without breaking whatever is already reading it.
*
* @param array<string, string> $fields
*/
public static function fieldErrors(array $fields, ?string $summary = null, int $status_code = 422): self
{
$json = self::error($summary ?? implode(' ', $fields), $status_code);
$json -> fields = $fields;
return $json;
}
/** The common case: one input, one reason. */
public static function fieldError(string $field, string $reason, int $status_code = 422): self
{
return self::fieldErrors([$field => $reason], $reason, $status_code);
}
public function send(): void
{
while (ob_get_level() > 0) {
ob_end_clean();
}
http_response_code($this -> statusCode);
header('Content-Type: application/json');
header('Cache-Control: no-store, no-cache, must-revalidate, max-age=0');
header('Pragma: no-cache');
header('Expires: 0');
$body = [
'error' => $this -> error,
'response' => $this -> response,
];
// Only when there are any, so every other response keeps the shape it
// has always had.
if ($this -> fields !== null) {
$body['fields'] = $this -> fields;
}
echo json_encode($body);
exit;
}
}
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.