HTMLPurifier_UnitConverter::convert PHP 메소드

convert() 공개 메소드

Converts a length object of one unit into another unit.
public convert ( HTMLPurifier_Length $length, string $to_unit ) : HTMLPurifier_Length | boolean
$length HTMLPurifier_Length Instance of HTMLPurifier_Length to convert. You must validate() it before passing it here!
$to_unit string Unit to convert to.
리턴 HTMLPurifier_Length | boolean
    public function convert($length, $to_unit)
    {
        if (!$length->isValid()) {
            return false;
        }
        $n = $length->getN();
        $unit = $length->getUnit();
        if ($n === '0' || $unit === false) {
            return new HTMLPurifier_Length('0', false);
        }
        $state = $dest_state = false;
        foreach (self::$units as $k => $x) {
            if (isset($x[$unit])) {
                $state = $k;
            }
            if (isset($x[$to_unit])) {
                $dest_state = $k;
            }
        }
        if (!$state || !$dest_state) {
            return false;
        }
        // Some calculations about the initial precision of the number;
        // this will be useful when we need to do final rounding.
        $sigfigs = $this->getSigFigs($n);
        if ($sigfigs < $this->outputPrecision) {
            $sigfigs = $this->outputPrecision;
        }
        // BCMath's internal precision deals only with decimals. Use
        // our default if the initial number has no decimals, or increase
        // it by how ever many decimals, thus, the number of guard digits
        // will always be greater than or equal to internalPrecision.
        $log = (int) floor(log(abs($n), 10));
        $cp = $log < 0 ? $this->internalPrecision - $log : $this->internalPrecision;
        // internal precision
        for ($i = 0; $i < 2; $i++) {
            // Determine what unit IN THIS SYSTEM we need to convert to
            if ($dest_state === $state) {
                // Simple conversion
                $dest_unit = $to_unit;
            } else {
                // Convert to the smallest unit, pending a system shift
                $dest_unit = self::$units[$state][$dest_state][0];
            }
            // Do the conversion if necessary
            if ($dest_unit !== $unit) {
                $factor = $this->div(self::$units[$state][$unit], self::$units[$state][$dest_unit], $cp);
                $n = $this->mul($n, $factor, $cp);
                $unit = $dest_unit;
            }
            // Output was zero, so bail out early. Shouldn't ever happen.
            if ($n === '') {
                $n = '0';
                $unit = $to_unit;
                break;
            }
            // It was a simple conversion, so bail out
            if ($dest_state === $state) {
                break;
            }
            if ($i !== 0) {
                // Conversion failed! Apparently, the system we forwarded
                // to didn't have this unit. This should never happen!
                return false;
            }
            // Pre-condition: $i == 0
            // Perform conversion to next system of units
            $n = $this->mul($n, self::$units[$state][$dest_state][1], $cp);
            $unit = self::$units[$state][$dest_state][2];
            $state = $dest_state;
            // One more loop around to convert the unit in the new system.
        }
        // Post-condition: $unit == $to_unit
        if ($unit !== $to_unit) {
            return false;
        }
        // Useful for debugging:
        //echo "<pre>n";
        //echo "$n\nsigfigs = $sigfigs\nnew_log = $new_log\nlog = $log\nrp = $rp\n</pre>\n";
        $n = $this->round($n, $sigfigs);
        if (strpos($n, '.') !== false) {
            $n = rtrim($n, '0');
        }
        $n = rtrim($n, '.');
        return new HTMLPurifier_Length($n, $unit);
    }

Usage Example

예제 #1
0
 protected function assertConversion($input, $expect, $unit = null, $test_negative = true)
 {
     $length = HTMLPurifier_Length::make($input);
     if ($expect !== false) {
         $expectl = HTMLPurifier_Length::make($expect);
     } else {
         $expectl = false;
     }
     $to_unit = $unit !== null ? $unit : $expectl->getUnit();
     $converter = new HTMLPurifier_UnitConverter(4, 10);
     $result = $converter->convert($length, $to_unit);
     if (!$result || !$expectl) {
         $this->assertIdentical($result, $expectl);
     } else {
         $this->assertIdentical($result->toString(), $expectl->toString());
     }
     $converter = new HTMLPurifier_UnitConverter(4, 10, true);
     $result = $converter->convert($length, $to_unit);
     if (!$result || !$expectl) {
         $this->assertIdentical($result, $expectl);
     } else {
         $this->assertIdentical($result->toString(), $expectl->toString(), 'BCMath substitute: %s');
     }
     if ($test_negative) {
         $this->assertConversion("-{$input}", $expect === false ? false : "-{$expect}", $unit, false);
     }
 }
All Usage Examples Of HTMLPurifier_UnitConverter::convert