Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use extracted serialization package #69

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"php": "~7.1",
"php-http/httplug": "~1",
"php-http/discovery": "^1.0",
"php-http/message-factory": "^1.0"
"php-http/message-factory": "^1.0",
"fxmlrpc/serialization": "^0.2.0"
},
"require-dev": {
"zendframework/zend-log": "~2",
Expand Down
156 changes: 0 additions & 156 deletions src/fXmlRpc/CodeGenerator/XmlReaderParserBitmaskGenerator.php

This file was deleted.

16 changes: 8 additions & 8 deletions src/fXmlRpc/Exception/FaultException.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,6 @@ final class FaultException extends RuntimeException
{
private $faultCode;

public static function fault($response)
{
$exception = new static(isset($response['faultString']) ? $response['faultString'] : 'Unknown');
$exception->faultCode = isset($response['faultCode']) ? $response['faultCode'] : 0;

return $exception;
}

public function getFaultString()
{
return $this->getMessage();
Expand All @@ -44,4 +36,12 @@ public function getFaultCode()
{
return $this->faultCode;
}

public static function fromFault(\Fxmlrpc\Serialization\Exception\FaultException $fault)
{
$e = new static($fault->getMessage(), 0, $fault);
$e->faultCode = $fault->getCode();

return $e;
}
}
35 changes: 0 additions & 35 deletions src/fXmlRpc/Exception/ParserException.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,39 +25,4 @@

final class ParserException extends RuntimeException
{
public static function unexpectedTag($tagName, $elements, array $definedVariables, $depth, $xml)
{
$expectedElements = [];
foreach ($definedVariables as $variableName => $variable) {
if (substr($variableName, 0, 4) !== 'flag') {
continue;
}

if (($elements & $variable) === $variable) {
$expectedElements[] = substr($variableName, 4);
}
}

return new static(
sprintf(
'Invalid XML. Expected one of "%s", got "%s" on depth %d (context: "%s")',
implode('", "', $expectedElements),
$tagName,
$depth,
$xml
)
);
}

public static function notXml($string)
{
return new static(sprintf('Invalid XML. Expected XML, string given: "%s"', $string));
}

public static function xmlrpcExtensionLibxmlParsehugeNotSupported()
{
return new static(
'Parsing huge XML responses using libxml’s LIBXML_PARSEHUGE flag is not supported in ext/xmlrpc'
);
}
}
4 changes: 0 additions & 4 deletions src/fXmlRpc/Exception/SerializationException.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,4 @@

final class SerializationException extends RuntimeException
{
public static function invalidType($value)
{
return new static(sprintf('Could not serialize %s of type "%s"', gettype($value), get_resource_type($value)));
}
}
18 changes: 2 additions & 16 deletions src/fXmlRpc/Parser/BestParserDelegate.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,10 @@
*/
namespace fXmlRpc\Parser;

final class BestParserDelegate implements ParserInterface
final class BestParserDelegate extends ParserWrapper
{
/** @var NativeParser */
private $nativeParser;

/** @var XmlReaderParser */
private $xmlReaderParser;

public function __construct($validateResponse = true)
{
$this->nativeParser = new NativeParser($validateResponse);
$this->xmlReaderParser = new XmlReaderParser($validateResponse);
}

public function parse($xmlString)
{
return !NativeParser::isBiggerThanParseLimit($xmlString)
? $this->nativeParser->parse($xmlString)
: $this->xmlReaderParser->parse($xmlString);
parent::__construct(new \Fxmlrpc\Serialization\Parser\BestParserDelegate($validateResponse));
}
}
77 changes: 2 additions & 75 deletions src/fXmlRpc/Parser/NativeParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,89 +23,16 @@
*/
namespace fXmlRpc\Parser;

use DateTime;
use DateTimeZone;
use fXmlRpc\Exception\FaultException;
use fXmlRpc\Exception\MissingExtensionException;
use fXmlRpc\Exception\ParserException;
use fXmlRpc\Value\Base64;

final class NativeParser implements ParserInterface
final class NativeParser extends ParserWrapper
{
const LIBXML_PARSEHUGE_THRESHOLD = 1024 * 1024 * 10;

/**
* @var bool
*/
private $validateResponse;

public function __construct($validateResponse = true)
{
if (!extension_loaded('xmlrpc')) {
throw MissingExtensionException::extensionMissing('xmlrpc');
}
$this->validateResponse = $validateResponse;
}

/**
* @param string $xmlString
* @return bool
*/
public static function isBiggerThanParseLimit($xmlString)
{
return strlen($xmlString) > static::LIBXML_PARSEHUGE_THRESHOLD;
}

/** {@inheritdoc} */
public function parse($xmlString)
{
if ($this->validateResponse) {
XmlChecker::validXml($xmlString);
}

$result = xmlrpc_decode($xmlString, 'UTF-8');

if ($result === null && self::isBiggerThanParseLimit($xmlString)) {
throw ParserException::xmlrpcExtensionLibxmlParsehugeNotSupported();
}

$toBeVisited = [&$result];
while (isset($toBeVisited[0]) && $value = &$toBeVisited[0]) {

$type = gettype($value);
if ($type === 'object') {
$xmlRpcType = $value->{'xmlrpc_type'};
if ($xmlRpcType === 'datetime') {
$value = DateTime::createFromFormat(
'Ymd\TH:i:s',
$value->scalar,
isset($timezone) ? $timezone : $timezone = new DateTimeZone('UTC')
);

} elseif ($xmlRpcType === 'base64') {
if ($value->scalar !== '') {
$value = Base64::serialize($value->scalar);
} else {
$value = null;
}
}

} elseif ($type === 'array') {
foreach ($value as &$element) {
$toBeVisited[] = &$element;
}
}

array_shift($toBeVisited);
}

if (is_array($result)) {
reset($result);
if (xmlrpc_is_fault($result)) {
throw FaultException::fault($result);
}
}

return $result;
parent::__construct(new \Fxmlrpc\Serialization\Parser\NativeParser($validateResponse));
}
}
Loading