<commit_before><?php
/**
 * AutomaticUpload.php
 *
 * PHP version 5.4+
 *
 * @author    <NAME> <<EMAIL>>
 * @copyright 2010-2014 Sweelix
 * @license   http://www.sweelix.net/license license
 * @version   XXX
 * @link      http://www.sweelix.net
 * @category  behaviors
 * @package   sweelix.yii2.plupload.behaviors
 */

namespace sweelix\yii2\plupload\behaviors;
use sweelix\yii2\plupload\components\UploadedFile;
use yii\base\Behavior;
use yii\validators\FileValidator;
use yii\db\ActiveRecord;
use yii\helpers\Json;
use yii\helpers\Html;
use Yii;
use Exception;
/**
 * This UploadedFile handle automagically the upload process in
 * models
 *
 * @author    <NAME> <<EMAIL>>
 * @copyright 2010-2014 Sweelix
 * @license   http://www.sweelix.net/license license
 * @version   XXX
 * @link      http://www.sweelix.net
 * @category  behaviors
 * @package   sweelix.yii2.plupload.behaviors
 * @since     XXX
 */
class AutomaticUpload extends Behavior {
	/**
	 * @var array handled attributes
	 */
	public $attributes=[];

	/**
	 * @var string define locale for sanitize (it would probably be better to use Yii::$app locale)
	 */
	public static $sanitizeLocale = 'fr_FR.UTF8';

	/**
	 * @var callable function used to serialize attributes. default to json
	 */
	public $serializeCallback;

	/**
	 * @var callable function used to unserialize attributes. default to json
	 */
	public $unserializeCallback;

	/**
	 * @var boolean check if we must wayt for afterInsert to save the files
	 */
	protected $modelShouldBeSaved = false;

	/**
	 * @var boolean check if we are "re-saving" data in afterSave (when we recompute the path)
	 */
	protected $modelIsUpdating = false;

	/**
	 * List of tracked events
	 *
	 * @return array
	 * @since  XXX
	 */
	public function events() {
		return [
			// ActiveRecord::EVENT_INIT => 'afterInit',
			ActiveRecord::EVENT_AFTER_FIND => 'afterFind',
			ActiveRecord::EVENT_BEFORE_INSERT => 'beforeInsert',
			ActiveRecord::EVENT_AFTER_INSERT => 'afterInsert',
			ActiveRecord::EVENT_BEFORE_UPDATE => 'beforeUpdate',
			ActiveRecord::EVENT_AFTER_UPDATE => 'afterUpdate',
			ActiveRecord::EVENT_AFTER_DELETE => 'afterDelete',
		];
	}

	/**
	 * Serialize file attribute when multifile upload is active
	 *
	 * @param string $attribute the attribute name
	 *
	 * @return void
	 * @since  XXX
	 */
	protected function serializeAttribute($attribute) {
		if($this->isMultifile($attribute) === true) {
			if(is_callable($this->serializeCallback) === true) {
				$this->owner->{$attribute} = call_user_func(array($this, 'serializeCallback'), $this->owner->{$attribute});
			} else {
				$this->owner->{$attribute} = Json::encode($this->owner->{$attribute});
			}
		} else {
			$realData = $this->owner->{$attribute};
			$this->owner->{$attribute} = array_pop($realData);
		}
	}

	/**
	 * Unserialize file attribute when multifile upload is active
	 *
	 * @param string $attribute the attribute name
	 *
	 * @return void<commit_msg>This case should not happen...<commit_after><?php
/**
 * AutomaticUpload.php
 *
 * PHP version 5.4+
 *
 * @author    <NAME> <<EMAIL>>
 * @copyright 2010-2014 Sweelix
 * @license   http://www.sweelix.net/license license
 * @version   XXX
 * @link      http://www.sweelix.net
 * @category  behaviors
 * @package   sweelix.yii2.plupload.behaviors
 */

namespace sweelix\yii2\plupload\behaviors;
use sweelix\yii2\plupload\components\UploadedFile;
use yii\base\Behavior;
use yii\validators\FileValidator;
use yii\db\ActiveRecord;
use yii\helpers\Json;
use yii\helpers\Html;
use Yii;
use Exception;
/**
 * This UploadedFile handle automagically the upload process in
 * models
 *
 * @author    <NAME> <<EMAIL>>
 * @copyright 2010-2014 Sweelix
 * @license   http://www.sweelix.net/license license
 * @version   XXX
 * @link      http://www.sweelix.net
 * @category  behaviors
 * @package   sweelix.yii2.plupload.behaviors
 * @since     XXX
 */
class AutomaticUpload extends Behavior {
	/**
	 * @var array handled attributes
	 */
	public $attributes=[];

	/**
	 * @var string define locale for sanitize (it would probably be better to use Yii::$app locale)
	 */
	public static $sanitizeLocale = 'fr_FR.UTF8';

	/**
	 * @var callable function used to serialize attributes. default to json
	 */
	public $serializeCallback;

	/**
	 * @var callable function used to unserialize attributes. default to json
	 */
	public $unserializeCallback;

	/**
	 * @var boolean check if we must wayt for afterInsert to save the files
	 */
	protected $modelShouldBeSaved = false;

	/**
	 * @var boolean check if we are "re-saving" data in afterSave (when we recompute the path)
	 */
	protected $modelIsUpdating = false;

	/**
	 * List of tracked events
	 *
	 * @return array
	 * @since  XXX
	 */
	public function events() {
		return [
			// ActiveRecord::EVENT_INIT => 'afterInit',
			ActiveRecord::EVENT_AFTER_FIND => 'afterFind',
			ActiveRecord::EVENT_BEFORE_INSERT => 'beforeInsert',
			ActiveRecord::EVENT_AFTER_INSERT => 'afterInsert',
			ActiveRecord::EVENT_BEFORE_UPDATE => 'beforeUpdate',
			ActiveRecord::EVENT_AFTER_UPDATE => 'afterUpdate',
			ActiveRecord::EVENT_AFTER_DELETE => 'afterDelete',
		];
	}

	/**
	 * Serialize file attribute when multifile upload is active
	 *
	 * @param string $attribute the attribute name
	 *
	 * @return void
	 * @since  XXX
	 */
	protected function serializeAttribute($attribute) {
		if($this->isMultifile($attribute) === true) {
			if(is_callable($this->serializeCallback) === true) {
				$this->owner->{$attribute} = call_user_func(array($this, 'serializeCallback'), $this->owner->{$attribute});
			} else {
				$this->owner->{$attribute} = Json::encode($this->owner->{$attribute});
			}
		} else {
			$realData = $this->owner->{$attribute};
			if(is_array($realData) === true) {
				$realData = array_pop($realData);
			}
			$this->owner->{$attribute} = $realData;
		}
	}

	/**
	 * Unserialize file attribute when multifile upload is active
	 *
	 * @param string $attribute the attribute name
	 *
	 * @return void
{'fraction_non_alphanumeric': 0.13688784890523148, 'fraction_numerical': 0.007688450610061842, 'mean_word_length': 3.7643312101910826, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 57, 'https://': 0, 'lorem ipsum': 0, 'www.': 8, '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': '3608585', 'n_tokens_mistral': 2059, 'n_tokens_neox': 1869, 'n_words': 475}