<commit_before><?php
namespace Tools\Model\Behavior;

use Cake\Event\Event;
use Cake\ORM\Behavior;
use Cake\ORM\Entity;
use Cake\Utility\Inflector;
use Cake\Core\Configure;
use Cake\ORM\Query;
use Cake\Utility\String;

/**
 * BitmaskedBehavior
 *
 * An implementation of bitwise masks for row-level operations.
 * You can submit/register flags in different ways. The easiest way is using a static model function.
 * It should contain the bits like so (starting with 1):
 *   1 => w, 2 => x, 4 => y, 8 => z, ... (bits as keys - names as values)
 * The order doesn't matter, as long as no bit is used twice.
 *
 * The theoretical limit for a 64-bit integer would be 64 bits (2^64).
 * But if you actually seem to need more than a hand full you
 * obviously do something wrong and should better use a joined table etc.
 *
 * @author <NAME>
 * @license MIT
 * @link http://www.dereuromark.de/2012/02/26/bitmasked-using-bitmasks-in-cakephp/
 */
class BitmaskedBehavior extends Behavior {

	/**
	 * Default config
	 *
	 * @var array
	 */
	protected $_defaultConfig = array(
		'field' => 'status',
		'mappedField' => null, // NULL = same as above
		'bits' => null, // Method or callback to get the bits data
		'on' => 'beforeValidate', // beforeSave or beforeValidate
		'defaultValue' => null, // NULL = auto (use empty string to trigger "notEmpty" rule for "default NOT NULL" db fields)
	);

	/**
	 * Behavior configuration
	 *
	 * @param array $config
	 * @return void
	 */
	public function initialize(array $config = array()) {
		$config = $this->_config;

		if (empty($config['bits'])) {
			$config['bits'] = Inflector::pluralize($config['field']);
		}

		$entity = $this->_table->newEntity();

		if (is_callable($config['bits'])) {
			$config['bits'] = call_user_func($config['bits']);

		} elseif (is_string($config['bits']) && method_exists($entity, $config['bits'])) {
			$config['bits'] = $entity->{$config['bits']}();
		} elseif (is_string($config['bits']) && method_exists($this->_table, $config['bits'])) {
			$config['bits'] = $this->_table->{$config['bits']}();
		} elseif (!is_array($config['bits'])) {
			$config['bits'] = false;
		}
		if (empty($config['bits'])) {
			throw new \Exception('Bits not found');
		}
		ksort($config['bits'], SORT_NUMERIC);

		$this->_config = $config;
	}

	/**
	 * @param Event $event
	 * @param Query $query
	 * @return void
	 */
	public function beforeFind(Event $event, Query $query) {
		$this->encodeBitmaskConditions($query);

		$field = $this->_config['field'];
		if (!($mappedField = $this->_config['mappedField'])) {
			$mappedField = $field;
		}

		$mapper = function ($row, $key, $mr) use ($field, $mappedField) {
			$row->set($mappedField, $this->decodeBitmask($row->get($field)));
			$mr->emit($row);
    };
    $query->mapReduce($mapper);
	}

	/**
	 * @param Event $event
	 * @param Entity $entity
	 * @param \ArrayObject $options
	 * @return void
	 */
	public function beforeValidate(Event $event, Entity $entity, \ArrayObject $options) {
		if ($this->_config['on'] !== 'beforeValidate' || !$options['validate']) {
			return;
		}
		$this->encodeBitmaskData($entity);
	}

	/**
	 * @param Event $event
	 * @param Entity $entity
	 * @param \ArrayObject $options
	 * @return void
	 */<commit_msg>fix cs<commit_after><?php
namespace Tools\Model\Behavior;

use Cake\Event\Event;
use Cake\ORM\Behavior;
use Cake\ORM\Entity;
use Cake\Utility\Inflector;
use Cake\Core\Configure;
use Cake\ORM\Query;
use Cake\Utility\String;

/**
 * BitmaskedBehavior
 *
 * An implementation of bitwise masks for row-level operations.
 * You can submit/register flags in different ways. The easiest way is using a static model function.
 * It should contain the bits like so (starting with 1):
 *   1 => w, 2 => x, 4 => y, 8 => z, ... (bits as keys - names as values)
 * The order doesn't matter, as long as no bit is used twice.
 *
 * The theoretical limit for a 64-bit integer would be 64 bits (2^64).
 * But if you actually seem to need more than a hand full you
 * obviously do something wrong and should better use a joined table etc.
 *
 * @author <NAME>
 * @license MIT
 * @link http://www.dereuromark.de/2012/02/26/bitmasked-using-bitmasks-in-cakephp/
 */
class BitmaskedBehavior extends Behavior {

	/**
	 * Default config
	 *
	 * @var array
	 */
	protected $_defaultConfig = array(
		'field' => 'status',
		'mappedField' => null, // NULL = same as above
		'bits' => null, // Method or callback to get the bits data
		'on' => 'beforeValidate', // beforeSave or beforeValidate
		'defaultValue' => null, // NULL = auto (use empty string to trigger "notEmpty" rule for "default NOT NULL" db fields)
	);

	/**
	 * Behavior configuration
	 *
	 * @param array $config
	 * @return void
	 */
	public function initialize(array $config = array()) {
		$config = $this->_config;

		if (empty($config['bits'])) {
			$config['bits'] = Inflector::pluralize($config['field']);
		}

		$entity = $this->_table->newEntity();

		if (is_callable($config['bits'])) {
			$config['bits'] = call_user_func($config['bits']);

		} elseif (is_string($config['bits']) && method_exists($entity, $config['bits'])) {
			$config['bits'] = $entity->{$config['bits']}();
		} elseif (is_string($config['bits']) && method_exists($this->_table, $config['bits'])) {
			$config['bits'] = $this->_table->{$config['bits']}();
		} elseif (!is_array($config['bits'])) {
			$config['bits'] = false;
		}
		if (empty($config['bits'])) {
			throw new \Exception('Bits not found');
		}
		ksort($config['bits'], SORT_NUMERIC);

		$this->_config = $config;
	}

	/**
	 * @param Event $event
	 * @param Query $query
	 * @return void
	 */
	public function beforeFind(Event $event, Query $query) {
		$this->encodeBitmaskConditions($query);

		$field = $this->_config['field'];
		if (!($mappedField = $this->_config['mappedField'])) {
			$mappedField = $field;
		}

		$mapper = function ($row, $key, $mr) use ($field, $mappedField) {
			$row->set($mappedField, $this->decodeBitmask($row->get($field)));
			$mr->emit($row);
		};
		$query->mapReduce($mapper);
	}

	/**
	 * @param Event $event
	 * @param Entity $entity
	 * @param \ArrayObject $options
	 * @return void
	 */
	public function beforeValidate(Event $event, Entity $entity, \ArrayObject $options) {
		if ($this->_config['on'] !== 'beforeValidate' || !$options['validate']) {
			return;
		}
		$this->encodeBitmaskData($entity);
	}

	/**
	 * @param Event $event
	 * @param Entity $entity
	 * @param \ArrayObject $options
	 * @return void
	 */
{'fraction_non_alphanumeric': 0.17626176879148017, 'fraction_numerical': 0.0061737922518907236, 'mean_word_length': 3.894259818731118, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 59, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '2965751', 'n_tokens_mistral': 2256, 'n_tokens_neox': 2063, 'n_words': 587}