2016-12-17 17:52:36 +00:00
|
|
|
<?php
|
|
|
|
|
2020-12-03 22:22:43 +00:00
|
|
|
namespace MediaWiki\Extension\AbuseFilter\Parser;
|
|
|
|
|
|
|
|
use InvalidArgumentException;
|
2021-08-29 22:58:17 +00:00
|
|
|
use MediaWiki\Extension\AbuseFilter\Parser\Exception\InternalException;
|
|
|
|
use MediaWiki\Extension\AbuseFilter\Parser\Exception\UserVisibleException;
|
2019-09-15 15:48:13 +00:00
|
|
|
use RuntimeException;
|
2020-12-03 22:22:43 +00:00
|
|
|
|
2016-12-17 17:52:36 +00:00
|
|
|
class AFPData {
|
|
|
|
// Datatypes
|
2020-01-21 11:13:11 +00:00
|
|
|
public const DINT = 'int';
|
|
|
|
public const DSTRING = 'string';
|
|
|
|
public const DNULL = 'null';
|
|
|
|
public const DBOOL = 'bool';
|
|
|
|
public const DFLOAT = 'float';
|
|
|
|
public const DARRAY = 'array';
|
2019-05-23 10:55:20 +00:00
|
|
|
// Special purpose type for non-initialized stuff
|
2020-01-21 11:13:11 +00:00
|
|
|
public const DUNDEFINED = 'undefined';
|
2016-12-17 17:52:36 +00:00
|
|
|
|
2019-08-12 12:23:46 +00:00
|
|
|
/**
|
|
|
|
* Translation table mapping shell-style wildcards to PCRE equivalents.
|
|
|
|
* Derived from <http://www.php.net/manual/en/function.fnmatch.php#100207>
|
|
|
|
* @internal
|
|
|
|
*/
|
2019-11-16 15:32:36 +00:00
|
|
|
public const WILDCARD_MAP = [
|
2016-12-17 17:52:36 +00:00
|
|
|
'\*' => '.*',
|
|
|
|
'\+' => '\+',
|
|
|
|
'\-' => '\-',
|
|
|
|
'\.' => '\.',
|
|
|
|
'\?' => '.',
|
|
|
|
'\[' => '[',
|
|
|
|
'\[\!' => '[^',
|
|
|
|
'\\' => '\\\\',
|
|
|
|
'\]' => ']',
|
2017-06-15 14:23:34 +00:00
|
|
|
];
|
2016-12-17 17:52:36 +00:00
|
|
|
|
2018-11-08 14:34:32 +00:00
|
|
|
/**
|
|
|
|
* @var string One of the D* const from this class
|
2022-09-29 16:54:36 +00:00
|
|
|
* @internal Use $this->getType() instead
|
2018-11-08 14:34:32 +00:00
|
|
|
*/
|
2016-12-17 17:52:36 +00:00
|
|
|
public $type;
|
2018-11-08 14:34:32 +00:00
|
|
|
/**
|
|
|
|
* @var mixed|null|AFPData[] The actual data contained in this object
|
2022-09-29 16:54:36 +00:00
|
|
|
* @internal Use $this->getData() instead
|
2018-11-08 14:34:32 +00:00
|
|
|
*/
|
2016-12-17 17:52:36 +00:00
|
|
|
public $data;
|
|
|
|
|
2019-01-24 10:10:22 +00:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getType() {
|
|
|
|
return $this->type;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return AFPData[]|mixed|null
|
|
|
|
*/
|
|
|
|
public function getData() {
|
|
|
|
return $this->data;
|
|
|
|
}
|
|
|
|
|
2016-12-17 17:52:36 +00:00
|
|
|
/**
|
|
|
|
* @param string $type
|
2019-01-24 10:10:22 +00:00
|
|
|
* @param AFPData[]|mixed|null $val
|
2016-12-17 17:52:36 +00:00
|
|
|
*/
|
2018-12-08 18:20:04 +00:00
|
|
|
public function __construct( $type, $val = null ) {
|
2023-04-13 05:27:38 +00:00
|
|
|
if ( $type === self::DUNDEFINED && $val !== null ) {
|
2019-08-12 09:18:15 +00:00
|
|
|
// Sanity
|
2023-04-13 05:27:38 +00:00
|
|
|
throw new InvalidArgumentException( 'DUNDEFINED cannot have a non-null value' );
|
2019-08-02 11:49:34 +00:00
|
|
|
}
|
2016-12-17 17:52:36 +00:00
|
|
|
$this->type = $type;
|
|
|
|
$this->data = $val;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-10-06 18:52:31 +00:00
|
|
|
* @param mixed $var
|
2016-12-17 17:52:36 +00:00
|
|
|
* @return AFPData
|
2021-08-29 22:58:17 +00:00
|
|
|
* @throws InternalException
|
2016-12-17 17:52:36 +00:00
|
|
|
*/
|
|
|
|
public static function newFromPHPVar( $var ) {
|
2019-05-25 08:41:22 +00:00
|
|
|
switch ( gettype( $var ) ) {
|
|
|
|
case 'string':
|
|
|
|
return new AFPData( self::DSTRING, $var );
|
|
|
|
case 'integer':
|
|
|
|
return new AFPData( self::DINT, $var );
|
|
|
|
case 'double':
|
|
|
|
return new AFPData( self::DFLOAT, $var );
|
|
|
|
case 'boolean':
|
|
|
|
return new AFPData( self::DBOOL, $var );
|
|
|
|
case 'array':
|
|
|
|
$result = [];
|
|
|
|
foreach ( $var as $item ) {
|
|
|
|
$result[] = self::newFromPHPVar( $item );
|
|
|
|
}
|
|
|
|
return new AFPData( self::DARRAY, $result );
|
|
|
|
case 'NULL':
|
2019-05-23 10:55:20 +00:00
|
|
|
return new AFPData( self::DNULL );
|
2019-05-25 08:41:22 +00:00
|
|
|
default:
|
2021-08-29 22:58:17 +00:00
|
|
|
throw new InternalException(
|
2024-08-12 20:54:14 +00:00
|
|
|
'Data type ' . get_debug_type( $var ) . ' is not supported by AbuseFilter'
|
2019-05-25 08:41:22 +00:00
|
|
|
);
|
2016-12-17 17:52:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-10-06 18:52:31 +00:00
|
|
|
* @param AFPData $orig
|
|
|
|
* @param string $target
|
2016-12-17 17:52:36 +00:00
|
|
|
* @return AFPData
|
|
|
|
*/
|
2018-10-03 15:19:40 +00:00
|
|
|
public static function castTypes( AFPData $orig, $target ) {
|
2018-08-26 08:34:42 +00:00
|
|
|
if ( $orig->type === $target ) {
|
2019-10-02 11:35:38 +00:00
|
|
|
return $orig;
|
2016-12-17 17:52:36 +00:00
|
|
|
}
|
2019-08-12 09:18:15 +00:00
|
|
|
if ( $orig->type === self::DUNDEFINED ) {
|
|
|
|
// This case should be handled at a higher level, to avoid implicitly relying on what
|
|
|
|
// this method will do for the specific case.
|
2021-08-29 22:58:17 +00:00
|
|
|
throw new InternalException( 'Refusing to cast DUNDEFINED to something else' );
|
2019-08-12 09:18:15 +00:00
|
|
|
}
|
2018-08-26 08:34:42 +00:00
|
|
|
if ( $target === self::DNULL ) {
|
2019-04-14 12:08:57 +00:00
|
|
|
// We don't expose any method to cast to null. And, actually, should we?
|
2019-05-23 10:55:20 +00:00
|
|
|
return new AFPData( self::DNULL );
|
2016-12-17 17:52:36 +00:00
|
|
|
}
|
|
|
|
|
2018-08-26 08:34:42 +00:00
|
|
|
if ( $orig->type === self::DARRAY ) {
|
|
|
|
if ( $target === self::DBOOL ) {
|
2016-12-17 17:52:36 +00:00
|
|
|
return new AFPData( self::DBOOL, (bool)count( $orig->data ) );
|
2018-08-26 08:34:42 +00:00
|
|
|
} elseif ( $target === self::DFLOAT ) {
|
2016-12-17 17:52:36 +00:00
|
|
|
return new AFPData( self::DFLOAT, floatval( count( $orig->data ) ) );
|
2018-08-26 08:34:42 +00:00
|
|
|
} elseif ( $target === self::DINT ) {
|
2019-10-09 10:37:38 +00:00
|
|
|
return new AFPData( self::DINT, count( $orig->data ) );
|
2018-08-26 08:34:42 +00:00
|
|
|
} elseif ( $target === self::DSTRING ) {
|
2016-12-17 17:52:36 +00:00
|
|
|
$s = '';
|
|
|
|
foreach ( $orig->data as $item ) {
|
|
|
|
$s .= $item->toString() . "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
return new AFPData( self::DSTRING, $s );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-26 08:34:42 +00:00
|
|
|
if ( $target === self::DBOOL ) {
|
2016-12-17 17:52:36 +00:00
|
|
|
return new AFPData( self::DBOOL, (bool)$orig->data );
|
2018-08-26 08:34:42 +00:00
|
|
|
} elseif ( $target === self::DFLOAT ) {
|
2016-12-17 17:52:36 +00:00
|
|
|
return new AFPData( self::DFLOAT, floatval( $orig->data ) );
|
2018-08-26 08:34:42 +00:00
|
|
|
} elseif ( $target === self::DINT ) {
|
2016-12-17 17:52:36 +00:00
|
|
|
return new AFPData( self::DINT, intval( $orig->data ) );
|
2018-08-26 08:34:42 +00:00
|
|
|
} elseif ( $target === self::DSTRING ) {
|
2016-12-17 17:52:36 +00:00
|
|
|
return new AFPData( self::DSTRING, strval( $orig->data ) );
|
2018-08-26 08:34:42 +00:00
|
|
|
} elseif ( $target === self::DARRAY ) {
|
2019-04-14 12:08:57 +00:00
|
|
|
// We don't expose any method to cast to array
|
2018-04-16 15:37:10 +00:00
|
|
|
return new AFPData( self::DARRAY, [ $orig ] );
|
2016-12-17 17:52:36 +00:00
|
|
|
}
|
2021-08-29 22:58:17 +00:00
|
|
|
throw new InternalException( 'Cannot cast ' . $orig->type . " to $target." );
|
2016-12-17 17:52:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return AFPData
|
|
|
|
*/
|
2019-08-12 11:53:38 +00:00
|
|
|
public function boolInvert() {
|
|
|
|
if ( $this->type === self::DUNDEFINED ) {
|
2019-08-03 15:52:14 +00:00
|
|
|
return new AFPData( self::DUNDEFINED );
|
2018-12-08 18:20:04 +00:00
|
|
|
}
|
2019-08-12 11:53:38 +00:00
|
|
|
return new AFPData( self::DBOOL, !$this->toBool() );
|
2016-12-17 17:52:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-10-06 18:52:31 +00:00
|
|
|
* @param AFPData $exponent
|
2016-12-17 17:52:36 +00:00
|
|
|
* @return AFPData
|
|
|
|
*/
|
2019-08-12 11:53:38 +00:00
|
|
|
public function pow( AFPData $exponent ) {
|
|
|
|
if ( $this->type === self::DUNDEFINED || $exponent->type === self::DUNDEFINED ) {
|
2019-08-03 15:52:14 +00:00
|
|
|
return new AFPData( self::DUNDEFINED );
|
2018-12-08 18:20:04 +00:00
|
|
|
}
|
2019-08-12 11:53:38 +00:00
|
|
|
$res = pow( $this->toNumber(), $exponent->toNumber() );
|
2018-10-03 15:19:40 +00:00
|
|
|
$type = is_int( $res ) ? self::DINT : self::DFLOAT;
|
|
|
|
|
|
|
|
return new AFPData( $type, $res );
|
2016-12-17 17:52:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-10-06 18:52:31 +00:00
|
|
|
* @param AFPData $d2
|
2018-03-25 10:03:26 +00:00
|
|
|
* @param bool $strict whether to also check types
|
2016-12-17 17:52:36 +00:00
|
|
|
* @return bool
|
2021-08-29 22:58:17 +00:00
|
|
|
* @throws InternalException if $this or $d2 is a DUNDEFINED. This shouldn't happen, because this method
|
2019-08-02 11:49:34 +00:00
|
|
|
* only returns a boolean, and thus the type of the result has already been decided and cannot
|
2019-08-03 15:52:14 +00:00
|
|
|
* be changed to be a DUNDEFINED from here.
|
2019-08-02 11:49:34 +00:00
|
|
|
* @internal
|
2016-12-17 17:52:36 +00:00
|
|
|
*/
|
2019-08-12 11:53:38 +00:00
|
|
|
public function equals( AFPData $d2, $strict = false ) {
|
|
|
|
if ( $this->type === self::DUNDEFINED || $d2->type === self::DUNDEFINED ) {
|
2021-08-29 22:58:17 +00:00
|
|
|
throw new InternalException(
|
2019-08-03 15:52:14 +00:00
|
|
|
__METHOD__ . " got a DUNDEFINED. This should be handled at a higher level"
|
2019-08-02 11:49:34 +00:00
|
|
|
);
|
2019-08-12 11:53:38 +00:00
|
|
|
} elseif ( $this->type !== self::DARRAY && $d2->type !== self::DARRAY ) {
|
|
|
|
$typecheck = $this->type === $d2->type || !$strict;
|
|
|
|
return $typecheck && $this->toString() === $d2->toString();
|
|
|
|
} elseif ( $this->type === self::DARRAY && $d2->type === self::DARRAY ) {
|
|
|
|
$data1 = $this->data;
|
2018-03-25 10:03:26 +00:00
|
|
|
$data2 = $d2->data;
|
|
|
|
if ( count( $data1 ) !== count( $data2 ) ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$length = count( $data1 );
|
|
|
|
for ( $i = 0; $i < $length; $i++ ) {
|
2019-08-02 11:49:34 +00:00
|
|
|
// @phan-suppress-next-line PhanTypeArraySuspiciousNullable Array type
|
2019-08-12 11:53:38 +00:00
|
|
|
if ( $data1[$i]->equals( $data2[$i], $strict ) === false ) {
|
2018-03-25 10:03:26 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
// Trying to compare an array to something else
|
2018-04-24 15:56:29 +00:00
|
|
|
if ( $strict ) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-08-12 11:53:38 +00:00
|
|
|
if ( $this->type === self::DARRAY && count( $this->data ) === 0 ) {
|
2018-08-26 08:34:42 +00:00
|
|
|
return ( $d2->type === self::DBOOL && $d2->toBool() === false ) || $d2->type === self::DNULL;
|
|
|
|
} elseif ( $d2->type === self::DARRAY && count( $d2->data ) === 0 ) {
|
2019-08-12 11:53:38 +00:00
|
|
|
return ( $this->type === self::DBOOL && $this->toBool() === false ) ||
|
|
|
|
$this->type === self::DNULL;
|
2018-04-24 15:56:29 +00:00
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
2018-03-25 10:03:26 +00:00
|
|
|
}
|
2016-12-17 17:52:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return AFPData
|
|
|
|
*/
|
2019-08-12 11:53:38 +00:00
|
|
|
public function unaryMinus() {
|
|
|
|
if ( $this->type === self::DUNDEFINED ) {
|
2019-08-03 15:52:14 +00:00
|
|
|
return new AFPData( self::DUNDEFINED );
|
2019-08-12 11:53:38 +00:00
|
|
|
} elseif ( $this->type === self::DINT ) {
|
|
|
|
return new AFPData( $this->type, -$this->toInt() );
|
2016-12-17 17:52:36 +00:00
|
|
|
} else {
|
2023-04-13 05:27:38 +00:00
|
|
|
return new AFPData( $this->type, -$this->toFloat() );
|
2016-12-17 17:52:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-10-06 18:52:31 +00:00
|
|
|
* @param AFPData $b
|
|
|
|
* @param string $op
|
2016-12-17 17:52:36 +00:00
|
|
|
* @return AFPData
|
2021-08-29 22:58:17 +00:00
|
|
|
* @throws InternalException
|
2016-12-17 17:52:36 +00:00
|
|
|
*/
|
2019-08-12 12:40:51 +00:00
|
|
|
public function boolOp( AFPData $b, $op ) {
|
|
|
|
$a = $this->type === self::DUNDEFINED ? false : $this->toBool();
|
2019-08-12 09:18:15 +00:00
|
|
|
$b = $b->type === self::DUNDEFINED ? false : $b->toBool();
|
|
|
|
|
2018-08-26 08:34:42 +00:00
|
|
|
if ( $op === '|' ) {
|
2016-12-17 17:52:36 +00:00
|
|
|
return new AFPData( self::DBOOL, $a || $b );
|
2018-08-26 08:34:42 +00:00
|
|
|
} elseif ( $op === '&' ) {
|
2016-12-17 17:52:36 +00:00
|
|
|
return new AFPData( self::DBOOL, $a && $b );
|
2018-08-26 08:34:42 +00:00
|
|
|
} elseif ( $op === '^' ) {
|
2016-12-17 17:52:36 +00:00
|
|
|
return new AFPData( self::DBOOL, $a xor $b );
|
|
|
|
}
|
2018-04-04 21:14:25 +00:00
|
|
|
// Should never happen.
|
2019-04-13 16:27:20 +00:00
|
|
|
// @codeCoverageIgnoreStart
|
2021-08-29 22:58:17 +00:00
|
|
|
throw new InternalException( "Invalid boolean operation: {$op}" );
|
2019-04-13 16:27:20 +00:00
|
|
|
// @codeCoverageIgnoreEnd
|
2016-12-17 17:52:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-10-06 18:52:31 +00:00
|
|
|
* @param AFPData $b
|
|
|
|
* @param string $op
|
2016-12-17 17:52:36 +00:00
|
|
|
* @return AFPData
|
2021-08-29 22:58:17 +00:00
|
|
|
* @throws InternalException
|
2016-12-17 17:52:36 +00:00
|
|
|
*/
|
2019-08-12 12:40:51 +00:00
|
|
|
public function compareOp( AFPData $b, $op ) {
|
|
|
|
if ( $this->type === self::DUNDEFINED || $b->type === self::DUNDEFINED ) {
|
2019-08-03 15:52:14 +00:00
|
|
|
return new AFPData( self::DUNDEFINED );
|
2019-08-02 11:49:34 +00:00
|
|
|
}
|
2018-08-26 08:34:42 +00:00
|
|
|
if ( $op === '==' || $op === '=' ) {
|
2019-08-12 12:40:51 +00:00
|
|
|
return new AFPData( self::DBOOL, $this->equals( $b ) );
|
2018-08-26 08:34:42 +00:00
|
|
|
} elseif ( $op === '!=' ) {
|
2019-08-12 12:40:51 +00:00
|
|
|
return new AFPData( self::DBOOL, !$this->equals( $b ) );
|
2018-08-26 08:34:42 +00:00
|
|
|
} elseif ( $op === '===' ) {
|
2019-08-12 12:40:51 +00:00
|
|
|
return new AFPData( self::DBOOL, $this->equals( $b, true ) );
|
2018-08-26 08:34:42 +00:00
|
|
|
} elseif ( $op === '!==' ) {
|
2019-08-12 12:40:51 +00:00
|
|
|
return new AFPData( self::DBOOL, !$this->equals( $b, true ) );
|
2016-12-17 17:52:36 +00:00
|
|
|
}
|
2018-10-03 15:19:40 +00:00
|
|
|
|
2019-08-12 12:40:51 +00:00
|
|
|
$a = $this->toString();
|
2016-12-17 17:52:36 +00:00
|
|
|
$b = $b->toString();
|
2018-08-26 08:34:42 +00:00
|
|
|
if ( $op === '>' ) {
|
2016-12-17 17:52:36 +00:00
|
|
|
return new AFPData( self::DBOOL, $a > $b );
|
2018-08-26 08:34:42 +00:00
|
|
|
} elseif ( $op === '<' ) {
|
2016-12-17 17:52:36 +00:00
|
|
|
return new AFPData( self::DBOOL, $a < $b );
|
2018-08-26 08:34:42 +00:00
|
|
|
} elseif ( $op === '>=' ) {
|
2016-12-17 17:52:36 +00:00
|
|
|
return new AFPData( self::DBOOL, $a >= $b );
|
2018-08-26 08:34:42 +00:00
|
|
|
} elseif ( $op === '<=' ) {
|
2016-12-17 17:52:36 +00:00
|
|
|
return new AFPData( self::DBOOL, $a <= $b );
|
|
|
|
}
|
2018-04-04 21:14:25 +00:00
|
|
|
// Should never happen
|
2019-04-13 16:27:20 +00:00
|
|
|
// @codeCoverageIgnoreStart
|
2021-08-29 22:58:17 +00:00
|
|
|
throw new InternalException( "Invalid comparison operation: {$op}" );
|
2019-04-13 16:27:20 +00:00
|
|
|
// @codeCoverageIgnoreEnd
|
2016-12-17 17:52:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-10-06 18:52:31 +00:00
|
|
|
* @param AFPData $b
|
|
|
|
* @param string $op
|
|
|
|
* @param int $pos
|
2016-12-17 17:52:36 +00:00
|
|
|
* @return AFPData
|
2021-08-29 22:58:17 +00:00
|
|
|
* @throws UserVisibleException
|
|
|
|
* @throws InternalException
|
2016-12-17 17:52:36 +00:00
|
|
|
*/
|
2019-08-12 12:40:51 +00:00
|
|
|
public function mulRel( AFPData $b, $op, $pos ) {
|
2019-11-07 16:36:49 +00:00
|
|
|
if ( $b->type === self::DUNDEFINED ) {
|
|
|
|
// The LHS type is checked later, because we first need to ensure we're not
|
|
|
|
// dividing or taking modulo by 0 (and that should throw regardless of whether
|
|
|
|
// the LHS is undefined).
|
2019-08-03 15:52:14 +00:00
|
|
|
return new AFPData( self::DUNDEFINED );
|
2018-12-08 18:20:04 +00:00
|
|
|
}
|
2019-11-07 16:36:49 +00:00
|
|
|
|
2018-04-07 11:03:41 +00:00
|
|
|
$b = $b->toNumber();
|
2016-12-17 17:52:36 +00:00
|
|
|
|
2019-11-06 09:18:43 +00:00
|
|
|
if (
|
|
|
|
( $op === '/' && (float)$b === 0.0 ) ||
|
|
|
|
( $op === '%' && (int)$b === 0 )
|
|
|
|
) {
|
2019-11-07 16:36:49 +00:00
|
|
|
$lhs = $this->type === self::DUNDEFINED ? 0 : $this->toNumber();
|
2021-08-29 22:58:17 +00:00
|
|
|
throw new UserVisibleException( 'dividebyzero', $pos, [ $lhs ] );
|
2019-11-07 16:36:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( $this->type === self::DUNDEFINED ) {
|
|
|
|
return new AFPData( self::DUNDEFINED );
|
2016-12-17 17:52:36 +00:00
|
|
|
}
|
2019-11-07 16:36:49 +00:00
|
|
|
$a = $this->toNumber();
|
2016-12-17 17:52:36 +00:00
|
|
|
|
2018-08-26 08:34:42 +00:00
|
|
|
if ( $op === '*' ) {
|
2016-12-17 17:52:36 +00:00
|
|
|
$data = $a * $b;
|
2018-08-26 08:34:42 +00:00
|
|
|
} elseif ( $op === '/' ) {
|
2016-12-17 17:52:36 +00:00
|
|
|
$data = $a / $b;
|
2018-08-26 08:34:42 +00:00
|
|
|
} elseif ( $op === '%' ) {
|
2022-10-03 15:28:11 +00:00
|
|
|
$data = (int)$a % (int)$b;
|
2016-12-17 17:52:36 +00:00
|
|
|
} else {
|
|
|
|
// Should never happen
|
2019-04-13 16:27:20 +00:00
|
|
|
// @codeCoverageIgnoreStart
|
2021-08-29 22:58:17 +00:00
|
|
|
throw new InternalException( "Invalid multiplication-related operation: {$op}" );
|
2019-04-13 16:27:20 +00:00
|
|
|
// @codeCoverageIgnoreEnd
|
2016-12-17 17:52:36 +00:00
|
|
|
}
|
|
|
|
|
2019-04-14 08:49:09 +00:00
|
|
|
$type = is_int( $data ) ? self::DINT : self::DFLOAT;
|
2016-12-17 17:52:36 +00:00
|
|
|
|
|
|
|
return new AFPData( $type, $data );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-10-06 18:52:31 +00:00
|
|
|
* @param AFPData $b
|
2016-12-17 17:52:36 +00:00
|
|
|
* @return AFPData
|
|
|
|
*/
|
2019-08-12 11:53:38 +00:00
|
|
|
public function sum( AFPData $b ) {
|
|
|
|
if ( $this->type === self::DUNDEFINED || $b->type === self::DUNDEFINED ) {
|
2019-08-03 15:52:14 +00:00
|
|
|
return new AFPData( self::DUNDEFINED );
|
2019-08-12 11:53:38 +00:00
|
|
|
} elseif ( $this->type === self::DSTRING || $b->type === self::DSTRING ) {
|
|
|
|
return new AFPData( self::DSTRING, $this->toString() . $b->toString() );
|
|
|
|
} elseif ( $this->type === self::DARRAY && $b->type === self::DARRAY ) {
|
|
|
|
return new AFPData( self::DARRAY, array_merge( $this->toArray(), $b->toArray() ) );
|
2016-12-17 17:52:36 +00:00
|
|
|
} else {
|
2019-08-12 11:53:38 +00:00
|
|
|
$res = $this->toNumber() + $b->toNumber();
|
2018-10-03 15:19:40 +00:00
|
|
|
$type = is_int( $res ) ? self::DINT : self::DFLOAT;
|
|
|
|
|
|
|
|
return new AFPData( $type, $res );
|
2016-12-17 17:52:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-10-06 18:52:31 +00:00
|
|
|
* @param AFPData $b
|
2016-12-17 17:52:36 +00:00
|
|
|
* @return AFPData
|
|
|
|
*/
|
2019-08-12 11:53:38 +00:00
|
|
|
public function sub( AFPData $b ) {
|
|
|
|
if ( $this->type === self::DUNDEFINED || $b->type === self::DUNDEFINED ) {
|
2019-08-03 15:52:14 +00:00
|
|
|
return new AFPData( self::DUNDEFINED );
|
2018-12-08 18:20:04 +00:00
|
|
|
}
|
2019-08-12 11:53:38 +00:00
|
|
|
$res = $this->toNumber() - $b->toNumber();
|
2018-10-03 15:19:40 +00:00
|
|
|
$type = is_int( $res ) ? self::DINT : self::DFLOAT;
|
|
|
|
|
|
|
|
return new AFPData( $type, $res );
|
2016-12-17 17:52:36 +00:00
|
|
|
}
|
|
|
|
|
2020-04-19 11:56:09 +00:00
|
|
|
/**
|
|
|
|
* Check whether this instance contains the DUNDEFINED type, recursively
|
|
|
|
* @return bool
|
|
|
|
*/
|
2021-07-21 18:51:12 +00:00
|
|
|
public function hasUndefined(): bool {
|
2020-04-19 11:56:09 +00:00
|
|
|
if ( $this->type === self::DUNDEFINED ) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if ( $this->type === self::DARRAY ) {
|
|
|
|
foreach ( $this->data as $el ) {
|
|
|
|
if ( $el->hasUndefined() ) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a clone of this instance where DUNDEFINED is replaced with DNULL
|
|
|
|
* @return $this
|
|
|
|
*/
|
2021-07-21 18:51:12 +00:00
|
|
|
public function cloneAsUndefinedReplacedWithNull(): self {
|
2020-04-19 11:56:09 +00:00
|
|
|
if ( $this->type === self::DUNDEFINED ) {
|
|
|
|
return new self( self::DNULL );
|
|
|
|
}
|
|
|
|
if ( $this->type === self::DARRAY ) {
|
|
|
|
$data = [];
|
|
|
|
foreach ( $this->data as $el ) {
|
|
|
|
$data[] = $el->cloneAsUndefinedReplacedWithNull();
|
|
|
|
}
|
|
|
|
return new self( self::DARRAY, $data );
|
|
|
|
}
|
|
|
|
return clone $this;
|
|
|
|
}
|
|
|
|
|
2016-12-17 17:52:36 +00:00
|
|
|
/** Convert shorteners */
|
|
|
|
|
|
|
|
/**
|
2019-09-15 15:48:13 +00:00
|
|
|
* @throws RuntimeException
|
2016-12-17 17:52:36 +00:00
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function toNative() {
|
|
|
|
switch ( $this->type ) {
|
|
|
|
case self::DBOOL:
|
|
|
|
return $this->toBool();
|
|
|
|
case self::DSTRING:
|
|
|
|
return $this->toString();
|
|
|
|
case self::DFLOAT:
|
|
|
|
return $this->toFloat();
|
|
|
|
case self::DINT:
|
|
|
|
return $this->toInt();
|
2018-04-16 15:37:10 +00:00
|
|
|
case self::DARRAY:
|
|
|
|
$input = $this->toArray();
|
2017-06-15 14:23:34 +00:00
|
|
|
$output = [];
|
2016-12-17 17:52:36 +00:00
|
|
|
foreach ( $input as $item ) {
|
|
|
|
$output[] = $item->toNative();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $output;
|
|
|
|
case self::DNULL:
|
2019-08-03 15:52:14 +00:00
|
|
|
case self::DUNDEFINED:
|
2016-12-17 17:52:36 +00:00
|
|
|
return null;
|
|
|
|
default:
|
2019-04-13 17:15:35 +00:00
|
|
|
// @codeCoverageIgnoreStart
|
2019-09-15 15:48:13 +00:00
|
|
|
throw new RuntimeException( "Unknown type" );
|
2019-04-13 17:15:35 +00:00
|
|
|
// @codeCoverageIgnoreEnd
|
2016-12-17 17:52:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function toBool() {
|
|
|
|
return self::castTypes( $this, self::DBOOL )->data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function toString() {
|
|
|
|
return self::castTypes( $this, self::DSTRING )->data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return float
|
|
|
|
*/
|
|
|
|
public function toFloat() {
|
|
|
|
return self::castTypes( $this, self::DFLOAT )->data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function toInt() {
|
|
|
|
return self::castTypes( $this, self::DINT )->data;
|
|
|
|
}
|
|
|
|
|
2018-04-07 10:55:07 +00:00
|
|
|
/**
|
|
|
|
* @return int|float
|
|
|
|
*/
|
|
|
|
public function toNumber() {
|
2019-11-07 14:14:17 +00:00
|
|
|
// Types that can be cast to int
|
|
|
|
$intLikeTypes = [
|
|
|
|
self::DINT,
|
|
|
|
self::DBOOL,
|
|
|
|
self::DNULL
|
|
|
|
];
|
|
|
|
return in_array( $this->type, $intLikeTypes, true ) ? $this->toInt() : $this->toFloat();
|
2018-04-07 10:55:07 +00:00
|
|
|
}
|
|
|
|
|
2018-04-04 21:14:25 +00:00
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
2018-04-16 15:37:10 +00:00
|
|
|
public function toArray() {
|
|
|
|
return self::castTypes( $this, self::DARRAY )->data;
|
2016-12-17 17:52:36 +00:00
|
|
|
}
|
|
|
|
}
|