<commit_before><?php namespace Bkwld\Decoy\Observers;

// Dependencies
use Input;

/**
 * Take input from a Many to Many Checklist and commit it to the db,
 * updating the relationshups
 */
class ManyToManyChecklist {

	/**
	 * @var string The form element prefix
	 */
	const PREFIX = '_many_to_many_';

	/**
	 * Take input from a Many to Many Checklist and commit it to the db. Called
	 * on model saved.
	 * 
	 * @param Bkwld\Decoy\Models\Base $model 
	 */
	public function handle($model) {

		// Check for matching input elements
		foreach(Input::get() as $key => $val) {
			if (preg_match('#^'.self::PREFIX.'(.+)#', $key, $matches)) {
				$this->updateRelationship($model, $matches[1]);
			}
		}

	}

	/**
	 * Process a particular input instance
	 * 
	 * @param Bkwld\Decoy\Models\Base $model A model instance
	 * @param string $relationship The relationship name
	 */
	private function updateRelationship($model, $relationship) {

		// Strip all the "0"s from the input.  These exist because push checkboxes is globally
		// set for all of Decoy;
		$ids = Input::get(self::PREFIX.$relationship);
		$ids = array_filter($ids, function($id) { return $id > 0; });

		// Attach just the ones mentioned in the input.  This blows away the previous joins
		$model->$relationship()->sync($ids);

	}

}<commit_msg>Fire events during many to many checklist saving, allowing pivot data to be added<commit_after><?php namespace Bkwld\Decoy\Observers;

// Dependencies
use Event;
use Input;

/**
 * Take input from a Many to Many Checklist and commit it to the db,
 * updating the relationshups
 */
class ManyToManyChecklist {

	/**
	 * @var string The form element prefix
	 */
	const PREFIX = '_many_to_many_';

	/**
	 * Take input from a Many to Many Checklist and commit it to the db. Called
	 * on model saved.
	 * 
	 * @param Bkwld\Decoy\Models\Base $model 
	 */
	public function handle($model) {

		// Check for matching input elements
		foreach(Input::get() as $key => $val) {
			if (preg_match('#^'.self::PREFIX.'(.+)#', $key, $matches)) {
				$this->updateRelationship($model, $matches[1]);
			}
		}
	}

	/**
	 * Process a particular input instance
	 * 
	 * @param Bkwld\Decoy\Models\Base $model A model instance
	 * @param string $relationship The relationship name
	 */
	private function updateRelationship($model, $relationship) {

		// Strip all the "0"s from the input.  These exist because push checkboxes 
		// is globally set for all of Decoy;
		$ids = Input::get(self::PREFIX.$relationship);
		$ids = array_filter($ids, function($id) { return $id > 0; });

		// Allow a single listener to transform the list of ids to, for instance, 
		// add pivot data.
		$prefix = 'decoy::many-to-many-checklist.';
		if ($mutated = Event::until($prefix."syncing: $relationship", [$ids])) {
			$ids = $mutated;
		}

		// Attach just the ones mentioned in the input.  This blows away the 
		// previous joins
		$model->$relationship()->sync($ids);

		// Fire completion event
		Event::fire($prefix."synced: $relationship");
	}

}
{'fraction_non_alphanumeric': 0.13534083388484447, 'fraction_numerical': 0.001985440105890139, 'mean_word_length': 3.4718934911242605, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 13, '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': '5520135', 'n_tokens_mistral': 1032, 'n_tokens_neox': 909, 'n_words': 317}