/** * Set the offset of this limit clause * @param int $offset * @return null * @throws zibo\ZiboException when $offset is not numeric * @throws zibo\library\database\exception\DatabaseException when $offset is negative */ private function setOffset($offset = null) { if ($offset !== null && !Number::isNumeric($offset, Number::NOT_NEGATIVE | Number::NOT_FLOAT)) { throw new DatabaseException('Provided offset is negative'); } $this->offset = $offset; }
/** * Sets how old a cache object may become before it's cleared. * @param int $age Number of seconds a cache object can exist before it's * cleared. Set to null or 0 to skip age checking * @return null * @throws zibo\ZiboException when the provided age is invalid */ public function setCleanUpAge($age) { if ($age === null || $age === 0) { $this->cleanUpAge = null; return; } if (!Number::isNumeric($age, Number::NOT_NEGATIVE | Number::NOT_FLOAT)) { throw new ZiboException('Provided clean up age is negative'); } $this->cleanUpAge = $age; }
/** * Format a size in bytes into a more human readable byte unit * @param int $size size in bytes * @return int a size in Kb, Mb, ... depending on the size * @throws zibo\library\filesystem\exception\FileSystemException when the size is not a zero or a positive number */ public static function formatSize($size) { if (!Number::isNumeric($size, Number::NOT_NEGATIVE | Number::NOT_FLOAT)) { throw new FileSystemException('Invalid is not a valid file size'); } if ($size == 0) { return '0 bytes'; } $fileSizeUnits = array(' bytes', ' Kb', ' Mb', ' Gb', ' Tb', ' Pb', ' Eb', ' Zb', ' Yb'); $i = floor(log($size, 1024)); return round($size / pow(1024, $i), 2) . $fileSizeUnits[$i]; }
/** * Generates a random string * @param integer $length Number of characters to generate * @param string $haystack String with the haystack to pick characters from * @return string A random string * @throws zibo\ZiboException when an invalid length is provided * @throws zibo\ZiboException when an empty haystack is provided * @throws zibo\ZiboException when the requested length is greater then * the length of the haystack */ public static function generate($length = 8, $haystack = null) { $string = ''; if ($haystack == null) { $haystack = self::GENERATE_HAYSTACK; } if (!Number::isNumeric($length, Number::NOT_NEGATIVE | Number::NOT_ZERO | Number::NOT_FLOAT)) { throw new ZiboException('Could not generate a random string: invalid length provided'); } if (!self::isString($haystack, self::NOT_EMPTY)) { throw new ZiboException('Could not generate a random string: empty or invalid haystack provided'); } $haystackLength = strlen($haystack); if ($length > $haystackLength) { throw new ZiboException('Length cannot be greater than the length of the haystack. Length is ' . $length . ' and the length of the haystack is ' . $haystackLength); } $i = 0; while ($i < $length) { $char = substr($haystack, mt_rand(0, $haystackLength - 1), 1); if (!strstr($string, $char)) { $string .= $char; $i++; } } return $string; }
/** * @dataProvider providerIsNumericOctal */ public function testIsNumericOctal($expected, $value) { $result = Number::isNumeric($value, Number::OCTAL); $this->assertEquals($expected, $result); }
/** * Registers a new event listener * @param string $event Name of the event * @param string|array|zibo\library\Callback $callback Callback for the * event listener * @param int $weight Weight for the new listener in the event listener * list. This will influence the order of the event listener calls. An * event with weight 1 will be called before an event with weight 10. * @return null * @throws zibo\ZiboException when the name of the event is empty or * invalid * @throws zibo\ZiboException when the weight of the event listener * is invalid or already set */ public function registerEventListener($event, $callback, $weight = null) { $this->checkEventName($event); // validate the weight value if ($weight === null) { $weight = $this->getNewWeight($event); } elseif (!Number::isNumeric($weight, Number::NOT_NEGATIVE) || $weight >= $this->maxEventListeners) { throw new ZiboException('Provided weight is invalid. Try a value between 0 and ' . $this->maxEventListeners); } // check the occupation if (isset($this->events[$event][$weight])) { throw new ZiboException('Weight ' . $weight . ' for event ' . $event . ' is already set with callback ' . $this->events[$event][$weight]); } // add it if (!isset($this->events[$event])) { $this->events[$event] = array(); } $this->events[$event][$weight] = new Callback($callback); // resort the event listeners by weight ksort($this->events[$event]); }
/** * Checks if the provided octal permissions is a valid value * @param int $octalPermissions Octal permissions * @return null * @throws zibo\ZiboException when the provided permissions is not a valid octal permissions value */ private function checkOctalPermissions($octalPermissions) { $lengthPermissions = strlen($octalPermissions); if ($lengthPermissions > 5) { throw new ZiboException('Provided permissions is not a valid octal permissions value: too much digits'); } if (!Number::isNumeric($octalPermissions, Number::OCTAL)) { throw new ZiboException('Provided permissions is not a valid octal permissions value: not an octal value'); } }
/** * Sets the date the content was last modified * @param integer $timestamp Timestamp of the date * @return null */ public function setLastModified($timestamp = null) { if ($timestamp === null) { $this->dateLastModified = null; $this->headers->removeHeader(Header::HEADER_LAST_MODIFIED); return; } if (!Number::isNumeric($timestamp, Number::NOT_NEGATIVE | Number::NOT_ZERO | Number::NOT_FLOAT)) { throw new ZiboException('Invalid date provided'); } $this->dateLastModified = $timestamp; $this->headers->setHeader(Header::HEADER_LAST_MODIFIED, Header::parseTime($timestamp)); }