This repository has been archived by the owner on Apr 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added command transaction interface and implementation
Adjusted request and response return types in both interfaces and implementations. Added factories to the package. Simplified README for easier maintenance.
- Loading branch information
1 parent
6e5ce42
commit 6caa9ff
Showing
15 changed files
with
579 additions
and
55 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
/** | ||
* Copyright (C) Jyxon, Inc. All rights reserved. | ||
* See LICENSE for license details. | ||
*/ | ||
|
||
namespace Ulrack\Transaction\Common; | ||
|
||
interface CommandInterface | ||
{ | ||
/** | ||
* Returns the command. | ||
* | ||
* @return string | ||
*/ | ||
public function getCommand(): string; | ||
|
||
/** | ||
* Returns the parameters. | ||
* | ||
* @return array | ||
*/ | ||
public function getParameters(): array; | ||
|
||
/** | ||
* Returns the options. | ||
* | ||
* @return array | ||
*/ | ||
public function getOptions(): array; | ||
|
||
/** | ||
* Returns the flags. | ||
* | ||
* @return array | ||
*/ | ||
public function getFlags(): array; | ||
} |
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,27 @@ | ||
<?php | ||
/** | ||
* Copyright (C) Jyxon, Inc. All rights reserved. | ||
* See LICENSE for license details. | ||
*/ | ||
|
||
namespace Ulrack\Transaction\Exception; | ||
|
||
use Exception; | ||
|
||
class InvalidRequestException extends Exception | ||
{ | ||
/** | ||
* Constructor | ||
* | ||
* @param string $message | ||
*/ | ||
public function __construct(string $message) | ||
{ | ||
parent::__construct( | ||
sprintf( | ||
'Invalid request: %s.', | ||
$message | ||
) | ||
); | ||
} | ||
} |
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,78 @@ | ||
<?php | ||
/** | ||
* Copyright (C) Jyxon, Inc. All rights reserved. | ||
* See LICENSE for license details. | ||
*/ | ||
|
||
namespace Ulrack\Transaction\Factory; | ||
|
||
use Ulrack\Transaction\Transaction\Command; | ||
use Ulrack\Transaction\Common\CommandInterface; | ||
|
||
class CommandFactory | ||
{ | ||
/** | ||
* Creates a CLI command request. | ||
* | ||
* @param array $arguments The arguments passed to the command line. | ||
* | ||
* @return CommandInterface | ||
*/ | ||
public static function create(array $arguments): CommandInterface | ||
{ | ||
return new Command( | ||
...static::prepareArguments( | ||
$arguments | ||
) | ||
); | ||
} | ||
|
||
/** | ||
* Prepare the CLI arguments. | ||
* | ||
* @return array | ||
*/ | ||
private static function prepareArguments(array $arguments): array | ||
{ | ||
// Strip the script | ||
array_shift($arguments); | ||
|
||
$command = array_shift($arguments); | ||
$flags = []; | ||
$options = []; | ||
$parameters = []; | ||
while ($argument = array_shift($arguments)) { | ||
// First find out if there is a flag or parameter passed. | ||
if (substr($argument, 0, 1) === '-') { | ||
// The --parameter=value markdown is used | ||
if (strpos($argument, '=') > 0) { | ||
$expArg = explode('=', $argument); | ||
$parameters[ltrim($expArg[0], '-')] = $expArg[1]; | ||
|
||
continue; | ||
} | ||
|
||
// No subsequent value starting without a "-" | ||
// It must be a flag | ||
if (empty($arguments[0]) | ||
|| substr($arguments[0], 0, 1) === '-') { | ||
$flags[] = ltrim($argument, '-'); | ||
|
||
continue; | ||
} | ||
|
||
// There was a subsequent value starting without a "-" | ||
// It counts as a parameter | ||
$argument = ltrim($argument, '-'); | ||
$parameters[$argument] = array_shift($arguments); | ||
|
||
continue; | ||
} | ||
|
||
// Additional arguments go into the options array | ||
$options[] = $argument; | ||
} | ||
|
||
return [$command, $parameters, $options, $flags]; | ||
} | ||
} |
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,92 @@ | ||
<?php | ||
/** | ||
* Copyright (C) Jyxon, Inc. All rights reserved. | ||
* See LICENSE for license details. | ||
*/ | ||
|
||
namespace Ulrack\Transaction\Factory; | ||
|
||
use Ulrack\Transaction\Common\MethodEnum; | ||
use Ulrack\Transaction\Transaction\Request; | ||
use Ulrack\Transaction\Common\RequestInterface; | ||
use Ulrack\Transaction\Exception\InvalidRequestException; | ||
|
||
class RequestFactory | ||
{ | ||
/** | ||
* Creates a Web request. | ||
* | ||
* @param array $server The server parameters. | ||
* @param array $get The get parameters. | ||
* @param array $post The post payload. | ||
* | ||
* @return RequestInterface | ||
* | ||
* @throws InvalidRequestException When a invalid request method is detected. | ||
*/ | ||
public static function create( | ||
array $server, | ||
array $get = [], | ||
array $post = [] | ||
): RequestInterface { | ||
$methods = MethodEnum::getOptions(); | ||
if (!isset($methods[$server['REQUEST_METHOD']])) { | ||
throw new InvalidRequestException('Invalid request method.'); | ||
} | ||
|
||
return new Request( | ||
new MethodEnum($methods[$server['REQUEST_METHOD']]), | ||
strtok($server['REQUEST_URI'], '?'), | ||
$get, | ||
$post, | ||
static::parseHeaders($server) | ||
); | ||
} | ||
|
||
/** | ||
* Parse the headers to normalized key value pairs. | ||
* | ||
* @param array $server | ||
* | ||
* @return array | ||
*/ | ||
private static function parseHeaders(array $server): array | ||
{ | ||
$headers = []; | ||
foreach ($server as $key => $header) { | ||
if (substr($key, 0, 5) === 'HTTP_') { | ||
$headers[static::normalizeKey(substr($key, 5))] = $header; | ||
|
||
continue; | ||
} | ||
|
||
$headers[static::normalizeKey($key)] = $header; | ||
} | ||
|
||
return $headers; | ||
} | ||
|
||
/** | ||
* Normalize key name to standard format. | ||
* | ||
* @param string $key | ||
* | ||
* @return string | ||
*/ | ||
private static function normalizeKey(string $key): string | ||
{ | ||
return str_replace( | ||
' ', | ||
'-', | ||
ucwords( | ||
strtolower( | ||
str_replace( | ||
'_', | ||
' ', | ||
$key | ||
) | ||
) | ||
) | ||
); | ||
} | ||
} |
Oops, something went wrong.