<commit_before><?php
namespace Zizaco\Mongolid;

use MongoClient, MongoDate;

class Model
{
    /**
     * The connection name for the model.
     *
     * @var MongoDB
     */
    public static $connection;

    /**
     * The collection associated with the model.
     *
     * @var string
     */
    protected $collection = null;

    /**
     * The database associated with the model.
     *
     * @var string
     */
    protected $database = null;

    /**
     * The Laravel's cache component. Or other cache manager that
     * has a method with the same signature
     *
     * @var CacheComponent
     */
    public static $cacheComponent = null;

    /**
     * Indicates if the model should be timestamped.
     *
     * @var bool
     */
    public $timestamps = true;

    /**
     * The model's attributes.
     *
     * @var array
     */
    protected $attributes = array();

    /**
     * The model attribute's original state.
     *
     * @var array
     */
    protected $original = array();

    /**
     * Once you put at least one string in this array, only
     * the attributes especified here will be changed
     * with the setAttributes method.
     *
     * @var array
     */
    public $fillable = array();

    /**
     * The attributes that aren't mass assignable. The oposite
     * to the fillable array;
     *
     * @var array
     */
    public $guarded = array();

    /**
     * Save the model to the database.
     *
     * @return bool
     */
    public function save()
    {
        // If the model has no collection. Aka: embeded model
        if (! $this->collection) return false;

        // If the "saving" event returns false we'll bail out of the save and return
        // false, indicating that the save failed. This gives an opportunities to
        // listeners to cancel save operations if validations fail or whatever.
        if ($this->fireModelEvent('saving') === false) {
            return false;
        }

        // To this model exists and are being updated?
        if ($this->_id) {

            // If the updating event returns false, we will cancel the update operation so
            // developers can hook Validation systems into their models and cancel this
            // operation if the model does not pass validation. Otherwise, we update.
            if ($this->fireModelEvent('updating') === false) {
                return false;
            }
        } else {

            // If the model has not an _id then fire the creating event
            if ($this->fireModelEvent('creating') === false) {
                return false;
            }
        }

        // Prepare the created_at and updated_at attributes for the given model
        $this->prepareTimestamps();

        // Prepare the attributes of the model
        $preparedAttr = $this->prepareMongoAttributes( $this->attributes );

        // Saves the model using the MongoClient
        $result = $this->collection()
            ->save( $preparedAttr, array("w" => 1) );

        if (isset($result['ok']) && $result['ok'] ) {

            if ($this->_id) {
                // Once we have run the update operation, we will fire the "updated" event for
                // this model instance. This will allow developers to hook into these after
                // models are updated, giving them a chance to do any special processing.
                $this->parseDocument($this->attributes);
                $this->fireModelEvent('updated', false);
            } else {
                // the created event is fired, just in case the developer tries to update it
                // during the event. This will allow them to do so and run an update here.
                $this->parseDocument($this->attributes);
                $this->fireModelEvent('created', false);
            }

            // The "saved" event will always be fired when inserting or updating an model<commit_msg>writeConcern attribute in Mongolid\Model in order to be able to choose the write concern to be used when saving an object of the given model.<commit_after><?php
namespace Zizaco\Mongolid;

use MongoClient, MongoDate;

class Model
{
    /**
     * The connection name for the model.
     *
     * @var MongoDB
     */
    public static $connection;

    /**
     * The collection associated with the model.
     *
     * @var string
     */
    protected $collection = null;

    /**
     * The database associated with the model.
     *
     * @var string
     */
    protected $database = null;

    /**
     * The Laravel's cache component. Or other cache manager that
     * has a method with the same signature
     *
     * @var CacheComponent
     */
    public static $cacheComponent = null;

    /**
     * Indicates if the model should be timestamped.
     *
     * @var bool
     */
    public $timestamps = true;

    /**
     * Write concern to be used when saving model.
     * -1 = Errors Ignored
     * 0 = Unacknowledged
     * 1 = Acknowledged
     * See: http://docs.mongodb.org/manual/core/write-concern/
     *
     * @var integer
     */
    public $writeConcern = 1;

    /**
     * The model's attributes.
     *
     * @var array
     */
    protected $attributes = array();

    /**
     * The model attribute's original state.
     *
     * @var array
     */
    protected $original = array();

    /**
     * Once you put at least one string in this array, only
     * the attributes especified here will be changed
     * with the setAttributes method.
     *
     * @var array
     */
    public $fillable = array();

    /**
     * The attributes that aren't mass assignable. The oposite
     * to the fillable array;
     *
     * @var array
     */
    public $guarded = array();

    /**
     * Save the model to the database.
     *
     * @return bool
     */
    public function save()
    {
        // If the model has no collection. Aka: embeded model
        if (! $this->collection) return false;

        // If the "saving" event returns false we'll bail out of the save and return
        // false, indicating that the save failed. This gives an opportunities to
        // listeners to cancel save operations if validations fail or whatever.
        if ($this->fireModelEvent('saving') === false) {
            return false;
        }

        // To this model exists and are being updated?
        if ($this->_id) {

            // If the updating event returns false, we will cancel the update operation so
            // developers can hook Validation systems into their models and cancel this
            // operation if the model does not pass validation. Otherwise, we update.
            if ($this->fireModelEvent('updating') === false) {
                return false;
            }
        } else {

            // If the model has not an _id then fire the creating event
            if ($this->fireModelEvent('creating') === false) {
                return false;
            }
        }

        // Prepare the created_at and updated_at attributes for the given model
        $this->prepareTimestamps();

        // Prepare the attributes of the model
        $preparedAttr = $this->prepareMongoAttributes( $this->attributes );

        // Saves the model using the MongoClient
        $result = $this->collection()
            ->save( $preparedAttr, array("w" => $this->writeConcern) );

        if (isset($result['ok']) && $result['ok'] ) {

            if ($this->_id) {
                // Once we have run the update operation, we will fire the "updated" event for
                // this model instance. This will allow developers to hook into these after
                // models are updated, giving them a chance to do any special processing.
                $this->parseDocument($this->attributes);
                $this->fireModelEvent('updated', false);
            } else {
                // the created event is fired, just in case the developer tries to update it
                // during the event. This will allow them to do so and run an update here.
                $this->parseDocument($this->attributes);
                $this->fireModelEvent('created', false);
            }

            // The "saved" event will always be fired when inserting or updating an model
{'fraction_non_alphanumeric': 0.09561223256376164, 'fraction_numerical': 0.0006043756799226399, 'mean_word_length': 1.8171603677221655, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 40, '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': '6318771', 'n_tokens_mistral': 2150, 'n_tokens_neox': 2046, 'n_words': 831}