Skip to content

Commit

Permalink
Missing constant INF
Browse files Browse the repository at this point in the history
  • Loading branch information
fisharebest committed Jan 23, 2016
1 parent 4d28746 commit 882c01c
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.md
@@ -1,6 +1,9 @@
CHANGE LOG
==========

## 1.3.0 (2016-01-23)
- INF

## 1.2.0 (2016-01-03)
- gzopen(), etc. are now part of symfony/polyfill

Expand Down
7 changes: 6 additions & 1 deletion README.md
Expand Up @@ -27,7 +27,7 @@ Add the dependency to your `composer.json` and allow autoloading magic to do the
```json
{
"require": {
"fisharebest/php-polyfill": "~1.2",
"fisharebest/php-polyfill": "~1.3",
},
}
```
Expand Down Expand Up @@ -58,6 +58,11 @@ status code, even if it was set using another function, such as `header()`.
This implementation can only get the current status code if it was also set by
`http_response_code()`.

PHP (general)
=============

- Some builds of PHP (such as the one used by strato.de) do not define INF.

Contributions
=============

Expand Down
50 changes: 50 additions & 0 deletions src/Php.php
@@ -0,0 +1,50 @@
<?php

/**
* PHP Polyfill
*
* @author Greg Roach <fisharebest@gmail.com>
* @copyright (c) 2016 Greg Roach
* @license MIT or GPLv3+
*/

namespace Fisharebest\PhpPolyfill;

use Symfony\Polyfill\Php54\Php54 as SymfonyPhp54;

/**
* Class Php - polyfills for poor implementations of PHP
*/
class Php {
const BIG_ENDIAN_INF_BYTES = '7ff0000000000000';
const LITTLE_ENDIAN_INF_BYTES = '000000000000f07f';

/**
* Some builds of PHP (e.g. strato.de on SunOS) omit the
* definition of INF.
*
* Note: we can't use hex2bin() on PHP 5.3. Use pack('H*') instead.
*
* @link http://php.net/manual/en/math.constants.php
*
* @return double
*/
public static function inf() {
$inf_bytes = self::isLittleEndian() ? self::LITTLE_ENDIAN_INF_BYTES : self::BIG_ENDIAN_INF_BYTES;
$inf = unpack('d', pack('H*', $inf_bytes));

return $inf[1];
}


/**
* Is this CPU big-endian or little-endian?
*
* @return bool
*/
private static function isLittleEndian() {
$tmp = unpack('S',"\x01\x00");

return $tmp[1] === 1;
}
}
6 changes: 6 additions & 0 deletions src/bootstrap.php
Expand Up @@ -8,13 +8,15 @@
* @license MIT or GPLv3+
*/

use Fisharebest\PhpPolyfill\Php;
use Fisharebest\PhpPolyfill\Php54;

if (PHP_VERSION_ID < 50400) {
// Magic quotes were removed in PHP 5.4
if (get_magic_quotes_gpc()) {
Php54::removeMagicQuotes();
}

// The global session variable bug/feature was removed in PHP 5.4
if (ini_get('session.bug_compat_42')) {
ini_set('session.bug_compat_42', '0');
Expand All @@ -25,3 +27,7 @@
function http_response_code($reponse_code = null) { return Php54::httpResponseCode($reponse_code); }
}
}

if (!defined('INF')) {
define('INF', PHP::inf());
}
File renamed without changes.
32 changes: 32 additions & 0 deletions tests/PhpTest.php
@@ -0,0 +1,32 @@
<?php

/**
* PHP Polyfill
*
* @author Greg Roach <fisharebest@gmail.com>
* @copyright (c) 2016 Greg Roach
* @license MIT or GPLv3+
*/

namespace Fisharebest\PhpPolyfill\Test;

use Fisharebest\PhpPolyfill\Php;
use PHPUnit_Framework_Error_Warning;
use PHPUnit_Framework_TestCase;

/**
* Class PhpTest - tests for class Php
*/
class PhpTest extends PHPUnit_Framework_TestCase {
/**
* @covers Fisharebest\PhpPolyfill\Php::inf
* @covers Fisharebest\PhpPolyfill\Php::isLittleEndian
* @runInSeparateProcess
*/
public function testInf() {
$this->assertTrue(is_float(Php::inf()));
$this->assertTrue(is_double(Php::inf()));
$this->assertTrue(is_infinite(Php::inf()));
$this->assertFalse(is_finite(Php::inf()));
}
}

0 comments on commit 882c01c

Please sign in to comment.