2016-12-17 17:52:36 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class AFPData {
|
|
|
|
// Datatypes
|
|
|
|
const DINT = 'int';
|
|
|
|
const DSTRING = 'string';
|
|
|
|
const DNULL = 'null';
|
|
|
|
const DBOOL = 'bool';
|
|
|
|
const DFLOAT = 'float';
|
2018-04-16 15:37:10 +00:00
|
|
|
const DARRAY = 'array';
|
2019-05-23 10:55:20 +00:00
|
|
|
// Special purpose type for non-initialized stuff
|
2019-08-03 15:52:14 +00:00
|
|
|
const DUNDEFINED = 'undefined';
|
|
|
|
// Special purpose for creating instances that will be populated later
|
|
|
|
const DEMPTY = 'empty';
|
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
|
|
|
|
*/
|
|
|
|
public static $wildcardMap = [
|
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
|
2019-01-24 10:10:22 +00:00
|
|
|
* @private Use $this->getType()
|
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
|
2019-01-24 10:10:22 +00:00
|
|
|
* @private Use $this->getData()
|
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 ) {
|
2019-08-03 15:52:14 +00:00
|
|
|
if ( ( $type === self::DUNDEFINED || $type === self::DEMPTY ) && $val !== null ) {
|
2019-08-12 09:18:15 +00:00
|
|
|
// Sanity
|
2019-08-20 17:36:59 +00:00
|
|
|
throw new InvalidArgumentException( 'DUNDEFINED and DEMPTY 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
|
|
|
|
* @throws AFPException
|
|
|
|
*/
|
|
|
|
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:
|
|
|
|
throw new AFPException(
|
|
|
|
'Data type ' . gettype( $var ) . ' is not supported by AbuseFilter'
|
|
|
|
);
|
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.
|
|
|
|
throw new AFPException( 'Refusing to cast DUNDEFINED to something else' );
|
|
|
|
}
|
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
|
|
|
}
|
2019-04-14 12:08:57 +00:00
|
|
|
throw new AFPException( '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
|
2019-08-12 11:53:38 +00:00
|
|
|
* @throws AFPException 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 ) {
|
2019-08-02 11:49:34 +00:00
|
|
|
throw new AFPException(
|
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 {
|
2019-08-20 17:36:59 +00:00
|
|
|
$type = $this->type === self::DEMPTY ? self::DNULL : $this->type;
|
|
|
|
return new AFPData( $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
|
|
|
|
* @throws AFPException
|
|
|
|
*/
|
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
|
2018-04-04 21:14:25 +00:00
|
|
|
throw new AFPException( "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
|
|
|
|
* @throws AFPException
|
|
|
|
*/
|
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
|
2018-04-04 21:14:25 +00:00
|
|
|
throw new AFPException( "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
|
|
|
|
* @throws AFPUserVisibleException
|
|
|
|
* @throws AFPException
|
|
|
|
*/
|
2019-08-12 12:40:51 +00:00
|
|
|
public function mulRel( AFPData $b, $op, $pos ) {
|
|
|
|
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 12:40:51 +00:00
|
|
|
$a = $this->toNumber();
|
2018-04-07 11:03:41 +00:00
|
|
|
$b = $b->toNumber();
|
2016-12-17 17:52:36 +00:00
|
|
|
|
2019-03-23 10:36:34 +00:00
|
|
|
if ( $op !== '*' && (float)$b === 0.0 ) {
|
2017-06-15 14:23:34 +00:00
|
|
|
throw new AFPUserVisibleException( 'dividebyzero', $pos, [ $a ] );
|
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 === '%' ) {
|
2016-12-17 17:52:36 +00:00
|
|
|
$data = $a % $b;
|
|
|
|
} else {
|
|
|
|
// Should never happen
|
2019-04-13 16:27:20 +00:00
|
|
|
// @codeCoverageIgnoreStart
|
2016-12-17 17:52:36 +00:00
|
|
|
throw new AFPException( "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
|
|
|
}
|
|
|
|
|
|
|
|
/** Convert shorteners */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @throws MWException
|
|
|
|
* @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:
|
|
|
|
case self::DEMPTY:
|
2016-12-17 17:52:36 +00:00
|
|
|
return null;
|
|
|
|
default:
|
2019-04-13 17:15:35 +00:00
|
|
|
// @codeCoverageIgnoreStart
|
2016-12-17 17:52:36 +00:00
|
|
|
throw new MWException( "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
|
|
|
}
|
|
|
|
}
|