Source of file ValueTransform.php

Size: 1,678 Bytes - Last Modified: 2018-11-03T09:50:48-04:00

G:/AdobeConnectClient/src/Helpers/ValueTransform.php

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
<?php

namespace AdobeConnectClient\Helpers;

use DateTimeInterface;
use DateTime;
use DateTimeImmutable;

/**
 * Converts the value into a type.
 */
abstract class ValueTransform
{
    /**
     * Converts arbitrary value into string
     *
     * @param mixed $value
     * @return string
     */
    public static function toString($value)
    {
        if (is_string($value)) {
            return $value;
        }

        if (is_bool($value)) {
            return $value ? 'true' : 'false';
        }

        if ($value instanceof DateTimeInterface) {
            return $value->format(DateTime::W3C);
        }

        return (string) $value;
    }

    /**
     * Converts arbitrary value into DateTimeImmutable
     *
     * @param mixed $value
     * @return DateTimeImmutable
     * @throws \Exception
     */
    public static function toDateTimeImmutable($value)
    {
        if ($value instanceof DateTimeImmutable) {
            return $value;
        }

        if ($value instanceof DateTime) {
            return DateTimeImmutable::createFromMutable($value);
        }

        return new DateTimeImmutable((string) $value);
    }

    /**
     * Transform the value into a boolean type.
     *
     * @param  mixed $value The value to transform
     * @return bool
     */
    public static function toBool($value)
    {
        if (!is_string($value)) {
            return boolval($value);
        }

        $value = mb_strtolower($value);

        if ($value === 'false' or $value === 'off') {
            return false;
        } elseif ($value === 'true' or $value === 'on') {
            return true;
        }

        return boolval($value);
    }
}