PHP Classes

File: src/Request/Target.php

Recommend this page to a friend!
  Packages of Thierry Feuzeu   Jaxon   src/Request/Target.php   Download  
File: src/Request/Target.php
Role: Class source
Content type: text/plain
Description: Class source
Class: Jaxon
Call PHP classes from JavaScript using AJAX
Author: By
Last change: Improve the generated js code for Jaxon classes.
Changed the format of the Jaxon request parameters.
Merge commit 'ad2bbee761f90bd5be9ed7cafe1099856dc7827b' as 'jaxon-core'
Date: 1 year ago
Size: 3,859 bytes
 

 

Contents

Class file image Download
<?php

/**
 * Target.php - Jaxon Request Target
 *
 * This class contains the name of the function or class and method targetted by a Jaxon request.
 *
 * @package jaxon-core
 * @author Thierry Feuzeu <thierry.feuzeu@gmail.com>
 * @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License
 * @link https://github.com/jaxon-php/jaxon-core
 */

namespace Jaxon\Request;

use function trim;

class Target implements TargetInterface
{
    /**
     * The target type for function.
     *
     * @var string
     */
    const TYPE_FUNCTION = 'TargetFunction';

    /**
     * The target type for class.
     *
     * @var string
     */
    const TYPE_CLASS = 'TargetClass';

    /**
     * The target type.
     *
     * @var string
     */
    private $sType = '';

    /**
     * The target function name.
     *
     * @var string
     */
    private $sFunctionName = '';

    /**
     * The target class name.
     *
     * @var string
     */
    private $sClassName = '';

    /**
     * The target method name.
     *
     * @var string
     */
    private $sMethodName = '';

    /**
     * The target method args.
     *
     * @var array
     */
    private $aMethodArgs = [];

    /**
     * The constructor
     *
     * @param string $sType    The target type
     * @param string $sFunctionName    The function name
     * @param string $sClassName    The class name
     * @param string $sMethodName    The method name
     * @param array $aMethodArgs    The method args
     */
    private function __construct(string $sType, string $sFunctionName, string $sClassName, string $sMethodName, array $aMethodArgs = [])
    {
        $this->sType = $sType;
        $this->sFunctionName = $sFunctionName;
        $this->sClassName = $sClassName;
        $this->sMethodName = $sMethodName;
        $this->aMethodArgs = $aMethodArgs;
    }

    /**
     * Create a target of type Function
     *
     * @param string $sFunctionName    The function name
     *
     * @return Target
     */
    public static function makeFunction(string $sFunctionName): Target
    {
        return new Target(self::TYPE_FUNCTION, $sFunctionName, '', '');
    }

    /**
     * Create a target of type Class
     *
     * @param array $aCall
     *
     * @return Target
     */
    public static function makeClass(array $aCall): Target
    {
        return new Target(self::TYPE_CLASS, '', trim($aCall['name']), trim($aCall['method']));
    }

    /**
     * Check if the target type is Function.
     *
     * @return bool
     */
    public function isFunction(): bool
    {
        return $this->sType === self::TYPE_FUNCTION;
    }

    /**
     * Check if the target type is Class.
     *
     * @return bool
     */
    public function isClass(): bool
    {
        return $this->sType === self::TYPE_CLASS;
    }

    /**
     * The target function name.
     *
     * @return string
     */
    public function getFunctionName(): string
    {
        return $this->sFunctionName;
    }

    /**
     * The target class name.
     *
     * @return string
     */
    public function getClassName(): string
    {
        return $this->sClassName;
    }

    /**
     * The target method name.
     *
     * @return string
     */
    public function getMethodName(): string
    {
        return $this->sMethodName;
    }

    /**
     * Set the target method name.
     *
     * @param array $aMethodArgs
     */
    public function setMethodArgs(array $aMethodArgs): void
    {
        $this->aMethodArgs = $aMethodArgs;
    }

    /**
     * The target method name.
     *
     * @return string
     */
    public function method(): string
    {
        return $this->sMethodName;
    }

    /**
     * The target method args.
     *
     * @return array
     */
    public function args(): array
    {
        return $this->aMethodArgs;
    }
}