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';
|
2016-12-17 17:52:36 +00:00
|
|
|
|
|
|
|
// Translation table mapping shell-style wildcards to PCRE equivalents.
|
|
|
|
// Derived from <http://www.php.net/manual/en/function.fnmatch.php#100207>
|
2017-06-15 14:23:34 +00:00
|
|
|
private 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
|
|
|
|
|
|
|
public $type;
|
|
|
|
public $data;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $type
|
2018-05-25 23:31:49 +00:00
|
|
|
* @param mixed|null $val
|
2016-12-17 17:52:36 +00:00
|
|
|
*/
|
|
|
|
public function __construct( $type = self::DNULL, $val = null ) {
|
|
|
|
$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 ) {
|
|
|
|
if ( is_string( $var ) ) {
|
|
|
|
return new AFPData( self::DSTRING, $var );
|
|
|
|
} elseif ( is_int( $var ) ) {
|
|
|
|
return new AFPData( self::DINT, $var );
|
|
|
|
} elseif ( is_float( $var ) ) {
|
|
|
|
return new AFPData( self::DFLOAT, $var );
|
|
|
|
} elseif ( is_bool( $var ) ) {
|
|
|
|
return new AFPData( self::DBOOL, $var );
|
|
|
|
} elseif ( is_array( $var ) ) {
|
2017-06-15 14:23:34 +00:00
|
|
|
$result = [];
|
2016-12-17 17:52:36 +00:00
|
|
|
foreach ( $var as $item ) {
|
|
|
|
$result[] = self::newFromPHPVar( $item );
|
|
|
|
}
|
|
|
|
|
2018-04-16 15:37:10 +00:00
|
|
|
return new AFPData( self::DARRAY, $result );
|
2016-12-17 17:52:36 +00:00
|
|
|
} elseif ( is_null( $var ) ) {
|
|
|
|
return new AFPData();
|
|
|
|
} else {
|
|
|
|
throw new AFPException(
|
|
|
|
'Data type ' . gettype( $var ) . ' is not supported by AbuseFilter'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return AFPData
|
|
|
|
*/
|
2018-10-03 15:19:40 +00:00
|
|
|
private function dup() {
|
2016-12-17 17:52:36 +00:00
|
|
|
return new AFPData( $this->type, $this->data );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
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 ) {
|
2016-12-17 17:52:36 +00:00
|
|
|
if ( $orig->type == $target ) {
|
|
|
|
return $orig->dup();
|
|
|
|
}
|
|
|
|
if ( $target == self::DNULL ) {
|
|
|
|
return new AFPData();
|
|
|
|
}
|
|
|
|
|
2018-04-16 15:37:10 +00:00
|
|
|
if ( $orig->type == self::DARRAY ) {
|
2016-12-17 17:52:36 +00:00
|
|
|
if ( $target == self::DBOOL ) {
|
|
|
|
return new AFPData( self::DBOOL, (bool)count( $orig->data ) );
|
2018-10-03 15:19:40 +00:00
|
|
|
} elseif ( $target == self::DFLOAT ) {
|
2016-12-17 17:52:36 +00:00
|
|
|
return new AFPData( self::DFLOAT, floatval( count( $orig->data ) ) );
|
2018-10-03 15:19:40 +00:00
|
|
|
} elseif ( $target == self::DINT ) {
|
2016-12-17 17:52:36 +00:00
|
|
|
return new AFPData( self::DINT, intval( count( $orig->data ) ) );
|
2018-10-03 15:19:40 +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 );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( $target == self::DBOOL ) {
|
|
|
|
return new AFPData( self::DBOOL, (bool)$orig->data );
|
2018-10-03 15:19:40 +00:00
|
|
|
} elseif ( $target == self::DFLOAT ) {
|
2016-12-17 17:52:36 +00:00
|
|
|
return new AFPData( self::DFLOAT, floatval( $orig->data ) );
|
2018-10-03 15:19:40 +00:00
|
|
|
} elseif ( $target == self::DINT ) {
|
2016-12-17 17:52:36 +00:00
|
|
|
return new AFPData( self::DINT, intval( $orig->data ) );
|
2018-10-03 15:19:40 +00:00
|
|
|
} elseif ( $target == self::DSTRING ) {
|
2016-12-17 17:52:36 +00:00
|
|
|
return new AFPData( self::DSTRING, strval( $orig->data ) );
|
2018-10-03 15:19:40 +00:00
|
|
|
} elseif ( $target == self::DARRAY ) {
|
2018-04-16 15:37:10 +00:00
|
|
|
return new AFPData( self::DARRAY, [ $orig ] );
|
2016-12-17 17:52:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-10-06 18:52:31 +00:00
|
|
|
* @param AFPData $value
|
2016-12-17 17:52:36 +00:00
|
|
|
* @return AFPData
|
|
|
|
*/
|
2018-10-03 15:19:40 +00:00
|
|
|
public static function boolInvert( AFPData $value ) {
|
2016-12-17 17:52:36 +00:00
|
|
|
return new AFPData( self::DBOOL, !$value->toBool() );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-10-06 18:52:31 +00:00
|
|
|
* @param AFPData $base
|
|
|
|
* @param AFPData $exponent
|
2016-12-17 17:52:36 +00:00
|
|
|
* @return AFPData
|
|
|
|
*/
|
2018-10-03 15:19:40 +00:00
|
|
|
public static function pow( AFPData $base, AFPData $exponent ) {
|
2018-04-07 12:01:17 +00:00
|
|
|
$res = pow( $base->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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-10-03 15:19:40 +00:00
|
|
|
* Checks if $a contains $b
|
|
|
|
*
|
2017-10-06 18:52:31 +00:00
|
|
|
* @param AFPData $a
|
|
|
|
* @param AFPData $b
|
2016-12-17 17:52:36 +00:00
|
|
|
* @return AFPData
|
|
|
|
*/
|
2018-10-03 15:19:40 +00:00
|
|
|
private static function containmentKeyword( AFPData $a, AFPData $b ) {
|
2016-12-17 17:52:36 +00:00
|
|
|
$a = $a->toString();
|
|
|
|
$b = $b->toString();
|
|
|
|
|
|
|
|
if ( $a == '' || $b == '' ) {
|
|
|
|
return new AFPData( self::DBOOL, false );
|
|
|
|
}
|
|
|
|
|
2018-10-03 15:19:40 +00:00
|
|
|
return new AFPData( self::DBOOL, strpos( $a, $b ) !== false );
|
2016-12-17 17:52:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-10-06 18:52:31 +00:00
|
|
|
* @param AFPData $a
|
|
|
|
* @param AFPData $b
|
2016-12-17 17:52:36 +00:00
|
|
|
* @return AFPData
|
|
|
|
*/
|
2018-10-03 15:19:40 +00:00
|
|
|
public static function keywordIn( AFPData $a, AFPData $b ) {
|
|
|
|
return self::containmentKeyword( $b, $a );
|
|
|
|
}
|
2016-12-17 17:52:36 +00:00
|
|
|
|
2018-10-03 15:19:40 +00:00
|
|
|
/**
|
|
|
|
* @param AFPData $a
|
|
|
|
* @param AFPData $b
|
|
|
|
* @return AFPData
|
|
|
|
*/
|
|
|
|
public static function keywordContains( AFPData $a, AFPData $b ) {
|
|
|
|
return self::containmentKeyword( $a, $b );
|
2016-12-17 17:52:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-10-06 18:52:31 +00:00
|
|
|
* @param AFPData $d1
|
|
|
|
* @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
|
|
|
|
*/
|
2018-10-03 15:19:40 +00:00
|
|
|
private static function equals( AFPData $d1, AFPData $d2, $strict = false ) {
|
2018-04-16 15:37:10 +00:00
|
|
|
if ( $d1->type != self::DARRAY && $d2->type != self::DARRAY ) {
|
2018-03-25 10:03:26 +00:00
|
|
|
$typecheck = $d1->type == $d2->type || !$strict;
|
|
|
|
return $typecheck && $d1->toString() === $d2->toString();
|
2018-04-16 15:37:10 +00:00
|
|
|
} elseif ( $d1->type == self::DARRAY && $d2->type == self::DARRAY ) {
|
2018-03-25 10:03:26 +00:00
|
|
|
$data1 = $d1->data;
|
|
|
|
$data2 = $d2->data;
|
|
|
|
if ( count( $data1 ) !== count( $data2 ) ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$length = count( $data1 );
|
|
|
|
for ( $i = 0; $i < $length; $i++ ) {
|
2018-10-03 15:19:40 +00:00
|
|
|
if ( self::equals( $data1[$i], $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;
|
|
|
|
}
|
2018-04-16 15:37:10 +00:00
|
|
|
if ( $d1->type == self::DARRAY && count( $d1->data ) === 0 ) {
|
2018-04-24 15:56:29 +00:00
|
|
|
return ( $d2->type == self::DBOOL && $d2->toBool() == false ) || $d2->type == self::DNULL;
|
2018-04-16 15:37:10 +00:00
|
|
|
} elseif ( $d2->type == self::DARRAY && count( $d2->data ) === 0 ) {
|
2018-04-24 15:56:29 +00:00
|
|
|
return ( $d1->type == self::DBOOL && $d1->toBool() == false ) || $d1->type == self::DNULL;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
2018-03-25 10:03:26 +00:00
|
|
|
}
|
2016-12-17 17:52:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-10-06 18:52:31 +00:00
|
|
|
* @param AFPData $str
|
|
|
|
* @param AFPData $pattern
|
2016-12-17 17:52:36 +00:00
|
|
|
* @return AFPData
|
|
|
|
*/
|
2018-10-03 15:19:40 +00:00
|
|
|
public static function keywordLike( AFPData $str, AFPData $pattern ) {
|
2016-12-17 17:52:36 +00:00
|
|
|
$str = $str->toString();
|
|
|
|
$pattern = '#^' . strtr( preg_quote( $pattern->toString(), '#' ), self::$wildcardMap ) . '$#u';
|
2018-02-12 10:29:11 +00:00
|
|
|
Wikimedia\suppressWarnings();
|
2016-12-17 17:52:36 +00:00
|
|
|
$result = preg_match( $pattern, $str );
|
2018-02-12 10:29:11 +00:00
|
|
|
Wikimedia\restoreWarnings();
|
2016-12-17 17:52:36 +00:00
|
|
|
|
|
|
|
return new AFPData( self::DBOOL, (bool)$result );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-10-06 18:52:31 +00:00
|
|
|
* @param AFPData $str
|
|
|
|
* @param AFPData $regex
|
|
|
|
* @param int $pos
|
|
|
|
* @param bool $insensitive
|
2016-12-17 17:52:36 +00:00
|
|
|
* @return AFPData
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
2018-10-03 15:19:40 +00:00
|
|
|
public static function keywordRegex( AFPData $str, AFPData $regex, $pos, $insensitive = false ) {
|
2016-12-17 17:52:36 +00:00
|
|
|
$str = $str->toString();
|
|
|
|
$pattern = $regex->toString();
|
|
|
|
|
|
|
|
$pattern = preg_replace( '!(\\\\\\\\)*(\\\\)?/!', '$1\/', $pattern );
|
|
|
|
$pattern = "/$pattern/u";
|
|
|
|
|
|
|
|
if ( $insensitive ) {
|
|
|
|
$pattern .= 'i';
|
|
|
|
}
|
|
|
|
|
2018-02-12 10:29:11 +00:00
|
|
|
Wikimedia\suppressWarnings();
|
2016-12-17 17:52:36 +00:00
|
|
|
$result = preg_match( $pattern, $str );
|
2018-02-12 10:29:11 +00:00
|
|
|
Wikimedia\restoreWarnings();
|
2016-12-17 17:52:36 +00:00
|
|
|
if ( $result === false ) {
|
|
|
|
throw new AFPUserVisibleException(
|
|
|
|
'regexfailure',
|
|
|
|
$pos,
|
2018-10-08 12:48:08 +00:00
|
|
|
[ $pattern ]
|
2016-12-17 17:52:36 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return new AFPData( self::DBOOL, (bool)$result );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-04-29 17:52:45 +00:00
|
|
|
* @param AFPData $str
|
|
|
|
* @param AFPData $regex
|
2017-10-06 18:52:31 +00:00
|
|
|
* @param int $pos
|
2016-12-17 17:52:36 +00:00
|
|
|
* @return AFPData
|
|
|
|
*/
|
2018-10-03 15:19:40 +00:00
|
|
|
public static function keywordRegexInsensitive( AFPData $str, AFPData $regex, $pos ) {
|
2016-12-17 17:52:36 +00:00
|
|
|
return self::keywordRegex( $str, $regex, $pos, true );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-10-06 18:52:31 +00:00
|
|
|
* @param AFPData $data
|
2016-12-17 17:52:36 +00:00
|
|
|
* @return AFPData
|
|
|
|
*/
|
2018-10-03 15:19:40 +00:00
|
|
|
public static function unaryMinus( AFPData $data ) {
|
2016-12-17 17:52:36 +00:00
|
|
|
if ( $data->type == self::DINT ) {
|
|
|
|
return new AFPData( $data->type, -$data->toInt() );
|
|
|
|
} else {
|
|
|
|
return new AFPData( $data->type, -$data->toFloat() );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-10-06 18:52:31 +00:00
|
|
|
* @param AFPData $a
|
|
|
|
* @param AFPData $b
|
|
|
|
* @param string $op
|
2016-12-17 17:52:36 +00:00
|
|
|
* @return AFPData
|
|
|
|
* @throws AFPException
|
|
|
|
*/
|
2018-10-03 15:19:40 +00:00
|
|
|
public static function boolOp( AFPData $a, AFPData $b, $op ) {
|
2016-12-17 17:52:36 +00:00
|
|
|
$a = $a->toBool();
|
|
|
|
$b = $b->toBool();
|
|
|
|
if ( $op == '|' ) {
|
|
|
|
return new AFPData( self::DBOOL, $a || $b );
|
2018-10-03 15:19:40 +00:00
|
|
|
} elseif ( $op == '&' ) {
|
2016-12-17 17:52:36 +00:00
|
|
|
return new AFPData( self::DBOOL, $a && $b );
|
2018-10-03 15:19:40 +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.
|
|
|
|
throw new AFPException( "Invalid boolean operation: {$op}" );
|
2016-12-17 17:52:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-10-06 18:52:31 +00:00
|
|
|
* @param AFPData $a
|
|
|
|
* @param AFPData $b
|
|
|
|
* @param string $op
|
2016-12-17 17:52:36 +00:00
|
|
|
* @return AFPData
|
|
|
|
* @throws AFPException
|
|
|
|
*/
|
2018-10-03 15:19:40 +00:00
|
|
|
public static function compareOp( AFPData $a, AFPData $b, $op ) {
|
2016-12-17 17:52:36 +00:00
|
|
|
if ( $op == '==' || $op == '=' ) {
|
|
|
|
return new AFPData( self::DBOOL, self::equals( $a, $b ) );
|
2018-10-03 15:19:40 +00:00
|
|
|
} elseif ( $op == '!=' ) {
|
2016-12-17 17:52:36 +00:00
|
|
|
return new AFPData( self::DBOOL, !self::equals( $a, $b ) );
|
2018-10-03 15:19:40 +00:00
|
|
|
} elseif ( $op == '===' ) {
|
2018-03-25 10:03:26 +00:00
|
|
|
return new AFPData( self::DBOOL, self::equals( $a, $b, true ) );
|
2018-10-03 15:19:40 +00:00
|
|
|
} elseif ( $op == '!==' ) {
|
2018-03-25 10:03:26 +00:00
|
|
|
return new AFPData( self::DBOOL, !self::equals( $a, $b, true ) );
|
2016-12-17 17:52:36 +00:00
|
|
|
}
|
2018-10-03 15:19:40 +00:00
|
|
|
|
2016-12-17 17:52:36 +00:00
|
|
|
$a = $a->toString();
|
|
|
|
$b = $b->toString();
|
|
|
|
if ( $op == '>' ) {
|
|
|
|
return new AFPData( self::DBOOL, $a > $b );
|
2018-10-03 15:19:40 +00:00
|
|
|
} elseif ( $op == '<' ) {
|
2016-12-17 17:52:36 +00:00
|
|
|
return new AFPData( self::DBOOL, $a < $b );
|
2018-10-03 15:19:40 +00:00
|
|
|
} elseif ( $op == '>=' ) {
|
2016-12-17 17:52:36 +00:00
|
|
|
return new AFPData( self::DBOOL, $a >= $b );
|
2018-10-03 15:19:40 +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
|
|
|
|
throw new AFPException( "Invalid comparison operation: {$op}" );
|
2016-12-17 17:52:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-10-06 18:52:31 +00:00
|
|
|
* @param AFPData $a
|
|
|
|
* @param AFPData $b
|
|
|
|
* @param string $op
|
|
|
|
* @param int $pos
|
2016-12-17 17:52:36 +00:00
|
|
|
* @return AFPData
|
|
|
|
* @throws AFPUserVisibleException
|
|
|
|
* @throws AFPException
|
|
|
|
*/
|
2018-10-03 15:19:40 +00:00
|
|
|
public static function mulRel( AFPData $a, AFPData $b, $op, $pos ) {
|
2018-04-07 11:03:41 +00:00
|
|
|
$a = $a->toNumber();
|
|
|
|
$b = $b->toNumber();
|
2016-12-17 17:52:36 +00:00
|
|
|
|
|
|
|
if ( $op != '*' && $b == 0 ) {
|
2017-06-15 14:23:34 +00:00
|
|
|
throw new AFPUserVisibleException( 'dividebyzero', $pos, [ $a ] );
|
2016-12-17 17:52:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( $op == '*' ) {
|
|
|
|
$data = $a * $b;
|
|
|
|
} elseif ( $op == '/' ) {
|
|
|
|
$data = $a / $b;
|
|
|
|
} elseif ( $op == '%' ) {
|
|
|
|
$data = $a % $b;
|
|
|
|
} else {
|
|
|
|
// Should never happen
|
|
|
|
throw new AFPException( "Invalid multiplication-related operation: {$op}" );
|
|
|
|
}
|
|
|
|
|
2018-10-03 15:19:40 +00:00
|
|
|
if ( is_int( $data ) ) {
|
2016-12-17 17:52:36 +00:00
|
|
|
$data = intval( $data );
|
2018-04-07 11:03:41 +00:00
|
|
|
$type = self::DINT;
|
2016-12-17 17:52:36 +00:00
|
|
|
} else {
|
|
|
|
$data = floatval( $data );
|
2018-04-07 11:03:41 +00:00
|
|
|
$type = 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 $a
|
|
|
|
* @param AFPData $b
|
2016-12-17 17:52:36 +00:00
|
|
|
* @return AFPData
|
|
|
|
*/
|
2018-10-03 15:19:40 +00:00
|
|
|
public static function sum( AFPData $a, AFPData $b ) {
|
2016-12-17 17:52:36 +00:00
|
|
|
if ( $a->type == self::DSTRING || $b->type == self::DSTRING ) {
|
|
|
|
return new AFPData( self::DSTRING, $a->toString() . $b->toString() );
|
2018-04-16 15:37:10 +00:00
|
|
|
} elseif ( $a->type == self::DARRAY && $b->type == self::DARRAY ) {
|
|
|
|
return new AFPData( self::DARRAY, array_merge( $a->toArray(), $b->toArray() ) );
|
2016-12-17 17:52:36 +00:00
|
|
|
} else {
|
2018-04-07 10:55:07 +00:00
|
|
|
$res = $a->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 $a
|
|
|
|
* @param AFPData $b
|
2016-12-17 17:52:36 +00:00
|
|
|
* @return AFPData
|
|
|
|
*/
|
2018-10-03 15:19:40 +00:00
|
|
|
public static function sub( AFPData $a, AFPData $b ) {
|
2018-04-07 10:55:07 +00:00
|
|
|
$res = $a->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:
|
|
|
|
return null;
|
|
|
|
default:
|
|
|
|
throw new MWException( "Unknown type" );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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() {
|
|
|
|
return $this->type == self::DINT ? $this->toInt() : $this->toFloat();
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|