Skip to content

Commit

Permalink
Fixed calls on the JQuery selector.
Browse files Browse the repository at this point in the history
  • Loading branch information
feuzeu committed May 18, 2024
1 parent 40571b6 commit 6ac3c72
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 44 deletions.
2 changes: 1 addition & 1 deletion src/Plugin/Code/AssetManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/Request/Js/Call.php
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ public function toArray(): array
if($this->bToInt)
{
$aCalls[] = [
'_type' => 'func',
'_type' => 'method',
'_name' => 'toInt',
'args' => [],
];
Expand Down
17 changes: 4 additions & 13 deletions src/Request/Js/Selector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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();
}
}
20 changes: 10 additions & 10 deletions src/Request/Js/Selector/AttrGet.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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;
}
}
20 changes: 10 additions & 10 deletions src/Request/Js/Selector/AttrSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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;
}
}
55 changes: 46 additions & 9 deletions src/Request/Js/Selector/Method.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<ParameterInterface>
*/
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),
];
}

/**
Expand All @@ -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) . ')';
}
}

0 comments on commit 6ac3c72

Please sign in to comment.