From 6ac3c721a16913ce13bf557472accd49f8315194 Mon Sep 17 00:00:00 2001 From: Thierry Feuzeu Date: Fri, 17 May 2024 06:09:22 +0200 Subject: [PATCH] Fixed calls on the JQuery selector. --- src/Plugin/Code/AssetManager.php | 2 +- src/Request/Js/Call.php | 2 +- src/Request/Js/Selector.php | 17 +++------ src/Request/Js/Selector/AttrGet.php | 20 +++++------ src/Request/Js/Selector/AttrSet.php | 20 +++++------ src/Request/Js/Selector/Method.php | 55 ++++++++++++++++++++++++----- 6 files changed, 72 insertions(+), 44 deletions(-) diff --git a/src/Plugin/Code/AssetManager.php b/src/Plugin/Code/AssetManager.php index 93108a9..95be130 100644 --- a/src/Plugin/Code/AssetManager.php +++ b/src/Plugin/Code/AssetManager.php @@ -35,7 +35,7 @@ class AssetManager * * @var string */ - const JS_LIB_URL = 'https://cdn.jsdelivr.net/gh/jaxon-php/jaxon-js@5.0.0rc3/dist'; + const JS_LIB_URL = 'https://cdn.jsdelivr.net/gh/jaxon-php/jaxon-js@5.0.0rc4/dist'; /** * The constructor diff --git a/src/Request/Js/Call.php b/src/Request/Js/Call.php index 90a5447..32a7b44 100644 --- a/src/Request/Js/Call.php +++ b/src/Request/Js/Call.php @@ -460,7 +460,7 @@ public function toArray(): array if($this->bToInt) { $aCalls[] = [ - '_type' => 'func', + '_type' => 'method', '_name' => 'toInt', 'args' => [], ]; diff --git a/src/Request/Js/Selector.php b/src/Request/Js/Selector.php index 51c6ace..6075b8b 100644 --- a/src/Request/Js/Selector.php +++ b/src/Request/Js/Selector.php @@ -205,9 +205,11 @@ private function getPathAsArray() } /** + * Generate the jQuery call, when converting the response into json. + * * @return array */ - public function toArray(): array + public function jsonSerialize(): array { $aCalls = [$this->getPathAsArray()]; foreach($this->aCalls as $xCall) @@ -217,22 +219,11 @@ public function toArray(): array if($this->bToInt) { $aCalls[] = [ - '_type' => 'func', + '_type' => 'method', '_name' => 'toInt', 'args' => [], ]; } return ['_type' => $this->getType(), 'calls' => $aCalls]; } - - /** - * Generate the jQuery call, when converting the response into json. - * This is a method of the JsonSerializable interface. - * - * @return array - */ - public function jsonSerialize(): array - { - return $this->toArray(); - } } diff --git a/src/Request/Js/Selector/AttrGet.php b/src/Request/Js/Selector/AttrGet.php index 4c0d816..967d3a9 100644 --- a/src/Request/Js/Selector/AttrGet.php +++ b/src/Request/Js/Selector/AttrGet.php @@ -24,16 +24,6 @@ public function __construct(string $sAttrName) $this->sAttrName = $sAttrName; } - /** - * Returns a string representation of the script output (javascript) from this call - * - * @return string - */ - public function __toString(): string - { - return '.' . $this->sAttrName; - } - /** * Convert this call to array, when converting the response into json. * @@ -46,4 +36,14 @@ public function jsonSerialize(): array '_name' => $this->sAttrName, ]; } + + /** + * Returns a string representation of the script output (javascript) from this call + * + * @return string + */ + public function __toString(): string + { + return '.' . $this->sAttrName; + } } diff --git a/src/Request/Js/Selector/AttrSet.php b/src/Request/Js/Selector/AttrSet.php index 6393685..ab02b94 100644 --- a/src/Request/Js/Selector/AttrSet.php +++ b/src/Request/Js/Selector/AttrSet.php @@ -35,16 +35,6 @@ public function __construct(string $sAttrName, $xAttrValue) $this->xAttrValue = Parameter::make($xAttrValue); } - /** - * Returns a string representation of this call - * - * @return string - */ - public function __toString(): string - { - return '.' . $this->sAttrName . ' = ' . $this->xAttrValue; - } - /** * Convert this call to array, when converting the response into json. * @@ -58,4 +48,14 @@ public function jsonSerialize(): array 'value' => $this->xAttrValue->jsonSerialize(), ]; } + + /** + * Returns a string representation of this call + * + * @return string + */ + public function __toString(): string + { + return '.' . $this->sAttrName . ' = ' . $this->xAttrValue; + } } diff --git a/src/Request/Js/Selector/Method.php b/src/Request/Js/Selector/Method.php index bb96c89..f198346 100644 --- a/src/Request/Js/Selector/Method.php +++ b/src/Request/Js/Selector/Method.php @@ -2,21 +2,55 @@ namespace Jaxon\Request\Js\Selector; -use Jaxon\Request\Js\Call; +use Jaxon\Request\Js\Parameter; +use JsonSerializable; +use Stringable; -class Method extends Call +use function array_map; +use function implode; + +class Method implements JsonSerializable, Stringable { + /** + * The name of the javascript function + * + * @var string + */ + private $sName; + + /** + * @var array + */ + private $aParameters = []; + /** * The constructor. * - * @param string $sMethod The jQuery function - * @param array $aArguments The arguments of the jQuery function + * @param string $sName The method name + * @param array $aArguments The method arguments + */ + public function __construct(string $sName, array $aArguments) + { + $this->sName = $sName; + $this->aParameters = array_map(function($xArgument) { + return Parameter::make($xArgument); + }, $aArguments); + } + + /** + * Convert this call to array, when converting the response into json. + * + * @return array */ - public function __construct(string $sMethod, array $aArguments) + public function jsonSerialize(): array { - parent::__construct($sMethod); - // Add the arguments to the parameter list - $this->addParameters($aArguments); + return [ + '_type' => 'method', + '_name' => $this->sName, + 'args' => array_map(function(JsonSerializable $xParam) { + return $xParam->jsonSerialize(); + }, $this->aParameters), + ]; } /** @@ -26,6 +60,9 @@ public function __construct(string $sMethod, array $aArguments) */ public function __toString(): string { - return '.' . parent::__toString(); + $aParameters = array_map(function(Stringable $xParam) { + return $xParam->__toString(); + }, $this->aParameters); + return '.' . $this->sName . '(' . implode(', ', $aParameters) . ')'; } }