55* [ Overview] ( #overview )
66* [ Installation] ( #installation )
77* [ How to use] ( #how-to-use )
8- * [ Using the status code ] ( #status_code )
9- * [ Creating a response ] ( #response )
8+ * [ Request ] ( #request )
9+ * [ Response ] ( #response )
1010* [ License] ( #license )
1111* [ Contributing] ( #contributing )
1212
1313<div id =' overview ' ></div >
1414
1515## Overview
1616
17- Common implementations for HTTP protocol. The library exposes concrete implementations that follow the PSR standards,
18- specifically designed to operate with [ PSR-7 ] ( https://www.php-fig.org/psr/psr-7 )
19- and [ PSR-15] ( https://www.php-fig.org/psr/psr-15 ) , providing solutions for building HTTP responses, requests, and other
20- HTTP-related components.
17+ Common implementations for the HTTP protocol. The library exposes concrete implementations that follow the PSR standards
18+ and are ** framework-agnostic ** , designed to work consistently across any ecosystem that supports
19+ [ PSR-7 ] ( https://www.php-fig.org/psr/psr-7 ) and [ PSR-15] ( https://www.php-fig.org/psr/psr-15 ) , providing solutions for
20+ building HTTP responses, requests, and other HTTP-related components.
2121
2222<div id =' installation ' ></div >
2323
@@ -31,58 +31,65 @@ composer require tiny-blocks/http
3131
3232## How to use
3333
34- The library exposes interfaces like ` Headers ` and concrete implementations like ` Response ` , ` ContentType ` , and others ,
35- which facilitate construction.
34+ The library exposes interfaces like ` Headers ` and concrete implementations like ` Request ` , ` Response ` , ` ContentType ` ,
35+ and others, which facilitate construction.
3636
37- <div id =' status_code ' ></div >
37+ <div id =' request ' ></div >
3838
39- ### Using the status code
39+ ### Request
4040
41- The library exposes a concrete implementation through the ` Code ` enum. You can retrieve the status codes, their
42- corresponding messages, and check for various status code ranges using the methods provided.
41+ #### Decoding a request
4342
44- - ** Get message** : Returns the [ HTTP status message] ( https://developer.mozilla.org/en-US/docs/Web/HTTP/Messages )
45- associated with the enum's code.
43+ The library provides a small public API to decode a PSR-7 ` ServerRequestInterface ` into a typed structure, allowing you
44+ to access route parameters and JSON body fields consistently.
45+
46+ - ** Decode a request** : Use ` Request::from(...) ` to wrap the PSR-7 request and call ` decode() ` . The decoded object
47+ exposes ` uri ` and ` body ` .
4648
4749 ``` php
48- use TinyBlocks\Http\Code;
49-
50- Code::OK->value; # 200
51- Code::OK->message(); # OK
52- Code::IM_A_TEAPOT->message(); # I'm a teapot
53- Code::INTERNAL_SERVER_ERROR->message(); # Internal Server Error
54- ```
50+ use Psr\Http\Message\ServerRequestInterface;
51+ use TinyBlocks\Http\Request;
5552
56- - ** Check if the code is valid** : Determines if the given code is a valid HTTP status code represented by the enum.
53+ /** @var ServerRequestInterface $psrRequest */
54+ $decoded = Request::from(request: $psrRequest)->decode();
5755
58- ``` php
59- use TinyBlocks\Http\Code;
60-
61- Code::isValidCode(code: 200); # true
62- Code::isValidCode(code: 999); # false
56+ $name = $decoded->body->get(key: 'name')->toString();
57+ $payload = $decoded->body->toArray();
58+
59+ $id = $decoded->uri->route()->get(key: 'id')->toInteger();
6360 ```
6461
65- - ** Check if the code is an error** : Determines if the given code is in the error range (** 4xx** or ** 5xx** ).
62+ - ** Typed access with defaults** : Each value is returned as an Attribute, which provides safe conversions and default
63+ values when the underlying value is missing or not compatible.
6664
6765 ``` php
68- use TinyBlocks\Http\Code ;
66+ use TinyBlocks\Http\Request ;
6967
70- Code::isErrorCode(code: 500); # true
71- Code::isErrorCode(code: 200); # false
68+ $decoded = Request::from(request: $psrRequest)->decode();
69+
70+ $id = $decoded->uri->route()->get(key: 'id')->toInteger(); # default: 0
71+ $note = $decoded->body->get(key: 'note')->toString(); # default: ""
72+ $tags = $decoded->body->get(key: 'tags')->toArray(); # default: []
73+ $price = $decoded->body->get(key: 'price')->toFloat(); # default: 0.00
74+ $active = $decoded->body->get(key: 'active')->toBoolean(); # default: false
7275 ```
7376
74- - ** Check if the code is a success** : Determines if the given code is in the success range (** 2xx** ).
77+ - ** Custom route attribute name** : If your framework stores route params in a different request attribute, you can
78+ specify it via route().
7579
7680 ``` php
77- use TinyBlocks\Http\Code ;
81+ use TinyBlocks\Http\Request ;
7882
79- Code::isSuccessCode(code: 500); # false
80- Code::isSuccessCode(code: 200); # true
83+ $decoded = Request::from(request: $psrRequest)->decode();
84+
85+ $id = $decoded->uri->route(name: '_route_params')->get(key: 'id')->toInteger();
8186 ```
8287
8388<div id =' response ' ></div >
8489
85- ### Creating a response
90+ ### Response
91+
92+ #### Creating a response
8693
8794The library provides an easy and flexible way to create HTTP responses, allowing you to specify the status code,
8895headers, and body. You can use the ` Response ` class to generate responses, and the result will always be a
@@ -122,6 +129,50 @@ to the [PSR-7](https://www.php-fig.org/psr/psr-7) standard.
122129 ->withHeader(name: 'X-NAME', value: 'Xpto');
123130 ```
124131
132+ #### Using the status code
133+
134+ The library exposes a concrete implementation through the ` Code ` enum. You can retrieve the status codes, their
135+ corresponding messages, and check for various status code ranges using the methods provided.
136+
137+ - ** Get message** : Returns the [ HTTP status message] ( https://developer.mozilla.org/en-US/docs/Web/HTTP/Messages )
138+ associated with the enum's code.
139+
140+ ``` php
141+ use TinyBlocks\Http\Code;
142+
143+ Code::OK->value; # 200
144+ Code::OK->message(); # OK
145+ Code::IM_A_TEAPOT->message(); # I'm a teapot
146+ Code::INTERNAL_SERVER_ERROR->message(); # Internal Server Error
147+ ```
148+
149+ - ** Check if the code is valid** : Determines if the given code is a valid HTTP status code represented by the enum.
150+
151+ ``` php
152+ use TinyBlocks\Http\Code;
153+
154+ Code::isValidCode(code: 200); # true
155+ Code::isValidCode(code: 999); # false
156+ ```
157+
158+ - ** Check if the code is an error** : Determines if the given code is in the error range (** 4xx** or ** 5xx** ).
159+
160+ ``` php
161+ use TinyBlocks\Http\Code;
162+
163+ Code::isErrorCode(code: 500); # true
164+ Code::isErrorCode(code: 200); # false
165+ ```
166+
167+ - ** Check if the code is a success** : Determines if the given code is in the success range (** 2xx** ).
168+
169+ ``` php
170+ use TinyBlocks\Http\Code;
171+
172+ Code::isSuccessCode(code: 500); # false
173+ Code::isSuccessCode(code: 200); # true
174+ ```
175+
125176<div id =' license ' ></div >
126177
127178## License
0 commit comments