Example #1
0
 public function setFromLanguage($fromLanguage)
 {
     $msg = 'From language must be 2 characters wide';
     ValidateString::mustHaveLengthEqualTo($fromLanguage, 2, $trim = TRUE);
     $this->fromLanguage = trim($fromLanguage);
     return $fromLanguage;
 }
Example #2
0
 public static function yesNo($msg, LogInterface &$log)
 {
     \apf\validate\String::mustBeString($msg);
     $msg = sprintf('%s (y/n):', $msg);
     $options = ['y', 'yes', 'ya', 'ye', 'yeah', 'yep', 'n', 'no', 'nope', 'negative'];
     $select = substr(self::select($options, $msg, $log), 0, 1);
     return $select == "y";
 }
Example #3
0
 /**
  *Due to repeating code thoroughly every single method, decided to simplify all of those
  *validations in this method. This method checks that:
  *1. The first argument (namely the class name) is not empty
  *2. That the class exists
  *3. That the second argument is also not empty
  *It is of course used inside this very same class due to it's internal nature. 
  *@return Int -1 if the first argument is not a string 
  *@return Int -2 if the first argument is an empty string
  *@return Int -3 if the class doesn't exists
  *@return Int -4 if the second argument is not a string
  *@return Int -5 if the second argument is an empty string
  */
 public static function parameterValidation($name)
 {
     if (!is_string($name)) {
         return -1;
     }
     if (\apf\validate\String::isEmpty($name)) {
         return -2;
     }
     if (!class_exists($name)) {
         return -3;
     }
     return TRUE;
 }
Example #4
0
 protected static function imperativeValidation($curValue, $exCode, $msg, array $codesAndMessages = array())
 {
     self::validateUserSuppliedExceptionCode($exCode);
     if (sizeof($codesAndMessages)) {
         $codesAndMessages = array_merge(self::getStandardExceptionMessages(), $codesAndMessages);
     } else {
         $codesAndMessages = self::getStandardExceptionMessages();
     }
     foreach ($codesAndMessages as $cam) {
         if ($cam["value"] === $curValue) {
             $_msg = \apf\validate\String::isEmpty($msg) ? $cam["msg"] : $msg;
             $ex = isset($cam["exception"]) ? $cam["exception"] : "\\InvalidArgumentException";
             throw new $ex($_msg);
         }
     }
 }
Example #5
0
 public static function resize($image, $destination, $width, $height, $filter = NULL)
 {
     ImageValidate::mustBeImage($image);
     StringValidate::mustBeNotEmpty($destination, "Destination must not be empty");
     $width = (int) $width;
     $height = (int) $height;
     IntValidate::mustBePositive($width, "Image width must be a positive number");
     IntValidate::mustBePositive($height, "Image height must be a positive number");
     $filter = empty($filter) ? \Imagick::FILTER_LANCZOS : $filter;
     $destDir = dirname($destination);
     if (!is_dir($destDir)) {
         if (!@mkdir($destDir, $mode = 0777, $recursive = TRUE)) {
             throw new \Exception("Could not create directory {$destDir} to save resized image");
         }
     }
     $resize = new \Imagick();
     $resize->readImage($image);
     $resize->resizeImage($width, $height, $filter, 1, $bestFit = TRUE);
     $resize->writeImage($destination);
     $resize->clear();
     $resize->destroy();
 }
Example #6
0
 private static function instanceExists($name = NULL)
 {
     \apf\validate\String::mustBeNotEmpty($name, "When checking for existing instance, name can't be empty");
     return array_key_exists($name, self::$instances);
 }
Example #7
0
 public function setName($hostName)
 {
     $this->name = StringValidate::mustBeNotEmpty($hostName, $useTrim = TRUE, 'Host name can not be empty');
     return $this;
 }
Example #8
0
 public function addSchema($name = NULL)
 {
     $name = trim($name);
     if (strpos($name, ' ')) {
         throw new \Exception("Schema name must NOT have any spaces in it");
     }
     if (in_array($name, $this->schemas)) {
         throw new \Exception("Duplicated schema name \"{$name}\"");
     }
     $this->schemas[] = \apf\validate\String::mustBeNotEmpty($name, "Schema name can't be empty");
 }
Example #9
0
 public function setURI($uri)
 {
     $this->uri = StringValidate::mustBeNotEmpty($uri, $trim = TRUE, 'Asset URI must be not empty');
     return $this;
 }
Example #10
0
 public function __construct($value)
 {
     \apf\validate\String::mustBeString($value, $trim = TRUE);
     $this->value = sprintf('%s', $value);
 }
Example #11
0
 public static function toSlug($string, $char = '-')
 {
     StringValidate::mustBeNotEmpty($string, $trim = TRUE, "Must provide a string to slugify");
     $string = preg_replace('/&/', '', $string);
     $string = preg_replace('/\\W/', $char, self::toAscii($string));
     $string = strtolower(preg_replace('/[-]{2,}/', '-', $string));
     $string = preg_replace('/[^a-zA-Z0-9\\-]/', '', $string);
     return trim($string, '-');
 }
Example #12
0
 public function setDatabase($database)
 {
     $this->database = StringValidate::mustBeNotEmpty($database, $useTrim = TRUE, 'Database name can not be empty');
     return $this;
 }
Example #13
0
 public function loadFragmentAsString($name = NULL, $folder = NULL, $data = NULL)
 {
     $args = func_get_args();
     $data = NULL;
     if (array_key_exists(2, $args)) {
         $data = $args[2];
         if (empty($data)) {
             return FALSE;
         }
     }
     String::mustBeNotEmpty($name, "Fragment name must be a non empty string");
     if (!String::isString($folder)) {
         $folder = $this->_controller;
     }
     $name = trim($name);
     $fw = DI::get('config')->framework;
     if (!isset($fw->fragments) || empty($fw->fragments)) {
         throw new \Exception("Don't know how to load fragments, no path was specified in configuration");
     }
     $ds = DIRECTORY_SEPARATOR;
     $fragment = sprintf('%s%s%s%s', Kernel::getAppDir(), $ds, $fw->fragments, $ds);
     $fragment = sprintf('%s%s%s%s', $fragment, $folder, $ds, $name);
     $fragment = new \apf\core\File($fragment);
     $this->assignConfigVars();
     ob_start();
     if ($this->fragmentDebug) {
         echo "<div class=\"__apf_fragment_debug\">";
     }
     require $fragment;
     if ($this->fragmentDebug) {
         echo "</div>";
     }
     $content = ob_get_contents();
     ob_end_clean();
     return $content;
 }