PHP Classes

File: src/Script/Action/Func.php

Recommend this page to a friend!
  Packages of Thierry Feuzeu   Jaxon   src/Script/Action/Func.php   Download  
File: src/Script/Action/Func.php
Role: Class source
Content type: text/plain
Description: Class source
Class: Jaxon
Call PHP classes from JavaScript using AJAX
Author: By
Last change:
Date: 6 months ago
Size: 1,279 bytes
 

Contents

Class file image Download
<?php

namespace Jaxon\Script\Action;

use
JsonSerializable;
use function
array_map;

class
Func extends TypedValue
{
   
/**
     * The constructor.
     *
     * @param string $sName The method name
     * @param array $aArguments The method arguments
     */
   
public function __construct(private string $sName, private array $aArguments)
    {}

   
/**
     * @inheritDoc
     */
   
public function getType(): string
   
{
        return
'func';
    }

   
/**
     * Add the page number to the function arguments.
     *
     * @return self
     */
   
public function withPage(): self
   
{
        foreach(
$this->aArguments as $xArgument)
        {
            if(
TypedValue::isPage($xArgument))
            {
                return
$this;
            }
        }
       
$this->aArguments[] = TypedValue::page();
        return
$this;
    }

   
/**
     * Convert this call to array, when converting the response into json.
     *
     * @return array
     */
   
public function jsonSerialize(): array
    {
        return [
           
'_type' => $this->getType(),
           
'_name' => $this->sName,
           
'args' => array_map(fn(mixed $xArgument) =>
               
TypedValue::make($xArgument)->jsonSerialize(), $this->aArguments),
        ];
    }
}