public function SetPassword($value, $repeatValue) { // Check if passwords match if ($value != $repeatValue) { ValidationService::AddValidationError(self::$constraints['password']['doNotMatchMsg']); return false; } // Check if password is valid if ($this->IsValidString("Password", $value, self::$constraints["password"])) { // Set password $this->password = trim($value); return true; } return false; }
protected function IsValidString($stringName, $stringContent, $constraints = []) { // Default values if (!isset($constraints['minLength'])) { $constraints['minLength'] = 1; } if (!isset($constraints['maxLength'])) { $constraints['maxLength'] = 100; } if (!isset($constraints['regex'])) { $constraints['regex'] = '/[^a-z_\\-0-9]/i'; } if (!isset($constraints['throwException'])) { $constraints['throwException'] = false; } // Default messages if (!isset($constraints['emptyMsg'])) { $constraints['emptyMsg'] = "{$stringName} is missing"; } if (!isset($constraints['minLengthMsg'])) { $constraints['minLengthMsg'] = "{$stringName} has too few characters, at least " . $constraints['minLength'] . " characters."; } if (!isset($constraints['maxLengthMsg'])) { $constraints['maxLengthMsg'] = "{$stringName} is too long. Max length is " . $constraints['maxLength'] . " characters."; } if (!isset($constraints['regexMsg'])) { $constraints['regexMsg'] = "{$stringName} contains invalid characters."; } // Check if $stringContent is empty if ($constraints['minLength'] == 1 && trim(strlen($stringContent)) == 0) { // Throw exception if specified if ($constraints['throwException']) { throw new \Exception($constraints['emptyMsg']); } ValidationService::AddValidationError($constraints['emptyMsg']); } // Check if $stringContent is too short if ($constraints['minLength'] > 1 && trim(strlen($stringContent)) < $constraints['minLength']) { // Throw exception if specified if ($constraints['throwException']) { throw new \Exception($constraints['minLengthMsg']); } ValidationService::AddValidationError($constraints['minLengthMsg']); } // Check if $stringContent is too long if (strlen($stringContent) > $constraints['maxLength']) { // Throw exception if specified if ($constraints['throwException']) { throw new \Exception($constraints['maxLengthMsg']); } ValidationService::AddValidationError($constraints['maxLength']); } // Check if $stringContent is valid if (preg_match($constraints['regex'], $stringContent)) { // Throw exception if specified if ($constraints['throwException']) { throw new \Exception($constraints['regexMsg']); } ValidationService::AddValidationError($constraints['regexMsg']); } return true; }
protected function IsClassType($objName, $objContent, $constraints = []) { // Return false if classType is not defined if (!isset($constraints['classType'])) { $constraints['classType'] = 'Unspecified'; } // Default settings if (!isset($constraints['allowNull'])) { $constraints['allowNull'] = false; } // Do not throw exception as default if (!isset($constraints['throwException'])) { $constraints['throwException'] = false; } // Default messages if (!isset($constraints['notClassTypeMsg'])) { $constraints['notClassTypeMsg'] = "{$objName} måste vara ett objekt av typen: " . $constraints['classType']; } // Check if its a valid class if (!($constraints['allowNull'] && is_null($objContent)) && !$objContent instanceof $constraints['classType']) { // Throw exception if specified if ($constraints['throwException']) { throw new \Exception($constraints['notClassTypeMsg']); } ValidationService::AddValidationError($constraints['notClassTypeMsg']); return false; } return true; }