Skip to content

Commit eb7ecf3

Browse files
authored
Release/4.0.0 (#34)
1 parent e4643e7 commit eb7ecf3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+1965
-968
lines changed

.github/workflows/auto-assign.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ jobs:
1313
pull-requests: write
1414
steps:
1515
- name: Assign issues
16-
uses: gustavofreze/auto-assign@1.0.0
16+
uses: gustavofreze/auto-assign@1.1.4
1717
with:
1818
assignees: '${{ secrets.ASSIGNEES }}'
1919
github_token: '${{ secrets.GITHUB_TOKEN }}'

.github/workflows/ci.yml

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,49 @@
11
name: CI
22

33
on:
4-
push:
54
pull_request:
65

76
permissions:
87
contents: read
98

9+
env:
10+
PHP_VERSION: '8.3'
11+
1012
jobs:
1113
auto-review:
1214
name: Auto review
1315
runs-on: ubuntu-latest
1416

1517
steps:
1618
- name: Checkout
17-
uses: actions/checkout@v3
19+
uses: actions/checkout@v4
1820

19-
- name: Use PHP 8.2
21+
- name: Configure PHP
2022
uses: shivammathur/setup-php@v2
2123
with:
22-
php-version: '8.2'
24+
php-version: ${{ env.PHP_VERSION }}
2325

2426
- name: Install dependencies
2527
run: composer update --no-progress --optimize-autoloader
2628

27-
- name: Run phpcs
28-
run: composer phpcs
29-
30-
- name: Run phpmd
31-
run: composer phpmd
29+
- name: Run review
30+
run: composer review
3231

3332
tests:
3433
name: Tests
3534
runs-on: ubuntu-latest
3635

3736
steps:
3837
- name: Checkout
39-
uses: actions/checkout@v3
38+
uses: actions/checkout@v4
4039

41-
- name: Use PHP 8.2
40+
- name: Use PHP ${{ env.PHP_VERSION }}
4241
uses: shivammathur/setup-php@v2
4342
with:
44-
php-version: '8.2'
43+
php-version: ${{ env.PHP_VERSION }}
4544

4645
- name: Install dependencies
4746
run: composer update --no-progress --optimize-autoloader
4847

49-
- name: Run unit tests
50-
env:
51-
XDEBUG_MODE: coverage
52-
run: composer test
53-
54-
- name: Run mutation tests
55-
run: composer test-mutation
48+
- name: Run tests
49+
run: composer tests

Makefile

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,17 @@
1-
DOCKER_RUN = docker run --rm -it --net=host -v ${PWD}:/app -w /app gustavofreze/php:8.2
1+
ifeq ($(OS),Windows_NT)
2+
PWD := $(shell cd)
3+
else
4+
PWD := $(shell pwd -L)
5+
endif
6+
7+
ARCH := $(shell uname -m)
8+
PLATFORM :=
9+
10+
ifeq ($(ARCH),arm64)
11+
PLATFORM := --platform=linux/amd64
12+
endif
13+
14+
DOCKER_RUN = docker run ${PLATFORM} --rm -it --net=host -v ${PWD}:/app -w /app gustavofreze/php:8.3
215

316
.PHONY: configure test test-file test-no-coverage review show-reports clean
417

@@ -9,7 +22,7 @@ test:
922
@${DOCKER_RUN} composer tests
1023

1124
test-file:
12-
@${DOCKER_RUN} composer tests-file-no-coverage ${FILE}
25+
@${DOCKER_RUN} composer test-file ${FILE}
1326

1427
test-no-coverage:
1528
@${DOCKER_RUN} composer tests-no-coverage
@@ -22,4 +35,4 @@ show-reports:
2235

2336
clean:
2437
@sudo chown -R ${USER}:${USER} ${PWD}
25-
@rm -rf report vendor .phpunit.cache
38+
@rm -rf report vendor .phpunit.cache .lock

README.md

Lines changed: 95 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,19 @@
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)
810
* [License](#license)
911
* [Contributing](#contributing)
1012

1113
<div id='overview'></div>
1214

1315
## Overview
1416

15-
Common implementations for HTTP protocol.
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.
1621

1722
<div id='installation'></div>
1823

@@ -26,46 +31,95 @@ composer require tiny-blocks/http
2631

2732
## How to use
2833

29-
The library exposes concrete implementations for the HTTP protocol, such as status codes, methods, etc.
30-
31-
### Using the HttpCode
32-
33-
The library exposes a concrete implementation through the `HttpCode` enum. You can get the status codes, and their
34-
corresponding messages.
35-
36-
```php
37-
$httpCode = HttpCode::CREATED;
38-
39-
$httpCode->name; # CREATED
40-
$httpCode->value; # 201
41-
$httpCode->message(); # 201 Created
42-
```
43-
44-
### Using the HttpMethod
45-
46-
The library exposes a concrete implementation via the `HttpMethod` enum. You can get a set of HTTP methods.
47-
48-
```php
49-
$method = HttpMethod::GET;
50-
51-
$method->name; # GET
52-
$method->value; # GET
53-
```
54-
55-
### Using the HttpResponse
56-
57-
The library exposes a concrete implementation for HTTP responses via the `HttpResponse` class. Responses are of the
58-
[ResponseInterface](https://github.com/php-fig/http-message/blob/master/src/ResponseInterface.php) type, according to
59-
the specifications defined in [PSR-7](https://www.php-fig.org/psr/psr-7).
60-
61-
```php
62-
$data = new Xyz(value: 10);
63-
$response = HttpResponse::ok(data: $data);
64-
65-
$response->getStatusCode(); # 200
66-
$response->getReasonPhrase(); # 200 OK
67-
$response->getBody()->getContents(); # {"value":10}
68-
```
34+
The library exposes interfaces like `Headers` and concrete implementations like `Response`, `ContentType`, and others,
35+
which facilitate construction.
36+
37+
<div id='status_code'></div>
38+
39+
### Using the status code
40+
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.
43+
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.
46+
47+
```php
48+
use TinyBlocks\Http\Code;
49+
50+
Code::OK->message(); # 200 OK
51+
Code::IM_A_TEAPOT->message(); # 418 I'm a teapot
52+
Code::INTERNAL_SERVER_ERROR->message(); # 500 Internal Server Error
53+
```
54+
55+
- **Check if the code is valid**: Determines if the given code is a valid HTTP status code represented by the enum.
56+
57+
```php
58+
use TinyBlocks\Http\Code;
59+
60+
Code::isValidCode(code: 200); # true
61+
Code::isValidCode(code: 999); # false
62+
```
63+
64+
- **Check if the code is an error**: Determines if the given code is in the error range (**4xx** or **5xx**).
65+
66+
```php
67+
use TinyBlocks\Http\Code;
68+
69+
Code::isErrorCode(code: 500); # true
70+
Code::isErrorCode(code: 200); # false
71+
```
72+
73+
- **Check if the code is a success**: Determines if the given code is in the success range (**2xx**).
74+
75+
```php
76+
use TinyBlocks\Http\Code;
77+
78+
Code::isSuccessCode(code: 500); # false
79+
Code::isSuccessCode(code: 200); # true
80+
```
81+
82+
<div id='response'></div>
83+
84+
### Creating a response
85+
86+
The library provides an easy and flexible way to create HTTP responses, allowing you to specify the status code,
87+
headers, and body. You can use the `Response` class to generate responses, and the result will always be a
88+
`ResponseInterface` from the PSR, ensuring compatibility with any framework that adheres
89+
to the [PSR-7](https://www.php-fig.org/psr/psr-7) standard.
90+
91+
- **Creating a response with a body**: To create an HTTP response, you can pass any type of data as the body.
92+
Optionally, you can also specify one or more headers. If no headers are provided, the response will default to
93+
`application/json` content type.
94+
95+
```php
96+
use TinyBlocks\Http\Response;
97+
98+
Response::ok(body: ['message' => 'Resource created successfully.']);
99+
```
100+
101+
- **Creating a response with a body and custom headers**: You can also add custom headers to the response. For instance,
102+
if you want to specify a custom content type or any other header, you can pass the headers as additional arguments.
103+
104+
```php
105+
use TinyBlocks\Http\Response;
106+
use TinyBlocks\Http\ContentType;
107+
use TinyBlocks\Http\CacheControl;
108+
use TinyBlocks\Http\ResponseCacheDirectives;
109+
110+
$body = 'This is a plain text response';
111+
112+
$contentType = ContentType::textPlain();
113+
114+
$cacheControl = CacheControl::fromResponseDirectives(
115+
maxAge: ResponseCacheDirectives::maxAge(maxAgeInWholeSeconds: 10000),
116+
staleIfError: ResponseCacheDirectives::staleIfError()
117+
);
118+
119+
Response::ok($body, $contentType, $cacheControl)
120+
->withHeader(name: 'X-ID', value: 100)
121+
->withHeader(name: 'X-NAME', value: 'Xpto');
122+
```
69123

70124
<div id='license'></div>
71125

composer.json

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,14 @@
99
"keywords": [
1010
"psr",
1111
"http",
12+
"psr-7",
13+
"psr-15",
14+
"request",
15+
"response",
1216
"http-code",
1317
"tiny-blocks",
1418
"http-status",
15-
"http-methods",
16-
"http-response"
19+
"http-methods"
1720
],
1821
"authors": [
1922
{
@@ -42,43 +45,41 @@
4245
}
4346
},
4447
"require": {
45-
"php": "^8.2",
46-
"tiny-blocks/serializer": "^3",
48+
"php": "^8.3",
4749
"psr/http-message": "^1.1",
50+
"tiny-blocks/serializer": "^3",
4851
"ext-mbstring": "*"
4952
},
5053
"require-dev": {
54+
"slim/psr7": "^1.7",
55+
"slim/slim": "^4.14",
5156
"phpmd/phpmd": "^2.15",
52-
"phpunit/phpunit": "^11",
5357
"phpstan/phpstan": "^1",
54-
"infection/infection": "^0.29",
55-
"squizlabs/php_codesniffer": "^3.10"
58+
"phpunit/phpunit": "^11",
59+
"infection/infection": "^0",
60+
"squizlabs/php_codesniffer": "^3.11"
5661
},
5762
"suggest": {
5863
"ext-mbstring": "Provides multibyte-specific string functions that help us deal with multibyte encodings in PHP."
5964
},
6065
"scripts": {
66+
"test": "phpunit --configuration phpunit.xml tests",
6167
"phpcs": "phpcs --standard=PSR12 --extensions=php ./src",
62-
"phpmd": "phpmd ./src text phpmd.xml --suffixes php --exclude /src/HttpCode.php --exclude /src/Internal/Response --ignore-violations-on-exit",
68+
"phpmd": "phpmd ./src text phpmd.xml --suffixes php --ignore-violations-on-exit",
6369
"phpstan": "phpstan analyse -c phpstan.neon.dist --quiet --no-progress",
64-
"test": "phpunit --log-junit=report/coverage/junit.xml --coverage-xml=report/coverage/coverage-xml --coverage-html=report/coverage/coverage-html tests",
65-
"test-mutation": "infection --only-covered --logger-html=report/coverage/mutation-report.html --coverage=report/coverage --min-msi=100 --min-covered-msi=100 --threads=4",
66-
"test-no-coverage": "phpunit --no-coverage",
67-
"test-mutation-no-coverage": "infection --only-covered --min-msi=100 --threads=4",
70+
"test-file": "phpunit --configuration phpunit.xml --no-coverage --filter",
71+
"mutation-test": "infection --only-covered --threads=max --logger-html=report/coverage/mutation-report.html --coverage=report/coverage",
72+
"test-no-coverage": "phpunit --configuration phpunit.xml --no-coverage tests",
6873
"review": [
6974
"@phpcs",
7075
"@phpmd",
7176
"@phpstan"
7277
],
7378
"tests": [
7479
"@test",
75-
"@test-mutation"
80+
"@mutation-test"
7681
],
7782
"tests-no-coverage": [
78-
"@test-no-coverage",
79-
"@test-mutation-no-coverage"
80-
],
81-
"tests-file-no-coverage": [
8283
"@test-no-coverage"
8384
]
8485
}

infection.json.dist

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,23 @@
11
{
2-
"timeout": 10,
3-
"testFramework": "phpunit",
2+
"logs": {
3+
"text": "report/infection/logs/infection-text.log",
4+
"summary": "report/infection/logs/infection-summary.log"
5+
},
46
"tmpDir": "report/infection/",
7+
"minMsi": 100,
8+
"timeout": 30,
59
"source": {
610
"directories": [
711
"src"
812
]
913
},
10-
"logs": {
11-
"text": "report/infection/logs/infection-text.log",
12-
"summary": "report/infection/logs/infection-summary.log"
13-
},
14-
"mutators": {
15-
"@default": true,
16-
"CastInt": false,
17-
"CastString": false,
18-
"MatchArmRemoval": false,
19-
"MethodCallRemoval": false
20-
},
2114
"phpUnit": {
2215
"configDir": "",
2316
"customPath": "./vendor/bin/phpunit"
24-
}
17+
},
18+
"mutators": {
19+
"@default": true
20+
},
21+
"minCoveredMsi": 100,
22+
"testFramework": "phpunit"
2523
}

phpmd.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,5 +53,4 @@
5353
<rule ref="rulesets/unusedcode.xml/UnusedPrivateField"/>
5454
<rule ref="rulesets/unusedcode.xml/UnusedLocalVariable"/>
5555
<rule ref="rulesets/unusedcode.xml/UnusedPrivateMethod"/>
56-
<rule ref="rulesets/unusedcode.xml/UnusedFormalParameter"/>
5756
</ruleset>

0 commit comments

Comments
 (0)