<commit_before><?php

namespace Patchwork\Model;

use Symfony\Component\Validator\Constraints as Assert;
use Patchwork\App;
use Patchwork\Helper\RedBean as R;
use Patchwork\Helper\Exception;

abstract class AbstractModel extends \RedBean_SimpleModel
{
    abstract public function getAsserts();



    public function getType()
    {
        return $this->bean->getMeta('type');
    }



    public function hasField($field)
    {
        return array_key_exists($field, $this->getAsserts());
    }



    public function setImage($dir, $file)
    {
        $this->image = $file;
    }



    public function update()
    {
        if ($this->hasField('position')) {
            if ((! $this->position) || ($this->position && count(R::find($this->getType(), 'position = ? AND id != ?', array($this->position, $this->id))))) {
                $position = R::getCell('SELECT position FROM '.$this->getType().' ORDER BY position DESC LIMIT 1');
                $this->position = $position + 1;
            }
        }

        $fields = $this->bean->export();

        foreach ($fields as &$field) {
            $field = strip_tags($field);
        }

        $errors = App::getInstance()['validator']->validateValue(
            $fields,
            new Assert\Collection(
                array(
                    'fields' => $this->getAsserts(),
                    'allowExtraFields' => true
                )
            )
        );

        if (count($errors)) {
            throw new Exception('L\'enregistrement a échoué pour les raisons suivantes :', 0, null, $errors);
        }
    }



    public function delete()
    {
        if ($this->hasField('position')) {
            R::exec('UPDATE '.$this->getType().' SET position = position - 1 WHERE position > ?', array($this->position));
        }

        if ($this->hasField('image') && $this->image) {
            $dir = BASE_PATH.'/public/assets/img/'.$this->getType().'/';
            unlink($dir.$this->image);
        }
    }
}
<commit_msg>made models ignore null asserts for validation<commit_after><?php

namespace Patchwork\Model;

use Symfony\Component\Validator\Constraints as Assert;
use Patchwork\App;
use Patchwork\Helper\RedBean as R;
use Patchwork\Helper\Exception;

abstract class AbstractModel extends \RedBean_SimpleModel
{
    abstract public function getAsserts();



    public function getType()
    {
        return $this->bean->getMeta('type');
    }



    public function hasField($field)
    {
        return array_key_exists($field, $this->getAsserts());
    }



    public function setImage($dir, $file)
    {
        $this->image = $file;
    }



    public function update()
    {
        if ($this->hasField('position')) {
            if ((! $this->position) || ($this->position && count(R::find($this->getType(), 'position = ? AND id != ?', array($this->position, $this->id))))) {
                $position = R::getCell('SELECT position FROM '.$this->getType().' ORDER BY position DESC LIMIT 1');
                $this->position = $position + 1;
            }
        }

        $fields = $this->bean->export();

        foreach ($fields as &$field) {
            $field = strip_tags($field);
        }

        $asserts = $this->getAsserts();

        foreach ($asserts as $key => $assert) {
            if ($assert === null) {
                unset($asserts[$key]);
            }
        }

        $errors = App::getInstance()['validator']->validateValue(
            $fields,
            new Assert\Collection(
                array(
                    'fields' => $asserts,
                    'allowExtraFields' => true
                )
            )
        );

        if (count($errors)) {
            throw new Exception('L\'enregistrement a échoué pour les raisons suivantes :', 0, null, $errors);
        }
    }



    public function delete()
    {
        if ($this->hasField('position')) {
            R::exec('UPDATE '.$this->getType().' SET position = position - 1 WHERE position > ?', array($this->position));
        }

        if ($this->hasField('image') && $this->image) {
            $dir = BASE_PATH.'/public/assets/img/'.$this->getType().'/';
            unlink($dir.$this->image);
        }
    }
}

{'fraction_non_alphanumeric': 0.15715976331360948, 'fraction_numerical': 0.001893491124260355, 'mean_word_length': 1.999290276792051, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 56, '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': '3000261', 'n_tokens_mistral': 1231, 'n_tokens_neox': 1171, 'n_words': 259}