Skip to content

Commit ecbd8be

Browse files
committed
Uri class constructor and get methods
1 parent 759967c commit ecbd8be

File tree

4 files changed

+123
-18
lines changed

4 files changed

+123
-18
lines changed

library/http/classes/Message.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
*/
1919
class Message implements MessageInterface
2020
{
21+
2122
/**
2223
* Retrieves the HTTP protocol version as a string.
2324
*
@@ -220,4 +221,4 @@ public function withBody(StreamInterface $body)
220221
{
221222

222223
}
223-
}
224+
}

library/http/classes/Request.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,16 @@
2929
class Request extends Message implements RequestInterface
3030
{
3131

32+
public $protocolVersion = '1.1';
33+
public $httpMethod = 'GET';
34+
public $uri = NULL;
35+
public $headers = array()
36+
public $body = ''
37+
public function __construct($protocolVersion, $httpMethod, $uri, $headers, $body)
38+
{
39+
40+
}
41+
3242

3343
/**
3444
* Retrieves the message's request target.
@@ -153,4 +163,4 @@ public function withUri(UriInterface $uri, $preserveHost = false)
153163
}
154164

155165

156-
}
166+
}

library/http/classes/Uri.php

Lines changed: 75 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,71 @@
2424
*/
2525
class Uri implements UriInterface
2626
{
27-
27+
28+
public $scheme = '';
29+
public $authority = '';
30+
public $userInfo='';
31+
public $host='';
32+
public $port=NULL;
33+
public $path='';
34+
public $query='';
35+
public $fragment='';
36+
37+
public function __construct($uriStr)
38+
{
39+
$uri = parse_url(trim($uriStr));
40+
$this->scheme = strtolower(strval($uri['scheme'])); #if scheme is NULL, strval(NULL) is an empty string
41+
$this->userInfo = strval($uri['user']);
42+
$this->port = $uri['port'];
43+
$this->path = strval($uri['path']);
44+
$this->query = strval($uri['query']);
45+
$this->fragment = strval($uri['fragment']);
46+
47+
#Sets port according to constraints
48+
if ($uri['port'] == getservbyname($this->scheme, 'tcp')) #If port is the default port
49+
{
50+
$this->port = NULL;
51+
}
52+
else
53+
{
54+
$this->port = $uri['port'];
55+
}
56+
57+
#Get around parse_url host bug when no scheme is present
58+
$uriStrTmp = $uriStr;
59+
#Adds a dummy scheme to dummy variable $uriStrTmp to retrieve the host correctly
60+
if(strpos($uriStrTmp,"://")===false && substr($uriStrTmp,0,1)!="/") $uriStrTmp = "http://".$uriStrTmp; #From http://stackoverflow.com/questions/10359347/php-parse-url-domain-retured-as-path-when-protocol-prefix-not-present
61+
$uriTmp = parse_url(trim($uriStrTmp));
62+
63+
64+
$this->host = $uriTmp['host'];
65+
66+
67+
#Construction of authority
68+
if ($this->userInfo !== '')
69+
{
70+
$authorityUserInfo = $this->userInfo."@";
71+
}
72+
else
73+
{
74+
$authorityUserInfo = "";
75+
}
76+
77+
if (is_null($this->port)) #If port is not present
78+
{
79+
$authorityPort = "";
80+
}
81+
else
82+
{
83+
$authorityPort = ":".strval($this->port);
84+
}
85+
86+
$this->authority = $authorityUserInfo.$this->host.$authorityPort;
87+
88+
}
89+
90+
91+
2892
/**
2993
* Retrieve the scheme component of the URI.
3094
*
@@ -41,7 +105,7 @@ class Uri implements UriInterface
41105
*/
42106
public function getScheme()
43107
{
44-
108+
return $this->scheme;
45109
}
46110

47111
/**
@@ -64,7 +128,7 @@ public function getScheme()
64128
*/
65129
public function getAuthority()
66130
{
67-
131+
return $this->authority;
68132
}
69133

70134
/**
@@ -84,7 +148,7 @@ public function getAuthority()
84148
*/
85149
public function getUserInfo()
86150
{
87-
151+
return $this->userInfo;
88152
}
89153

90154
/**
@@ -100,7 +164,7 @@ public function getUserInfo()
100164
*/
101165
public function getHost()
102166
{
103-
167+
return $this->host;
104168
}
105169

106170
/**
@@ -120,7 +184,7 @@ public function getHost()
120184
*/
121185
public function getPort()
122186
{
123-
187+
return $this->port;
124188
}
125189

126190
/**
@@ -150,7 +214,7 @@ public function getPort()
150214
*/
151215
public function getPath()
152216
{
153-
217+
return $this->path;
154218
}
155219

156220
/**
@@ -175,7 +239,7 @@ public function getPath()
175239
*/
176240
public function getQuery()
177241
{
178-
242+
return $this->query;
179243
}
180244

181245
/**
@@ -196,7 +260,7 @@ public function getQuery()
196260
*/
197261
public function getFragment()
198262
{
199-
263+
return $this->fragment;
200264
}
201265

202266
/**
@@ -369,7 +433,7 @@ public function withFragment($fragment)
369433
*/
370434
public function __toString()
371435
{
372-
436+
373437
}
374438

375-
}
439+
}

public/index.php

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,37 @@
1-
<?php
2-
require __DIR__ . '/../vendor/autoload.php';
1+
<html>
2+
<head>
3+
<title>PHP Test</title>
4+
</head>
5+
<body>
6+
<?php
37

4-
# TIP: Use the $_SERVER Sugerglobal to get all the data your need from the Client's HTTP Request.
8+
# TIP: Use the $_SERVER Sugerglobal to get all the data your need from the Client's HTTP Request.
59

6-
# TIP: HTTP headers are printed natively in PHP by invoking header().
7-
# Ex. header('Content-Type', 'text/html');
10+
# TIP: HTTP headers are printed natively in PHP by invoking header().
11+
# Ex. header('Content-Type', 'text/html');
12+
13+
require __DIR__ .'/../vendor/autoload.php';
14+
15+
use pillr\library\http\Message as Message;
16+
use pillr\library\http\Uri as Uri;
17+
18+
$uri_string = "http://pillrcompany.com/interns/test?psr=true";
19+
20+
21+
22+
$test = new Uri($uri_string);
23+
echo $test->getScheme().' ';
24+
echo $test->getHost().' ';
25+
echo $test->getAuthority().' ';
26+
echo $test->getPath().' ';
27+
echo $test->getQuery().' ';
28+
echo $test->getFragment().' ';
29+
30+
31+
32+
33+
34+
35+
?>
36+
</body>
37+
</html>

0 commit comments

Comments
 (0)