<commit_before><?php
/**
 * Garp_Model_Behavior_Sluggable
 * Generates a unique slug for a record
 * @author <NAME>, <NAME> | grrr.nl
 * @modifiedby $LastChangedBy: $
 * @version $Revision: $
 * @package Garp
 * @subpackage Behavior
 * @lastmodified $Date: $
 */
class Garp_Model_Behavior_Sluggable extends Garp_Model_Behavior_Abstract {
	/**
	 * Version separator (e.g. <base><version-separator><int>)
	 * @var String
	 */
	const VERSION_SEPARATOR = '-';
		
	
	/**
	 * Configuration
	 * @var Array
	 */
	protected $_config;
	
	
	/**
	 * Make sure the config array is at least filled with some default values to work with.
	 * @param Array $config Configuration values
	 * 					String or Array ['baseField']	String:
	 * 														The database column on which the slug should be based.
	 * 													Array:
	 * 														if $config['slugField'] is empty or a single value:
	 * 															A list of database columns that should be combined to base the slug on.
	 * 														if $config['slugField'] is an array of more than one item:
	 * 															A list of database columns to base the slugs on, corresponding in length
	 * 															Note that slugs combined of multiple fields are not possible in this configuration.
	 * @return Void
	 */
	protected function _setup($config) {
		if (!array_key_exists('baseField', $config)) {
			throw new Garp_Model_Behavior_Exception('"baseField" is a required config key');
		}
		$config['slugField'] = array_key_exists('slugField', $config) ? $config['slugField'] : 'slug';

		$baseFieldCount = count($config['baseField']);
		$slugFieldCount = count($config['slugField']);

		if ($slugFieldCount > 1) {
			/**
			 * There are multiple slug fields. 'slugField' must be an array as well
			 * with the same length.
			 */
			if ($baseFieldCount !== $slugFieldCount) {
				throw new Garp_Model_Behavior_Exception('"baseField" and "slugField" need to have the same '.
														'length when using multiple slugFields.');
			}			
		}
		$this->_config = $config;
	}
	
	
	/**
	 * Before insert callback. Manipulate the new data here.
	 * @param Array $options The new data is in $args[1]
	 * @return Void
	 */
	public function beforeInsert(array &$args) {
		$model =  $args[0];
		$data  = &$args[1];
		/**
		 * Multiple slug fields might be generated. That's why 
		 * we turn the values in arrays for easy processing.
		 */
		$baseFields = (array)$this->_config['baseField'];
		$slugFields = (array)$this->_config['slugField'];

		if (
			count($baseFields) > 1 &&
			count($slugFields) <= 1
		) {
			/**
			* This slug is composed of multiple basefields
			*/
			$baseData = '';
			foreach ($baseFields as $b) {
				if (!empty($data[$b])) {
					$baseData .= (!empty($baseData) ? ' ':'').$data[$b];
				}
			}
			$slugField = current($slugFields);
			$data[$slugField] = $this->generateUniqueSlug($baseData, $model, $slugField);
		} else {
			/**
			* This record has one or more slugfields that are composed of a single basefield
			*/
			foreach ($baseFields as $i => $baseField) {
				$baseData = $data[$baseField];
				if (!empty($baseData)) {
					$slugField = $slugFields[$i];
					$data[$slugField] = $this->generateUniqueSlug($baseData, $model, $slugField);
				}<commit_msg>Fixed sloppy if statement<commit_after><?php
/**
 * Garp_Model_Behavior_Sluggable
 * Generates a unique slug for a record
 * @author <NAME>, <NAME> | grrr.nl
 * @modifiedby $LastChangedBy: $
 * @version $Revision: $
 * @package Garp
 * @subpackage Behavior
 * @lastmodified $Date: $
 */
class Garp_Model_Behavior_Sluggable extends Garp_Model_Behavior_Abstract {
	/**
	 * Version separator (e.g. <base><version-separator><int>)
	 * @var String
	 */
	const VERSION_SEPARATOR = '-';
		
	
	/**
	 * Configuration
	 * @var Array
	 */
	protected $_config;
	
	
	/**
	 * Make sure the config array is at least filled with some default values to work with.
	 * @param Array $config Configuration values
	 * 					String or Array ['baseField']	String:
	 * 														The database column on which the slug should be based.
	 * 													Array:
	 * 														if $config['slugField'] is empty or a single value:
	 * 															A list of database columns that should be combined to base the slug on.
	 * 														if $config['slugField'] is an array of more than one item:
	 * 															A list of database columns to base the slugs on, corresponding in length
	 * 															Note that slugs combined of multiple fields are not possible in this configuration.
	 * @return Void
	 */
	protected function _setup($config) {
		if (!array_key_exists('baseField', $config)) {
			throw new Garp_Model_Behavior_Exception('"baseField" is a required config key');
		}
		$config['slugField'] = array_key_exists('slugField', $config) ? $config['slugField'] : 'slug';

		$baseFieldCount = count($config['baseField']);
		$slugFieldCount = count($config['slugField']);

		if ($slugFieldCount > 1) {
			/**
			 * There are multiple slug fields. 'slugField' must be an array as well
			 * with the same length.
			 */
			if ($baseFieldCount !== $slugFieldCount) {
				throw new Garp_Model_Behavior_Exception('"baseField" and "slugField" need to have the same '.
														'length when using multiple slugFields.');
			}			
		}
		$this->_config = $config;
	}
	
	
	/**
	 * Before insert callback. Manipulate the new data here.
	 * @param Array $options The new data is in $args[1]
	 * @return Void
	 */
	public function beforeInsert(array &$args) {
		$model =  $args[0];
		$data  = &$args[1];
		/**
		 * Multiple slug fields might be generated. That's why 
		 * we turn the values in arrays for easy processing.
		 */
		$baseFields = (array)$this->_config['baseField'];
		$slugFields = (array)$this->_config['slugField'];

		if (
			count($baseFields) > 1 &&
			count($slugFields) <= 1
		) {
			/**
			* This slug is composed of multiple basefields
			*/
			$baseData = '';
			foreach ($baseFields as $b) {
				if (!empty($data[$b])) {
					$baseData .= (!empty($baseData) ? ' ':'').$data[$b];
				}
			}
			$slugField = current($slugFields);
			$data[$slugField] = $this->generateUniqueSlug($baseData, $model, $slugField);
		} else {
			/**
			* This record has one or more slugfields that are composed of a single basefield
			*/
			foreach ($baseFields as $i => $baseField) {
				if (!empty($data[$baseField])) {
					$baseData = $data[$baseField];
					$slugField = $slugFields[$i];
					$data[$slugField] = $this->generateUniqueSlug($baseData, $model, $slugField);
				}
{'fraction_non_alphanumeric': 0.1336504526622679, 'fraction_numerical': 0.0018413380389749885, 'mean_word_length': 3.061059190031153, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 29, 'https://': 0, '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': '6332915', 'n_tokens_mistral': 2445, 'n_tokens_neox': 1993, 'n_words': 613}