-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from Art4/requests-v1-support
Add support for Requests v1
- Loading branch information
Showing
6 changed files
with
145 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
/** | ||
* @source https://github.com/WordPress/Requests/blob/v2.0.0/src/Exception/InvalidArgument.php | ||
*/ | ||
|
||
namespace WpOrg\Requests\Exception; | ||
|
||
use InvalidArgumentException; | ||
|
||
/** | ||
* Exception for an invalid argument passed. | ||
* | ||
* @package Requests\Exceptions | ||
* @since 2.0.0 | ||
*/ | ||
final class InvalidArgument extends InvalidArgumentException { | ||
|
||
/** | ||
* Create a new invalid argument exception with a standardized text. | ||
* | ||
* @param int $position The argument position in the function signature. 1-based. | ||
* @param string $name The argument name in the function signature. | ||
* @param string $expected The argument type expected as a string. | ||
* @param string $received The actual argument type received. | ||
* | ||
* @return \WpOrg\Requests\Exception\InvalidArgument | ||
*/ | ||
public static function create($position, $name, $expected, $received) { | ||
// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_debug_backtrace | ||
$stack = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2); | ||
|
||
return new self( | ||
sprintf( | ||
'%s::%s(): Argument #%d (%s) must be of type %s, %s given', | ||
$stack[1]['class'], | ||
$stack[1]['function'], | ||
$position, | ||
$name, | ||
$expected, | ||
$received | ||
) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?php | ||
/** | ||
* Port utilities for Requests | ||
* | ||
* @package Requests\Utilities | ||
* @since 2.0.0 | ||
* @source https://github.com/WordPress/Requests/blob/v2.0.0/src/Port.php | ||
*/ | ||
|
||
namespace WpOrg\Requests; | ||
|
||
use WpOrg\Requests\Exception; | ||
use WpOrg\Requests\Exception\InvalidArgument; | ||
|
||
/** | ||
* Find the correct port depending on the Request type. | ||
* | ||
* @package Requests\Utilities | ||
* @since 2.0.0 | ||
*/ | ||
final class Port { | ||
|
||
/** | ||
* Port to use with Acap requests. | ||
* | ||
* @var int | ||
*/ | ||
const ACAP = 674; | ||
|
||
/** | ||
* Port to use with Dictionary requests. | ||
* | ||
* @var int | ||
*/ | ||
const DICT = 2628; | ||
|
||
/** | ||
* Port to use with HTTP requests. | ||
* | ||
* @var int | ||
*/ | ||
const HTTP = 80; | ||
|
||
/** | ||
* Port to use with HTTP over SSL requests. | ||
* | ||
* @var int | ||
*/ | ||
const HTTPS = 443; | ||
|
||
/** | ||
* Retrieve the port number to use. | ||
* | ||
* @param string $type Request type. | ||
* The following requests types are supported: | ||
* 'acap', 'dict', 'http' and 'https'. | ||
* | ||
* @return int | ||
* | ||
* @throws \WpOrg\Requests\Exception\InvalidArgument When a non-string input has been passed. | ||
* @throws \WpOrg\Requests\Exception When a non-supported port is requested ('portnotsupported'). | ||
*/ | ||
public static function get($type) { | ||
if (!is_string($type)) { | ||
throw InvalidArgument::create(1, '$type', 'string', gettype($type)); | ||
} | ||
|
||
$type = strtoupper($type); | ||
if (!defined("self::{$type}")) { | ||
$message = sprintf('Invalid port type (%s) passed', $type); | ||
throw new Exception($message, 'portnotsupported'); | ||
} | ||
|
||
return constant("self::{$type}"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
if (! class_exists('WpOrg\Requests\Requests') && class_exists('Requests')) { | ||
class_alias('Requests', 'WpOrg\Requests\Requests'); | ||
class_alias('Requests_Exception', 'WpOrg\Requests\Exception'); | ||
class_alias('Requests_Exception_Transport', 'WpOrg\Requests\Exception\Transport'); | ||
class_alias('Requests_IRI', 'WpOrg\Requests\Iri'); | ||
class_alias('Requests_Response', 'WpOrg\Requests\Response'); | ||
class_alias('Requests_Transport', 'WpOrg\Requests\Transport'); | ||
|
||
require_once(dirname(__FILE__) . '/InvalidArgument.php'); | ||
require_once(dirname(__FILE__) . '/Port.php'); | ||
} |