/** * Cache a certain block. * * @param string $name The name of the block that you want to cache. * @param int[optional] $lifetime The lifetime in seconds. */ public function cache($name, $lifetime = 60) { // redefine lifetime $lifetime = SpoonFilter::isBetween(10, 30758400, $lifetime) ? (int) $lifetime : 60; // set lifetime $this->cache[(string) $name] = $lifetime; }
/** * Checks if the field is between a given minimum and maximum (includes min & max). * * @return bool * @param int $minimum The minimum. * @param int $maximum The maximum. * @param string[optional] $error The error message to set. */ public function isBetween($minimum, $maximum, $error = null) { // filled if ($this->isFilled()) { // post/get data $data = $this->getMethod(true); // validate if (!isset($data[$this->attributes['name']]) || !SpoonFilter::isBetween($minimum, $maximum, $data[$this->attributes['name']])) { if ($error !== null) { $this->setError($error); } return false; } return true; } // not submitted if ($error !== null) { $this->setError($error); } return false; }
public function testIsBetween() { $this->assertTrue(SpoonFilter::isBetween(1, 10, 5)); $this->assertTrue(SpoonFilter::isBetween(1, 10, 1)); $this->assertTrue(SpoonFilter::isBetween(1, 10, 10)); $this->assertFalse(SpoonFilter::isBetween(1, 10, -1)); $this->assertFalse(SpoonFilter::isBetween(1, 10, 0)); $this->assertFalse(SpoonFilter::isBetween(1, 10, 12)); }
/** * Saves the image to a file (quality is only used for jpg images). * * @return bool True if the image was saved, false if not. * @param string $filename The path where the image should be saved. * @param int[optional] $quality The quality to use (only applies on jpg-images). * @param int[optional] $chmod Mode that should be applied on the file. */ public function parseToFile($filename, $quality = 100, $chmod = 0666) { // redefine vars $filename = (string) $filename; $quality = (int) $quality; // if (@is_writable(dirname($filename)) !== true) { // does the folder exist? if not, try to create if (!SpoonDirectory::create(dirname($filename))) { if ($this->strict) { throw new SpoonThumbnailException('The destination-path should be writable.'); } return false; } } // get extension $extension = SpoonFile::getExtension($filename); // invalid quality if (!SpoonFilter::isBetween(1, 100, $quality)) { // strict? if ($this->strict) { throw new SpoonThumbnailException('The quality should be between 1 - 100'); } return false; } // invalid extension if (SpoonFilter::getValue($extension, array('gif', 'jpeg', 'jpg', 'png'), '') == '') { if ($this->strict) { throw new SpoonThumbnailException('Only gif, jpeg, jpg or png are allowed types.'); } return false; } // get current dimensions $imageProperties = @getimagesize($this->filename); // validate imageProperties if ($imageProperties === false) { // strict? if ($this->strict) { throw new SpoonThumbnailException('The sourcefile "' . $this->filename . '" could not be found.'); } return false; } // set current dimensions $currentWidth = (int) $imageProperties[0]; $currentHeight = (int) $imageProperties[1]; $currentType = (int) $imageProperties[2]; $currentMime = (string) $imageProperties['mime']; // file is the same? if ($currentType == IMAGETYPE_GIF && $extension == 'gif' || $currentType == IMAGETYPE_JPEG && in_array($extension, array('jpg', 'jpeg')) || $currentType == IMAGETYPE_PNG && $extension == 'png') { if ($currentWidth == $this->width && $currentHeight == $this->height) { return SpoonDirectory::copy($this->filename, $filename, true, true, $chmod); } } // resize image $this->resizeImage($currentWidth, $currentHeight, $currentType, $currentMime); // output to file switch (strtolower($extension)) { case 'gif': $return = @imagegif($this->image, $filename); break; case 'jpeg': case 'jpg': $return = @imagejpeg($this->image, $filename, $quality); break; case 'png': $return = @imagepng($this->image, $filename); break; } // chmod @chmod($filename, $chmod); // cleanup memory @imagedestroy($this->image); // return success return (bool) $return; }