public function testSign() { $this->assertTrue(CMathi::sign(-1234) === -1); $this->assertTrue(CMathi::sign(5678) === 1); $this->assertTrue(CMathi::sign(0) === 0); $this->assertTrue(CMathi::sign(-1234.5) === -1); $this->assertTrue(CMathi::sign(5678.5) === 1); $this->assertTrue(CMathi::sign(0.0) === 0); }
/** * Determines the order in which two arrays should appear in a place where it matters. * * You can use your own comparator for the comparison of the elements in the arrays, but the default comparator has * got you covered when comparing scalar values, such as `string`, `int`, `float`, and `bool` in the ascending * order or in the descending order if you use `CComparator::ORDER_DESC`. And the default comparator is smart * enough to know how to compare objects of those classes that conform to the IEqualityAndOrder interface (static * or not), including CArray and CMap. See the [CComparator](CComparator.html) class for more on this. * * @param array $array The first array for comparison. * @param array $toArray The second array for comparison. * @param callable $comparator **OPTIONAL. Default is** `CComparator::ORDER_ASC`. The function or method to be * used for the comparison of any two elements. If this parameter is provided, the comparator should take two * parameters, with the first parameter being an element from the first array and the second parameter being an * element from the second array, and return `-1` if the element from the first array would need to go before the * element from the second array if the two were being ordered in separate, `1` if the other way around, and `0` if * the two elements are equal. * * @return int A negative value (typically `-1`) if the first array should go before the second array, a positive * value (typically `1`) if the other way around, and `0` if the two arrays are equal. * * @link CComparator.html CComparator */ public static function compare($array, $toArray, $comparator = CComparator::ORDER_ASC) { assert('is_carray($array) && is_carray($toArray) && is_callable($comparator)', vs(isset($this), get_defined_vars())); $array = splarray($array); $toArray = splarray($toArray); if ($array->getSize() != $toArray->getSize()) { return CMathi::sign($array->getSize() - $toArray->getSize()); } for ($i = 0; $i < $array->getSize(); $i++) { $compRes = call_user_func($comparator, $array[$i], $toArray[$i]); if ($compRes != 0) { return $compRes; } } return 0; }
protected static function shiftTimeInTimeZone(CTime $time, $timeUnit, $quantity, $timeZone) { $units; switch ($timeUnit) { case self::SECOND: $units = "seconds"; break; case self::MINUTE: $units = "minutes"; break; case self::HOUR: $units = "hours"; break; case self::DAY: $units = "days"; break; case self::WEEK: $units = "weeks"; break; case self::MONTH: $units = "months"; break; case self::YEAR: $units = "years"; break; default: assert('false', vs(isset($this), get_defined_vars())); break; } $dt = new DateTime(); $dt->setTimestamp($time->UTime()); $dt->setTimezone(is_cstring($timeZone) ? new DateTimeZone($timeZone) : $timeZone->DTimeZone()); $sign = $quantity < 0 ? "-" : "+"; $absQty = CString::fromInt(CMathi::abs($quantity)); $dt->modify("{$sign}{$absQty} {$units}"); $UTime = $dt->getTimestamp(); $MTime = $time->MTime(); if ($UTime != 0 && $MTime != 0 && CMathi::sign($UTime) != CMathi::sign($MTime)) { if ($UTime < 0) { // $MTime > 0 $UTime++; $MTime -= 1000; } else { // $MTime < 0 $UTime--; $MTime += 1000; } } return new self($UTime, $MTime); }
/** * Determines the order in which two strings should appear in a place where it matters, assuming the ascending * order, comparing the strings case-insensitively, and using natural order comparison. * * To illustrate natural order with an example, the strings "a100" and "a20" would get ordered as such with * `compareCi` method, but as "a20" and "a100" with this method, which is the order a human being would choose. * * @param string $string The first string for comparison. * @param string $toString The second string for comparison. * * @return int `-1` if the first string should go before the second string, `1` if the other way around, and `0` if * the two strings are equal, ignoring the letter case of the characters. */ public static function compareNatCi($string, $toString) { assert('is_cstring($string) && is_cstring($toString)', vs(isset($this), get_defined_vars())); return CMathi::sign(strnatcasecmp($string, $toString)); }
/** * Determines the order in which two maps should appear in a place where it matters. * * You can use your own comparator for the comparison of the values in the maps, but the default comparator has got * you covered when comparing scalar values, such as `string`, `int`, `float`, and `bool` in the ascending order or * in the descending order if you use `CComparator::ORDER_DESC`. And the default comparator is smart enough to know * how to compare objects of those classes that conform to the IEqualityAndOrder interface (static or not), * including CArray and CMap. See the [CComparator](CComparator.html) class for more on this. * * @param map $map The first map for comparison. * @param map $toMap The second map for comparison. * @param callable $comparator **OPTIONAL. Default is** `CComparator::ORDER_ASC`. The function or method to be * used for the comparison of any two values. If this parameter is provided, the comparator should take two * parameters, with the first parameter being a value from the first map and the second parameter being a value * from the second map, and return `-1` if the value from the first map would need to go before the value from the * second map if the two were being ordered in separate, `1` if the other way around, and `0` if the two values are * equal. * * @return int A negative value (typically `-1`) if the first map should go before the second map, a positive value * (typically `1`) if the other way around, and `0` if the two maps are equal. * * @link CComparator.html CComparator */ public static function compare($map, $toMap, $comparator = CComparator::ORDER_ASC) { assert('is_cmap($map) && is_cmap($toMap)', vs(isset($this), get_defined_vars())); $map = parray($map); $toMap = parray($toMap); // Compare the keys. $keys = array_keys($map); $toKeys = array_keys($toMap); if (count($keys) != count($toKeys)) { return CMathi::sign(count($keys) - count($toKeys)); } for ($i = 0; $i < count($keys); $i++) { if ($keys[$i] !== $toKeys[$i]) { return $keys[$i] < $toKeys[$i] ? -1 : 1; } } // Compare the values. The quantities should match at this point. $values = array_values($map); $toValues = array_values($toMap); for ($i = 0; $i < count($values); $i++) { $compRes = call_user_func($comparator, $values[$i], $toValues[$i]); if ($compRes != 0) { return $compRes; } } return 0; }