<commit_before><?php

namespace Structr\Tree\Base;

use Structr\Tree\Base\ScalarNode;

use Structr\Exception;

abstract class NumberNode extends ScalarNode
{

    private $_hasGt = false;
    private $_hasGte = false;
    private $_hasLt = false;
    private $_hasLte = false;
    private $_compareGt;
    private $_compareGte;
    private $_compareLt;
    private $_compareLte;
    private $_clampGte = false;
    private $_clampLte = false;

    public function gt($value) {
        $this->_hasGt = true;
        $this->_compareGt = $value;

        return $this;
    }

    public function gte($value, $clamp = false) {
        $this->_hasGte = true;
        $this->_compareGte = $value;
        $this->_clampGte = $clamp;

        return $this;
    }

    public function lt($value) {
        $this->_hasLt = true;
        $this->_compareLt = $value;

        return $this;
    }

    public function lte($value, $clamp = false) {
        $this->_hasLte = true;
        $this->_compareLte = $value;
        $this->_clampLte = $clamp;

        return $this;
    }

    public function clamp($low, $high) {
        $this->gte($low, true);
        $this->lte($high, true);

        return $this;
    }

    public function _walk_value($value) {
        $value = parent::_walk_value($value);

        if ($this->_hasGte) {
            if($value < $this->_compareGte && !$this->_clampGte)
                throw new Exception(
                    "Value '{$value}' lower than"
                    . " allowed ({$this->_compareGte})");
            $value = max($this->_compareGte, $value);
        }
        if ($this->_hasLte) {
            if($value > $this->_compareLte && !$this->_clampLte)
                throw new Exception(
                    "Value '{$value}' higher"
                    . " than allowed ({$this->_compareLte})");
            $value = min($this->_compareLte, $value);
        }

        if ($this->_hasGt && $value <= $this->_compareGt)
            throw new Exception(
                "Value '{$value}' lower than allowed ({$this->_compareGt})");
        if ($this->_hasLt && $value >= $this->_compareLt)
            throw new Exception(
                "Value '{$value}' higher than allowed ({$this->_compareLt})");

        return $value;
    }

    public function setType(&$value) {
        if ($this->_coerceStrict && !is_numeric($value))
            return false;
        return parent::setType($value);
    }
}
<commit_msg>Added comments to numbernode, and refactored it<commit_after><?php

namespace Structr\Tree\Base;

use Structr\Tree\Base\ScalarNode;

use Structr\Exception;

/**
 * Base functionality for all scalar nodes that
 * are concerned with numbers
 */
abstract class NumberNode extends ScalarNode
{
    /**
     * @var mixed The value of this node is expected to be greater than
     *     this value
     */
    private $_gt = false;
    
    /**
     * @var mixed The value of this node is expected to be greater than or equal
     *     to this value
     */
    private $_gte = false;
    
    /**
     * @var mixed The value of this node is expected to be less than this value
     */
    private $_lt = false;
    
    /**
     * @var mixed The value of this node is expected to be less than or equal to
     *     this value
     */
    private $_lte = false;
    
    /**
     * @var bool Whether to clamp the value if it is greater than the value of
     *    _gte
     */
    private $_clampGte = false;
    
    /**
     * @var bool Whether to clamp the value if it is less than the value of
     *    _lte
     */
    private $_clampLte = false;

    /**
     * The value of this node is expected to be greater than a fixed value
     * 
     * @param type $value
     * @return \Structr\Tree\Base\NumberNode
     */
    public function gt($value)
    {
        $this->_gt = $value;

        return $this;
    }

    /**
     * The value of this node is expected to be greater than or equal to 
     *     a fixed value
     * 
     * @param type $value
     * @param type $clamp
     * @return \Structr\Tree\Base\NumberNode
     */
    public function gte($value, $clamp = false)
    {
        $this->_gte = $value;
        $this->_clampGte = $clamp;

        return $this;
    }

    /**
     * The value of this node is expected to be less than a fixed value
     * 
     * @param type $value
     * @return \Structr\Tree\Base\NumberNode
     */
    public function lt($value)
    {
        $this->_lt = $value;

        return $this;
    }

    /**
     * The value of this node is expected to be less than or equal to a
     *     fixed value
     * 
     * @param type $value
     * @param type $clamp
     * @return \Structr\Tree\Base\NumberNode
     */
    public function lte($value, $clamp = false)
    {
        $this->_lte = $value;
        $this->_clampLte = $clamp;

        return $this;
    }

    /**
     * The value of this node is expected to be between two fixed values. 
     * If it isn't, it will be forced within the interval.
     * 
     * @param type $low
     * @param type $high
     * @return \Structr\Tree\Base\NumberNode
     */
    public function clamp($low, $high)
    {
        if ($high <= $low) {
            throw new Exception(
                'Low value must be stricly lower than high value for clamping'
            );
        }
        $this->gte($low, true);
        $this->lte($high, true);

        return $this;
    }

    /**
     * {@inheritdoc}
     * @throws Structr\Exception
     */
    public function _walk_value($value)
    {
        $value = parent::_walk_value($value);

        $value = $this->checkGte($value);
        $value = $this->checkLte($value);
        $this->checkGt($value);
        $this->checkLt($value);

        return $value;
    }
    
    /**
     * Check that the value is within bounds (greater than or equal)
     * 
     * @param type $value The value to check
     * @throws Structr\Exception
     */
    protected function checkGte($value)
    {
        if ($this->_gte) {
            if ($value < $this->_gte && !$this->_clampGte) {
                $this->tooLow($value, $this->_gte);
            }
            $value = max($this->_gte, $value);
        }
        
        return $value;
    }
    
    /**
     * Check that the value is within bounds (less than or equal)
     * 
     * @param type $value The value to check
     * @throws Structr\Exception
     */
    protected function checkLte($value)
    {
        if ($this->_lte) {
            if ($value > $this->_lte && !$this->_clampLte) {
               $this->tooHigh($value, $this->_lte);
            }
            $value = min($this->_lte, $value);
        }
        
        return $value;
    }
    
    /**
     * Check that the value is within bounds (greater than)
     * 
     * @param type $value The value to check
     * @throws Structr\Exception
     */
    protected function checkGt($value)
    {
        if ($this->_gt && $value <= $this->_gt) {
            $this->tooLow($value, $this->_gt);
        }
    }
    
    /**
     * Check that the value is within bounds (less than)
     * 
     * @param type $value The value to check
     * @throws Structr\Exception
     */
    protected function checkLt($value)
    {
        if ($this->_lt && $value >= $this->_lt) {
            $this->tooHigh($value, $this->_lt);
        }
    }
    
    /**
     * Error: the value is too high
     * @throws Structr\Exception
     */
    protected function tooHigh($value, $max)
    {
         throw new Exception(sprintf(
            "Value '%s' is higher than allowed (%f)",
            $value,
            $max
        ));
    }
    
    /**
     * Error: the value is too low
     * @throws Structr\Exception
     */
    protected function tooLow($value, $min)
    {
        throw new Exception(sprintf(
            "Value '%s' is lower than allowed (%f)",
            $value,
            $min
        ));
    }

    /**
     * {@inheritdoc}
     */
    public function setType(&$value)
    {
        if ($this->_coerceStrict && !is_numeric($value)) {
            return false;
        }
        
        return parent::setType($value);
    }
}

{'fraction_non_alphanumeric': 0.12660865302120358, 'fraction_numerical': 0.0, 'mean_word_length': 1.7364185110663983, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 69, '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': '5378826', 'n_tokens_mistral': 2490, 'n_tokens_neox': 2439, 'n_words': 697}