Skip to content

Commit 182c14d

Browse files
committed
First commit
1 parent 31c3456 commit 182c14d

File tree

4 files changed

+177
-0
lines changed

4 files changed

+177
-0
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/.idea/
2+
/.vs/
3+
/.vscode/
4+
/vendor/
5+
/composer.lock

README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,34 @@
11
# HTTPFactory
22
PSR-17 HTTP Factory Library
3+
4+
5+
## Requirements
6+
7+
- PHP 7.4 or higher
8+
- PSR-17 HTTP Factory Package
9+
- [InitPHP HTTP Library](https://github.com/InitPHP/HTTP)
10+
11+
## Installation
12+
13+
```
14+
composer require initphp/http-factory
15+
```
16+
17+
## Usage
18+
19+
```php
20+
require_once "vendor/autoload.php";
21+
use InitPHP\HTTPFactory\HTTPFactory;
22+
23+
$http = new HTTPFactory();
24+
25+
$request = $http->createRequest('GET', 'https://www.muhammetsafak.com.tr');
26+
```
27+
28+
## Credits
29+
30+
- [Muhammet ŞAFAK](https://www.muhammetsafak.com.tr) <<[email protected]>>
31+
32+
## License
33+
34+
Copyright &copy; 2022 [MIT License](./LICENSE)

composer.json

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"name": "initphp/http-factory",
3+
"description": "PSR17 HTTP Factory Library",
4+
"type": "library",
5+
"license": "MIT",
6+
"autoload": {
7+
"psr-4": {
8+
"InitPHP\\HTTPFactory\\": "src/"
9+
}
10+
},
11+
"authors": [
12+
{
13+
"name": "Muhammet ŞAFAK",
14+
"email": "[email protected]",
15+
"role": "Developer",
16+
"homepage": "https://www.muhammetsafak.com.tr"
17+
}
18+
],
19+
"minimum-stability": "stable",
20+
"require": {
21+
"php": ">=7.4",
22+
"psr/http-factory": "^1.0",
23+
"initphp/http": "^1.0"
24+
}
25+
}

src/HTTPFactory.php

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
<?php
2+
/**
3+
* HTTPFactory.php
4+
*
5+
* This file is part of HTTPFactory.
6+
*
7+
* @author Muhammet ŞAFAK <[email protected]>
8+
* @copyright Copyright © 2022 Muhammet ŞAFAK
9+
* @license ./LICENSE MIT
10+
* @version 1.0
11+
* @link https://www.muhammetsafak.com.tr
12+
*/
13+
14+
declare(strict_types=1);
15+
16+
namespace InitPHP\HTTPFactory;
17+
18+
use \InitPHP\HTTP\{Request,
19+
Response,
20+
ServerRequest,
21+
Stream,
22+
UploadedFile,
23+
Uri};
24+
use Psr\Http\Message\{RequestInterface,
25+
ResponseInterface,
26+
ServerRequestInterface,
27+
StreamInterface,
28+
UploadedFileInterface,
29+
UriInterface,
30+
UriFactoryInterface,
31+
RequestFactoryInterface,
32+
ResponseFactoryInterface,
33+
ServerRequestFactoryInterface,
34+
StreamFactoryInterface,
35+
UploadedFileFactoryInterface};
36+
37+
class HTTPFactory implements UriFactoryInterface, RequestFactoryInterface, ResponseFactoryInterface, ServerRequestFactoryInterface, StreamFactoryInterface, UploadedFileFactoryInterface
38+
{
39+
40+
/**
41+
* @inheritDoc
42+
*/
43+
public function createUri(string $uri = ''): UriInterface
44+
{
45+
return new Uri($uri);
46+
}
47+
48+
/**
49+
* @inheritDoc
50+
*/
51+
public function createRequest(string $method, $uri): RequestInterface
52+
{
53+
return new Request($method, $uri, [], new Stream('', null), '1.1');
54+
}
55+
56+
/**
57+
* @inheritDoc
58+
*/
59+
public function createResponse(int $code = 200, string $reasonPhrase = ''): ResponseInterface
60+
{
61+
return new Response($code, [], new Stream('', null), '1.1', $reasonPhrase);
62+
}
63+
64+
/**
65+
* @inheritDoc
66+
*/
67+
public function createServerRequest(string $method, $uri, array $serverParams = []): ServerRequestInterface
68+
{
69+
return new ServerRequest($method, $uri, [], null, '1.1', $serverParams);
70+
}
71+
72+
/**
73+
* @inheritDoc
74+
*/
75+
public function createStream(string $content = ''): StreamInterface
76+
{
77+
return new Stream($content);
78+
}
79+
80+
/**
81+
* @inheritDoc
82+
*/
83+
public function createStreamFromFile(string $filename, string $mode = 'r'): StreamInterface
84+
{
85+
if(empty($filename) || !is_file($filename)){
86+
return new Stream('', null);
87+
}
88+
$resource = @fopen($filename, $mode);
89+
if($resource === FALSE){
90+
return new Stream('', null);
91+
}
92+
93+
return new Stream($resource);
94+
}
95+
96+
/**
97+
* @inheritDoc
98+
*/
99+
public function createStreamFromResource($resource): StreamInterface
100+
{
101+
return new Stream($resource);
102+
}
103+
104+
/**
105+
* @inheritDoc
106+
*/
107+
public function createUploadedFile(StreamInterface $stream, int $size = null, int $error = \UPLOAD_ERR_OK, string $clientFilename = null, string $clientMediaType = null): UploadedFileInterface
108+
{
109+
if($size === null){
110+
$size = $stream->getSize();
111+
}
112+
return new UploadedFile($stream, $size, $error, $clientFilename, $clientMediaType);
113+
}
114+
115+
}

0 commit comments

Comments
 (0)