<commit_before><?php

namespace Jasny\DB\Entity\Validation;

use Jasny\ValidationResult,
    Jasny\DB\Entity\ChangeAware,
    Jasny\DB\Entity\SelfAware;

/**
 * Validate using meta
 */
trait MetaImplementation
{
    /**
     * Validate entity
     * 
     * @return ValidationResult
     */
    public function validate()
    {
        $validation = new ValidationResult();
        
        foreach (static::meta()->ofProperties() as $prop => $meta) {
            if ($this instanceof ChangeAware && !$this->isNew() && !$this->hasModified($prop)) {
                continue;
            }
            
            if (isset($meta['required']) && !isset($this->$prop)) {
                $validation->addError("%s is required", $prop);
            }
            
            if (!isset($this->$prop)) continue;
            
            $validation->add($this->validateBasics($prop, $meta));
            
            if (isset($meta['unique'])) {
                $uniqueGroup = is_string($meta['unique']) ? $meta['unique'] : null;
                
                if (!$this instanceof SelfAware) {
                    trigger_error(static::class . " can't check if it has a unique $prop", E_USER_WARNING);
                } elseif (!$this->hasUnique($prop, $uniqueGroup)) {
                    $validation->addError("There is already a %s with this %s", get_class($this), $prop);
                }
            }
            
            if (isset($meta['immutable'])) {
                if (!$this instanceof ChangeAware) {
                    trigger_error(static::class . " can't check if $prop has changed", E_USER_WARNING);
                } elseif (!$this->isNew()) {
                    $validation->addError("%s shouldn't be modified", $prop);
                }
            }<commit_msg>Basic validation should happen after dataset validation<commit_after><?php

namespace Jasny\DB\Entity\Validation;

use Jasny\ValidationResult,
    Jasny\DB\Entity\ChangeAware,
    Jasny\DB\Entity\SelfAware;

/**
 * Validate using meta
 */
trait MetaImplementation
{
    /**
     * Validate entity
     * 
     * @return ValidationResult
     */
    public function validate()
    {
        $validation = new ValidationResult();
        
        foreach (static::meta()->ofProperties() as $prop => $meta) {
            if ($this instanceof ChangeAware && !$this->isNew() && !$this->hasModified($prop)) {
                continue;
            }
            
            if (isset($meta['required']) && !isset($this->$prop)) {
                $validation->addError("%s is required", $prop);
            }
            
            if (!isset($this->$prop)) continue;
            
            if (isset($meta['unique'])) {
                $uniqueGroup = is_string($meta['unique']) ? $meta['unique'] : null;
                
                if (!$this instanceof SelfAware) {
                    trigger_error(static::class . " can't check if it has a unique $prop", E_USER_WARNING);
                } elseif (!$this->hasUnique($prop, $uniqueGroup)) {
                    $validation->addError("There is already a %s with this %s", get_class($this), $prop);
                    continue;
                }
            }
            
            if (isset($meta['immutable'])) {
                if (!$this instanceof ChangeAware) {
                    trigger_error(static::class . " can't check if $prop has changed", E_USER_WARNING);
                } elseif (!$this->isNew()) {
                    $validation->addError("%s shouldn't be modified", $prop);
                    continue;
                }
            }
            
            $validation->add($this->validateBasics($prop, $meta));
{'fraction_non_alphanumeric': 0.14169381107491857, 'fraction_numerical': 0.0, 'mean_word_length': 1.68, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 29, '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': '1880330', 'n_tokens_mistral': 975, 'n_tokens_neox': 955, 'n_words': 208}