mirror of
https://github.com/alchemy-fr/Phraseanet.git
synced 2025-10-13 04:53:26 +00:00
Status operation returns data with same length as input
This commit is contained in:
@@ -10,18 +10,17 @@
|
||||
*/
|
||||
|
||||
use Alchemy\Phrasea\Application;
|
||||
use Alchemy\Phrasea\SearchEngine\Elastic\RecordHelper;
|
||||
use MediaAlchemyst\Exception\ExceptionInterface;
|
||||
use Symfony\Component\HttpFoundation\File\UploadedFile;
|
||||
use Alchemy\Phrasea\Exception\InvalidArgumentException;
|
||||
use MediaAlchemyst\Specification\Image as ImageSpecification;
|
||||
use Symfony\Component\HttpFoundation\File\Exception\FileException;
|
||||
use Alchemy\Phrasea\Model\RecordInterface;
|
||||
|
||||
class databox_status
|
||||
{
|
||||
public static function getSearchStatus(Application $app)
|
||||
{
|
||||
$structures = $stats = [];
|
||||
$structures = [];
|
||||
foreach ($app->getAclForUser($app->getAuthenticatedUser())->get_granted_sbas() as $databox) {
|
||||
$see_all = false;
|
||||
foreach ($databox->get_collections() as $collection) {
|
||||
@@ -131,7 +130,7 @@ class databox_status
|
||||
|
||||
try {
|
||||
$app['media-alchemyst']->turninto($filePath, $destPath, $imageSpec);
|
||||
} catch (\MediaAlchemyst\Exception $e) {
|
||||
} catch (ExceptionInterface $e) {
|
||||
|
||||
}
|
||||
|
||||
@@ -149,8 +148,9 @@ class databox_status
|
||||
if (substr($stat2, 0, 2) === '0x') {
|
||||
$stat2 = self::hex2bin(substr($stat2, 2));
|
||||
}
|
||||
$length = max(strlen($stat1), strlen($stat2));
|
||||
|
||||
return dechex(hexdec($stat1) & hexdec($stat2));
|
||||
return str_pad(decbin(bindec($stat1) & bindec($stat2)), $length, '0', STR_PAD_LEFT);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -164,6 +164,8 @@ class databox_status
|
||||
*/
|
||||
public static function operation_mask($stat1, $stat2)
|
||||
{
|
||||
$length = max(strlen($stat1), strlen($stat2));
|
||||
|
||||
$stat1 = str_pad($stat1, 32, '0', STR_PAD_LEFT);
|
||||
$stat2 = str_pad($stat2, 32, '0', STR_PAD_LEFT);
|
||||
$stat1_or = bindec(trim(str_replace("x", "0", $stat1)));
|
||||
@@ -171,7 +173,9 @@ class databox_status
|
||||
$stat2_or = bindec(trim(str_replace("x", "0", $stat2)));
|
||||
$stat2_and = bindec(trim(str_replace("x", "1", $stat2)));
|
||||
|
||||
return decbin((((0 | $stat1_or) & $stat1_and) | $stat2_or) & $stat2_and);
|
||||
$decbin = decbin((((0 | $stat1_or) & $stat1_and) | $stat2_or) & $stat2_and);
|
||||
|
||||
return str_pad($decbin, $length, '0', STR_PAD_LEFT);
|
||||
}
|
||||
|
||||
public static function operation_and_not($stat1, $stat2)
|
||||
@@ -182,10 +186,12 @@ class databox_status
|
||||
if (substr($stat2, 0, 2) === '0x') {
|
||||
$stat2 = self::hex2bin(substr($stat2, 2));
|
||||
}
|
||||
$length = max(strlen($stat1), strlen($stat2));
|
||||
|
||||
$stat1 = str_pad($stat1, 32, '0', STR_PAD_LEFT);
|
||||
$stat2 = str_pad($stat2, 32, '0', STR_PAD_LEFT);
|
||||
|
||||
return decbin(bindec($stat1) & ~bindec($stat2));
|
||||
return str_pad(decbin(bindec($stat1) & ~bindec($stat2)), $length, '0', STR_PAD_LEFT);
|
||||
}
|
||||
|
||||
public static function operation_or($stat1, $stat2)
|
||||
@@ -196,10 +202,11 @@ class databox_status
|
||||
if (substr($stat2, 0, 2) === '0x') {
|
||||
$stat2 = self::hex2bin(substr($stat2, 2));
|
||||
}
|
||||
$length = max(strlen($stat1), strlen($stat2));
|
||||
$stat1 = str_pad($stat1, 32, '0', STR_PAD_LEFT);
|
||||
$stat2 = str_pad($stat2, 32, '0', STR_PAD_LEFT);
|
||||
|
||||
return decbin(bindec($stat1) | bindec($stat2));
|
||||
return str_pad(decbin(bindec($stat1) | bindec($stat2)), $length, '0', STR_PAD_LEFT);
|
||||
}
|
||||
|
||||
public static function dec2bin($status)
|
||||
@@ -224,7 +231,7 @@ class databox_status
|
||||
throw new \Exception('Non-hexadecimal value');
|
||||
}
|
||||
|
||||
return base_convert($status, 16, 2);
|
||||
return str_pad(base_convert($status, 16, 2), 4*strlen($status), '0', STR_PAD_LEFT);
|
||||
}
|
||||
|
||||
public static function bitIsSet($bitValue, $nthBit)
|
||||
|
@@ -4,49 +4,49 @@ class databox_statusTest extends PHPUnit_Framework_TestCase
|
||||
{
|
||||
public function testOperation_and()
|
||||
{
|
||||
$this->assertEquals('0', databox_status::operation_and('0x001', '0x010'));
|
||||
$this->assertEquals('1', databox_status::operation_and('01', '11'));
|
||||
$this->assertEquals('0', databox_status::operation_and('01', '10'));
|
||||
$this->assertEquals('10', databox_status::operation_and('11', '10'));
|
||||
$this->assertSame('000000000000', databox_status::operation_and('0x001', '0x010'));
|
||||
$this->assertSame('01', databox_status::operation_and('01', '11'));
|
||||
$this->assertSame('00', databox_status::operation_and('01', '10'));
|
||||
$this->assertSame('10', databox_status::operation_and('11', '10'));
|
||||
}
|
||||
|
||||
public function testOperation_and_not()
|
||||
{
|
||||
$this->assertEquals('0', databox_status::operation_and_not('0x001', '0x011'));
|
||||
$this->assertEquals('0', databox_status::operation_and_not('01', '11'));
|
||||
$this->assertEquals('1', databox_status::operation_and_not('01', '10'));
|
||||
$this->assertEquals('1', databox_status::operation_and_not('11', '10'));
|
||||
$this->assertEquals('10', databox_status::operation_and_not('10', '01'));
|
||||
$this->assertSame('000000000000', databox_status::operation_and_not('0x001', '0x011'));
|
||||
$this->assertSame('00', databox_status::operation_and_not('01', '11'));
|
||||
$this->assertSame('01', databox_status::operation_and_not('01', '10'));
|
||||
$this->assertSame('01', databox_status::operation_and_not('11', '10'));
|
||||
$this->assertSame('10', databox_status::operation_and_not('10', '01'));
|
||||
}
|
||||
|
||||
public function testOperation_mask()
|
||||
{
|
||||
$this->assertEquals('001101', databox_status::operation_mask('010101', '0011xx'));
|
||||
$this->assertEquals('001100', databox_status::operation_mask('0', '0011xx'));
|
||||
$this->assertEquals('001101', databox_status::operation_mask('1', '0011xx'));
|
||||
$this->assertSame('001101', databox_status::operation_mask('010101', '0011xx'));
|
||||
$this->assertSame('001100', databox_status::operation_mask('0', '0011xx'));
|
||||
$this->assertSame('001101', databox_status::operation_mask('1', '0011xx'));
|
||||
}
|
||||
|
||||
public function testOperation_or()
|
||||
{
|
||||
$this->assertEquals('10001', databox_status::operation_or('0x001', '0x011'));
|
||||
$this->assertEquals('11', databox_status::operation_or('01', '11'));
|
||||
$this->assertSame('000000010001', databox_status::operation_or('0x001', '0x011'));
|
||||
$this->assertSame('11', databox_status::operation_or('01', '11'));
|
||||
}
|
||||
|
||||
public function testDec2bin()
|
||||
{
|
||||
$this->assertEquals('1010', databox_status::dec2bin('10'));
|
||||
$this->assertSame('1010', databox_status::dec2bin('10'));
|
||||
}
|
||||
|
||||
public function testHex2bin()
|
||||
{
|
||||
$this->assertEquals('10100001', databox_status::hex2bin('0x0A1'));
|
||||
$this->assertEquals('10100001', databox_status::hex2bin('0A1'));
|
||||
$this->assertSame('000010100001', databox_status::hex2bin('0x0A1'));
|
||||
$this->assertSame('000010100001', databox_status::hex2bin('0A1'));
|
||||
|
||||
try {
|
||||
databox_status::hex2bin('G1');
|
||||
$this->fail('Should raise an exception');
|
||||
} catch (Exception $e) {
|
||||
$this->assertEquals('Non-hexadecimal value', $e->getMessage());
|
||||
$this->assertSame('Non-hexadecimal value', $e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user