Version 5.0.0 beta 1

This commit is contained in:
Neo committed 2025-12-19 16:27:35 -08:00
1 parent 25ddeb65d6
commit 15c7beabc5
6736 files changed
+627902 -497943

No files matched your search

+33 -22
View File
@@ -11,16 +11,25 @@
namespace IPS\Helpers\Form;
/* To prevent PHP errors (extending class does not exist) revealing path */
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
use InvalidArgumentException;
use IPS\Member;
use IPS\Theme;
use LengthException;
use function defined;
use function floatval;
use function is_numeric;
if ( !defined( '\IPS\SUITE_UNIQUE_KEY' ) )
{
header( ( isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0' ) . ' 403 Forbidden' );
header( ( $_SERVER['SERVER_PROTOCOL'] ?? 'HTTP/1.0' ) . ' 403 Forbidden' );
exit;
}
/**
* Interval input class for Form Builder
*/
class _Interval extends FormAbstract
class Interval extends FormAbstract
{
/**
* @brief Seconds
@@ -69,7 +78,7 @@ class _Interval extends FormAbstract
);
* @endcode
*/
protected $defaultOptions = array(
protected array $defaultOptions = array(
'valueAs' => self::SECONDS,
'min' => 0,
'max' => NULL,
@@ -87,7 +96,7 @@ class _Interval extends FormAbstract
*
* @return string
*/
public function html()
public function html(): string
{
$valueNumber = '';
$selectedUnit = $this->options['valueAs'];
@@ -136,7 +145,7 @@ class _Interval extends FormAbstract
$minimum = static::convertValue( $this->options['min'], $this->options['valueAs'], static::SECONDS );
}
return \IPS\Theme::i()->getTemplate( 'forms', 'core', 'global' )->interval( $this->name, $valueNumber, $selectedUnit, $this->required, $this->options['unlimited'], $this->options['unlimitedLang'], $this->options['unlimitedToggles'], $this->options['unlimitedToggleOn'], $this->options['valueToggles'], $minimum, $this->options['max'] === NULL ? NULL : static::convertValue( $this->options['max'], $this->options['valueAs'], static::SECONDS ), $this->options['disabled'], $this->suffix );
return Theme::i()->getTemplate( 'forms', 'core', 'global' )->interval( $this->name, $valueNumber, $selectedUnit, $this->required, $this->options['unlimited'], $this->options['unlimitedLang'], $this->options['unlimitedToggles'], $this->options['unlimitedToggleOn'], $this->options['valueToggles'], $minimum, $this->options['max'] === NULL ? NULL : static::convertValue( $this->options['max'], $this->options['valueAs'], static::SECONDS ), $this->options['disabled'], $this->suffix );
}
/**
@@ -144,7 +153,7 @@ class _Interval extends FormAbstract
*
* @return mixed
*/
public function getValue()
public function getValue(): mixed
{
$rawValue = parent::getValue();
@@ -155,9 +164,9 @@ class _Interval extends FormAbstract
else
{
$rawValue['val'] = str_replace( trim( ',' ), '.', $rawValue['val'] );
if ( !\is_numeric( $rawValue['val'] ) )
if ( !is_numeric( $rawValue['val'] ) )
{
throw new \InvalidArgumentException( 'form_number_bad' );
throw new InvalidArgumentException( 'form_number_bad' );
}
return static::convertValue( $rawValue['val'], $rawValue['unit'], $this->options['valueAs'] );
@@ -167,15 +176,15 @@ class _Interval extends FormAbstract
/**
* Convert values
*
* @param int $value The value
* @param string $fromUnit The unit that value is in (see constants)
* @param string $toUnit The unit to convert to (see constants)
* @return int
* @param int $value The value
* @param string $fromUnit The unit that value is in (see constants)
* @param string $toUnit The unit to convert to (see constants)
* @return int|float
*/
public static function convertValue( $value, $fromUnit, $toUnit )
public static function convertValue( int $value, string $fromUnit, string $toUnit ): int|float
{
/* Convert from $fromUnit to seconds */
$value = \floatval( $value );
$value = floatval( $value );
switch ( $fromUnit )
{
case static::WEEKS:
@@ -206,10 +215,10 @@ class _Interval extends FormAbstract
/**
* Get best unit
*
* @param int $value Number of seconds (modified by reference to the returned unit)
* @param int $value Number of seconds (modified by reference to the returned unit)
* @return string
*/
public static function bestUnit( &$value )
public static function bestUnit( int &$value ): string
{
if ( $value >= 604800 and ( $value % 604800 ) === 0 )
{
@@ -240,10 +249,10 @@ class _Interval extends FormAbstract
/**
* Validate
*
* @throws \LengthException
* @throws LengthException
* @return TRUE
*/
public function validate()
public function validate(): bool
{
parent::validate();
@@ -251,21 +260,23 @@ class _Interval extends FormAbstract
{
if ( $this->value === 0 and $this->required )
{
throw new \InvalidArgumentException('form_required');
throw new InvalidArgumentException('form_required');
}
if ( $this->options['min'] !== NULL and $this->value < $this->options['min'] )
{
$minValue = static::convertValue( $this->options['min'], $this->options['valueAs'], static::SECONDS );
$minUnit = static::bestUnit( $minValue );
throw new \LengthException( \IPS\Member::loggedIn()->language()->addToStack('form_interval_min_' . $minUnit, FALSE, array( 'pluralize' => array( $minValue ) ) ) );
throw new LengthException( Member::loggedIn()->language()->addToStack('form_interval_min_' . $minUnit, FALSE, array( 'pluralize' => array( $minValue ) ) ) );
}
if ( $this->options['max'] !== NULL and $this->value > $this->options['max'] )
{
$maxValue = static::convertValue( $this->options['max'], $this->options['valueAs'], static::SECONDS );
$maxUnit = static::bestUnit( $maxValue );
throw new \LengthException( \IPS\Member::loggedIn()->language()->addToStack('form_interval_max_' . $maxUnit, FALSE, array( 'pluralize' => array( $maxValue ) ) ) );
throw new LengthException( Member::loggedIn()->language()->addToStack('form_interval_max_' . $maxUnit, FALSE, array( 'pluralize' => array( $maxValue ) ) ) );
}
}
return TRUE;
}
}