Source of file RequestArg.php

Size: 1,621 Bytes - Last Modified: 2019-08-09T18:27:54+00:00

/home/travis/build/zerospam/sdk-framework/src/Request/Arguments/RequestArg.php

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
<?php
/**
 * Created by PhpStorm.
 * User: aaflalo
 * Date: 30/05/18
 * Time: 4:25 PM.
 */

namespace ZEROSPAM\Framework\SDK\Request\Arguments;

use ZEROSPAM\Framework\SDK\Utils\Contracts\PrimalValued;

/**
 * Class RequestArg
 *
 * Basic query argument
 *
 * @package ZEROSPAM\Framework\SDK\Request\Arguments
 */
class RequestArg implements IArgument
{
    /**
     * @var string
     */
    protected $key;

    /** @var string */
    protected $value;

    /**
     * RequestArg constructor.
     *
     * @param string       $key
     * @param string|array $value
     */
    public function __construct($key, $value)
    {
        if (is_null($key)) {
            throw new \InvalidArgumentException("Key can't be null");
        }
        if (!is_string($key)) {
            throw new \InvalidArgumentException('The key need to be a string');
        }
        $this->key   = $key;
        $this->value = $value;
    }

    /**
     * Key for the argument.
     *
     * @return string
     */
    public function getKey(): string
    {
        return $this->key;
    }

    public function toPrimitive()
    {
        $value = $this->value;
        if ($value instanceof PrimalValued) {
            $value = $value->toPrimitive();
        }

        if (is_bool($value)) {
            return (int)$value;
        }

        if (is_float($value)) {
            return str_replace(',', '.', $value);
        }

        return $value;
    }

    /**
     * Possible Subkey of the argument
     *
     * @return string
     */
    public function getSubKey(): string
    {
        return $this->toPrimitive();
    }
}