Skip to content

Commit

Permalink
- use psr style
Browse files Browse the repository at this point in the history
  • Loading branch information
ARACOOOL committed Mar 31, 2015
1 parent 93f39f2 commit da815c4
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 69 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ script:
- phpunit --colors tests/dHttpTest --coverage-clover=coverage.xml

after_success:
- codecov
- codecov
11 changes: 6 additions & 5 deletions dHttp/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function __construct(array $response)
if (isset($response['options'][CURLOPT_HEADER]) && $response['options'][CURLOPT_HEADER]) {
list($headers, $this->_body) = explode("\r\n\r\n", $response['response'], 2);
// Parse headers
$this->_parseHeaders($headers);
$this->parseHeaders($headers);
} else {
$this->_body = $response['response'];
}
Expand Down Expand Up @@ -139,10 +139,11 @@ public function __call($name, $params)
return null;
}

/**
* Parse headers
*/
private function _parseHeaders($headers)
/**
* Parse headers
* @param $headers
*/
private function parseHeaders($headers)
{
$exploded = explode("\r\n", $headers);
foreach($exploded as $headerString) {
Expand Down
126 changes: 63 additions & 63 deletions dHttp/Url.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,76 +7,76 @@
*/
class Url
{
/**
* Validate URL
*
* @param $url
* @return string
* @throws \Exception
*/
public static function validateUrl($url)
{
if (!trim($url)) {
throw new \Exception("Provided URL '$url' cannot be empty");
}
/**
* Validate URL
*
* @param $url
* @return string
* @throws \Exception
*/
public static function validateUrl($url)
{
if (!trim($url)) {
throw new \Exception("Provided URL '$url' cannot be empty");
}

// Split URL into parts first
$parts = parse_url($url);
// Split URL into parts first
$parts = parse_url($url);

if (!$parts) {
throw new \Exception("Error parsing URL '$url'");
}
if (!$parts) {
throw new \Exception("Error parsing URL '$url'");
}

if (!array_key_exists('host', $parts)) {
throw new \Exception("Provided URL '$url' doesn't contain a hostname");
}
if (!array_key_exists('host', $parts)) {
throw new \Exception("Provided URL '$url' doesn't contain a hostname");
}

// Rebuild the URL
$url = self::buildUrl($parts);
// Rebuild the URL
$url = self::buildUrl($parts);

return $url;
}
return $url;
}

/**
* Re-build a URL based on an array of parts
*
* @param array $parts
* @return string
*/
public static function buildUrl(array $parts)
{
$url = '';
$url .= (!empty($parts['scheme'])) ? $parts['scheme'] . '://' : '';
$url .= (!empty($parts['user'])) ? $parts['user'] : '';
$url .= (!empty($parts['pass'])) ? ':' . $parts['pass'] : '';
//If we have a user or pass, make sure to add an "@"
$url .= (!empty($parts['user']) || !empty($parts['pass'])) ? '@' : '';
$url .= (!empty($parts['host'])) ? $parts['host'] : '';
$url .= (!empty($parts['port'])) ? ':' . $parts['port'] : '';
$url .= (!empty($parts['path'])) ? $parts['path'] : '';
$url .= (!empty($parts['query'])) ? '?' . $parts['query'] : '';
$url .= (!empty($parts['fragment'])) ? '#' . $parts['fragment'] : '';
/**
* Re-build a URL based on an array of parts
*
* @param array $parts
* @return string
*/
public static function buildUrl(array $parts)
{
$url = '';
$url .= (!empty($parts['scheme'])) ? $parts['scheme'] . '://' : '';
$url .= (!empty($parts['user'])) ? $parts['user'] : '';
$url .= (!empty($parts['pass'])) ? ':' . $parts['pass'] : '';
//If we have a user or pass, make sure to add an "@"
$url .= (!empty($parts['user']) || !empty($parts['pass'])) ? '@' : '';
$url .= (!empty($parts['host'])) ? $parts['host'] : '';
$url .= (!empty($parts['port'])) ? ':' . $parts['port'] : '';
$url .= (!empty($parts['path'])) ? $parts['path'] : '';
$url .= (!empty($parts['query'])) ? '?' . $parts['query'] : '';
$url .= (!empty($parts['fragment'])) ? '#' . $parts['fragment'] : '';

return $url;
}
return $url;
}

/**
* Checks a passed in IP against a CIDR.
*
* @param string $ip
* @param string|array $ranges
* @return bool
*/
public static function cidr_match($ip, $ranges)
{
$ranges = is_array($ranges) ? $ranges : (array)$ranges;
foreach ($ranges as $range) {
list($subnet, $mask) = explode('/', $range);
if ((ip2long($ip) & ~((1 << (32 - $mask)) - 1)) == ip2long($subnet)) {
return true;
}
}
/**
* Checks a passed in IP against a CIDR.
*
* @param string $ip
* @param string|array $ranges
* @return bool
*/
public static function cidrMatch($ip, $ranges)
{
$ranges = is_array($ranges) ? $ranges : (array)$ranges;
foreach ($ranges as $range) {
list($subnet, $mask) = explode('/', $range);
if ((ip2long($ip) & ~((1 << (32 - $mask)) - 1)) == ip2long($subnet)) {
return true;
}
}

return false;
}
return false;
}
}

0 comments on commit da815c4

Please sign in to comment.