/** * calculates the offset based on the wrap flag * * This is generally used by array functions to wrap offsets * * @param Integer $length Starting from 1 (not 0), the length of the list being wrapped around * @param Integer $offset The offset being wrapped * @param Integer $wrapFlag How to handle offsets that fall outside of the length of the list. * @return Integer|Boolean Returns the wrapped offset. Returns FALSE on failure */ function offsetWrap($length, $offset, $wrapFlag) { $length = (int) $length; if ($length <= 0) { throw new \r8\Exception\Index($offset, "Offset", "List is empty"); } $offset = (int) \r8\reduce($offset); switch ($wrapFlag) { default: throw new \r8\Exception\Argument(2, "wrapFlag", "Invalid offset wrap flag"); case \r8\num\OFFSET_NONE: if (!\r8\num\between($offset, 0 - $length, $length - 1)) { throw new \r8\Exception\Index($offset, "Offset", "Offset is out of bounds"); } else { if ($offset >= 0) { return $offset; } else { return $length + $offset; } } case \r8\num\OFFSET_WRAP: return \r8\num\intWrap($offset, 0, $length - 1); case FALSE: case \r8\num\OFFSET_RESTRICT: $offset = \r8\num\limit($offset, 0 - $length, $length - 1); if ($offset < 0) { $offset = $length + $offset; } return $offset; case \r8\num\OFFSET_LIMIT: return \r8\num\limit($offset, 0, $length - 1); } }
function testLimit() { $this->assertEquals(8, \r8\num\limit(8, 4, 10)); $this->assertEquals(4, \r8\num\limit(2, 4, 10)); $this->assertEquals(10, \r8\num\limit(12, 4, 10)); $this->assertEquals(8.5, \r8\num\limit(8.5, 4.5, 10.5)); $this->assertEquals(4.5, \r8\num\limit(2, 4.5, 10.5)); $this->assertEquals(10.5, \r8\num\limit(12, 4.5, 10.5)); }