<commit_before><?php 

namespace Phalcon\Mvc\Model\Validator {

	/**
	 * Phalcon\Mvc\Model\Validator\StringLength
	 *
	 * Simply validates specified string length constraints
	 *
	 *<code>
	 *use Phalcon\Mvc\Model\Validator\StringLength as StringLengthValidator;
	 *
	 *class Subscriptors extends Phalcon\Mvc\Model
	 *{
	 *
	 *	public function validation()
	 *	{
	 *		$this->validate(new StringLengthValidator(array(
	 *			'field' => 'name_last',
	 *			'max' => 50,
	 *			'min' => 2,
	 *			'messageMaximum' => 'We don\'t like really long names',
	 *			'messageMinimum' => 'We want more than just their initials'
	 *		)));
	 *		if ($this->validationHasFailed() == true) {
	 *			return false;
	 *		}
	 *	}
	 *
	 *}
	 *</code>
	 *
	 */
	
	class StringLength extends \Phalcon\Mvc\Model\Validator implements \Phalcon\Mvc\Model\ValidatorInterface {

		/**
		 * Executes the validator
		 *
		 * @param \Phalcon\Mvc\ModelInterface $record
		 * @return boolean
		 */
		public function validate($record){ }

	}
}<commit_msg>MVC string length validator implementation<commit_after><?php
/**
 * String Length Validator
 *
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 * @version 1.2.6
 * @package Phalcon
*/
namespace Phalcon\Mvc\Model\Validator;

use \Phalcon\Mvc\Model\Validator,
	\Phalcon\Mvc\Model\ValidatorInterface,
	\Phalcon\Mvc\Model\Exception,
	\Phalcon\Mvc\ModelInterface;

/**
 * Phalcon\Mvc\Model\Validator\StringLength
 *
 * Simply validates specified string length constraints
 *
 *<code>
 *use Phalcon\Mvc\Model\Validator\StringLength as StringLengthValidator;
 *
 *class Subscriptors extends Phalcon\Mvc\Model
 *{
 *
 *	public function validation()
 *	{
 *		$this->validate(new StringLengthValidator(array(
 *			'field' => 'name_last',
 *			'max' => 50,
 *			'min' => 2,
 *			'messageMaximum' => 'We don\'t like really long names',
 *			'messageMinimum' => 'We want more than just their initials'
 *		)));
 *		if ($this->validationHasFailed() == true) {
 *			return false;
 *		}
 *	}
 *
 *}
 *</code>
 *
 * @see https://github.com/phalcon/cphalcon/blob/1.2.6/ext/mvc/model/validator/stringlength.c
 */
class StringLength extends Validator implements ValidatorInterface
{
	/**
	 * Executes the validator
	 *
	 * @param \Phalcon\Mvc\ModelInterface $record
	 * @return boolean
	 * @throws Exception
	 */
	public function validate($record)
	{
		if(is_object($record) === false ||
			$record instanceof ModelInterface === false) {
			throw new Exception('Invalid parameter type.');
		}

		$field = $this->getOption('field');
		if(is_string($field) === false) {
			throw new Exception('Field name must be a string');
		}

		$isset_min = $this->isSetOption('min');
		$isset_max = $this->isSetOption('max');

		//At least one of 'min' or 'max' must be set
		if($isset_min === false &&
			$isset_max === false) {
			throw new Exception('A minimum or maximum must be set');
		}

		$value = $record->readAttribute($field);

		//Check if mbstring is available to calculate the correct length
		if(function_exists('mb_strlen') === true) {
			$length = mb_strlen($value);
		} else {
			$length = strlen($value);
		}

		//Maximum length
		if($isset_max === true) {
			$maximum = $this->getOption('max');
			if($maximum < $length) {
				//Check if the developer has defined a custom message
				$message = $this->getOption('messageMaximum');
				if(isset($message) === false) {
					$message = "Value of field '".$field."' exceeds the maximum ".$maximum.' characters';
				}

				$this->appendMessage($message, $field, 'TooLong');
				return false;
			}
		}

		//Minimum length
		if($isset_min === true) {
			$minimum = $this->getOption('min');
			if($length < $minimum) {
				//Check if the developer has defined a custom message
				$message = $this->getOption('messageMinimum');
				if(isset($message) === false) {
					$message = "Value of field '".$field."' is less than the minimum ".$minimum.' characters';
				}

				$this->appendMessage($message, $field, 'TooShort');
				return false;
			}
		}

		return true;
	}
}
{'fraction_non_alphanumeric': 0.1604542088373241, 'fraction_numerical': 0.0029622315477659837, 'mean_word_length': 3.4674751929437706, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 40, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5890253', 'n_tokens_mistral': 1495, 'n_tokens_neox': 1325, 'n_words': 310}