<commit_before><?php

/*
 * This file is part of the Pho package.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Pho\Framework\Handlers;

use Pho\Framework\ParticleInterface;
use Pho\Framework\Cargo\FieldsCargo;
use Pho\Framework\Exceptions\InvalidEdgeHeadTypeException;
use Webmozart\Assert\Assert;

/**
 * Setter Handler
 * 
 * @author <NAME> <<EMAIL>>
 */
class Set implements HandlerInterface
{

    /**
     * {@inheritDoc}
     * 
     * @throws \InvalidArgumentException thrown when there argument does not meet the constraints.
     */
    public static function handle(
        ParticleInterface $particle,
        array $pack,
        string $name, 
        array $args
        ) /*:  \Pho\Lib\Graph\EntityInterface*/
    {
        if( Utils::fieldExists($pack["fields"], substr($name, 3)) ) {
            return static::field($particle, $pack["fields"], substr($name, 3), $args);
        }
        $check = false;
        foreach($pack["out"]->setter_label_settable_pairs[$name] as $settable) {
            $check |= is_a($args[0], $settable);
        }
        if(!$check) { 
            throw new InvalidEdgeHeadTypeException($args[0], $pack["out"]->setter_label_settable_pairs[$name]);
        }
        $edge = new $pack["out"]->setter_classes[$name]($particle, $args[0]);
        return $edge->return();
    }

    /**
     * Sets the field value
     *
     * @param ParticleInterface $particle
     * @param FieldsCargo $cargo
     * @param string $name Field name
     * @param array $args Field argument; [0] mixed, the argument. [1] ?bool defer_persist
     * 
     * @return void
     * 
     * @throws \InvalidArgumentException thrown when there argument does not meet the constraints.
     */
    protected static function field(
        ParticleInterface $particle,
        FieldsCargo $cargo,
        string $name,
        array $value 
        ): void
    {
        $defer_persist = false;
        if( isset($value[1]) && $value[1] )
            $defer_persist = true;
        $value = $value[0];
        $name = Utils::findFieldName($cargo, $name);
        if(isset($cargo->fields[$name]["constraints"])) {
                static::probeField($cargo->fields[$name]["constraints"], $value);
        }
        $value = static::applyDirectives($value, $cargo->fields[$name]);
        if($defer_persist) {
            $particle->attributes()->$name = $value;
        }
        $particle->attributes()->quietSet($name, $value);
    }

    /**
     * Applies directives to the value to return
     * 
     * @param mixed $value The value to check.
     * @param array $directives Particle directives.
     * 
     * @return mixed 
     */
    protected static function applyDirectives(/*mixed*/ $value, array $field_settings) /*: mixed*/
    {
        if(!isset($field_settings["directives"]))
            return $value;<commit_msg>bugfix<commit_after><?php

/*
 * This file is part of the Pho package.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Pho\Framework\Handlers;

use Pho\Framework\ParticleInterface;
use Pho\Framework\Cargo\FieldsCargo;
use Pho\Framework\Exceptions\InvalidEdgeHeadTypeException;
use Webmozart\Assert\Assert;

/**
 * Setter Handler
 * 
 * @author <NAME> <<EMAIL>>
 */
class Set implements HandlerInterface
{

    /**
     * {@inheritDoc}
     * 
     * @throws \InvalidArgumentException thrown when there argument does not meet the constraints.
     */
    public static function handle(
        ParticleInterface $particle,
        array $pack,
        string $name, 
        array $args
        ) /*:  \Pho\Lib\Graph\EntityInterface*/
    {
        if( Utils::fieldExists($pack["fields"], substr($name, 3)) ) {
            return static::field($particle, $pack["fields"], substr($name, 3), $args);
        }
        $check = false;
        foreach($pack["out"]->setter_label_settable_pairs[$name] as $settable) {
            $check |= is_a($args[0], $settable);
        }
        if(!$check) { 
            throw new InvalidEdgeHeadTypeException($args[0], $pack["out"]->setter_label_settable_pairs[$name]);
        }
        $edge = new $pack["out"]->setter_classes[$name]($particle, $args[0]);
        return $edge->return();
    }

    /**
     * Sets the field value
     *
     * @param ParticleInterface $particle
     * @param FieldsCargo $cargo
     * @param string $name Field name
     * @param array $args Field argument; [0] mixed, the argument. [1] ?bool defer_persist
     * 
     * @return void
     * 
     * @throws \InvalidArgumentException thrown when there argument does not meet the constraints.
     */
    protected static function field(
        ParticleInterface $particle,
        FieldsCargo $cargo,
        string $name,
        array $value 
        ): void
    {
        $defer_persist = false;
        if( isset($value[1]) && $value[1] )
            $defer_persist = true;
        $value = $value[0];
        $name = Utils::findFieldName($cargo, $name);
        if(isset($cargo->fields[$name]["constraints"])) {
                static::probeField($cargo->fields[$name]["constraints"], $value);
        }
        $value = static::applyDirectives($value, $cargo->fields[$name]);
        if(!$defer_persist) {
            $particle->attributes()->$name = $value;
        }
        $particle->attributes()->quietSet($name, $value);
    }

    /**
     * Applies directives to the value to return
     * 
     * @param mixed $value The value to check.
     * @param array $directives Particle directives.
     * 
     * @return mixed 
     */
    protected static function applyDirectives(/*mixed*/ $value, array $field_settings) /*: mixed*/
    {
        if(!isset($field_settings["directives"]))
            return $value;
{'fraction_non_alphanumeric': 0.13831932773109243, 'fraction_numerical': 0.0033613445378151263, 'mean_word_length': 2.5233866193013617, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 37, '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': '4313455', 'n_tokens_mistral': 1781, 'n_tokens_neox': 1707, 'n_words': 423}