<commit_before><?php
/**
 * @package		Koowa_Model
 * @subpackage  State
 * @copyright	Copyright (C) 2007 - 2012 <NAME>. All rights reserved.
 * @license		GNU GPLv3 <http://www.gnu.org/licenses/gpl.html>
 * @link     	http://www.nooku.org
 */

namespace Nooku\Library;

/**
 * Model State Class
 *
 * A state requires a model object. It will call back to the model upon a state change using the onStateChange notifier.
 *
 * State values can only be of type scalar or array. Values are only filtered if not NULL. If the value is an empty
 * string it will be filtered to NULL. Values will only be set if the state exists. To insert new states use the
 * the insert() function.
 *
 * @author		<NAME> <<EMAIL>>
 * @package     Koowa_Model
 * @subpackage  State
 */
class ModelState extends ObjectArray implements ModelStateInterface
{
    /**
     * Model object
     *
     * @var string|object
     */
    protected $_model;

    /**
     * Constructor
     *
     * @param ObjectConfig $config  An optional ObjectConfig object with configuration options
     * @return ObjectArray
     */
    public function __construct(ObjectConfig $config)
    {
        parent::__construct($config);

        if (empty($config->model))
        {
            throw new \InvalidArgumentException(
                'model [ModelInterface] config option is required'
            );
        }

        if(!$config->model instanceof ModelInterface)
        {
            throw new \UnexpectedValueException(
                'Model: '.get_class($config->model).' does not implement ModelInterface'
            );
        }

        $this->_model = $config->model;
    }

    /**
     * Initializes the options for the object
     *
     * Called from {@link __construct()} as a first step of object instantiation.
     *
     * @param   ObjectConfig $object An optional ObjectConfig object with configuration options
     * @return  void
     */
    protected function _initialize(ObjectConfig $config)
    {
        $config->append(array(
            'model' => null,
        ));

        parent::_initialize($config);
    }

    /**
     * Insert a new state
     *
     * @param   string   $name     The name of the state
     * @param   mixed    $filter   Filter(s), can be a FilterInterface object, a filter name or an array of filter names
     * @param   mixed    $default  The default value of the state
     * @param   boolean  $unique   TRUE if the state uniquely identifies an entity, FALSE otherwise. Default FALSE.
     * @param   array    $required Array of required states to determine if the state is unique. Only applicable if the
     *                             state is unqiue.
     * @return  ModelState
     */
    public function insert($name, $filter, $default = null, $unique = false, $required = array())
    {
        //Create the state
        $state = new \stdClass();
        $state->name     = $name;
        $state->filter   = $filter;
        $state->value    = null;
        $state->unique   = $unique;
        $state->required = $required;
        $state->default  = $default;
        $this->_data[$name] = $state;

        //Set the value to default
        if(isset($default)) {
            $this->offsetSet($name, $default);
        }

        return $this;
    }

    /**
     * Retrieve a configuration item and return $default if there is no element set.
     *
     * @param string $name      The state name
     * @param mixed  $default   The state default value if no state can be found
     * @return mixed
     */
    public function get($name, $default = null)
    {
        $result = $default;
        if($this->offsetExists($name)) {
            $result = $this->offsetGet($name);
        }

        return $result;
    }

    /**
     * Set the a state value
     *
     * This function only acts on existing states, if a state has changed it will call back to the model triggering the
     * onStateChange notifier.
     *
     * @param  	string 	$name  The state name.
     * @param  	mixed  	$value The state value.
     * @return  ModelAbstract
     */
    public function set($name, $value = null)
    {
        $this->offsetSet($name, $value);

        if ($this->has($name) && $this->get($name) != $value) {
            $this->_model->onStateChange($name);
        }

        return $this;
    }<commit_msg>re #374 : Makes sure onStateChange is actually called.<commit_after><?php
/**
 * @package		Koowa_Model
 * @subpackage  State
 * @copyright	Copyright (C) 2007 - 2012 <NAME>. All rights reserved.
 * @license		GNU GPLv3 <http://www.gnu.org/licenses/gpl.html>
 * @link     	http://www.nooku.org
 */

namespace Nooku\Library;

/**
 * Model State Class
 *
 * A state requires a model object. It will call back to the model upon a state change using the onStateChange notifier.
 *
 * State values can only be of type scalar or array. Values are only filtered if not NULL. If the value is an empty
 * string it will be filtered to NULL. Values will only be set if the state exists. To insert new states use the
 * the insert() function.
 *
 * @author		<NAME> <<EMAIL>>
 * @package     Koowa_Model
 * @subpackage  State
 */
class ModelState extends ObjectArray implements ModelStateInterface
{
    /**
     * Model object
     *
     * @var string|object
     */
    protected $_model;

    /**
     * Constructor
     *
     * @param ObjectConfig $config  An optional ObjectConfig object with configuration options
     * @return ObjectArray
     */
    public function __construct(ObjectConfig $config)
    {
        parent::__construct($config);

        if (empty($config->model))
        {
            throw new \InvalidArgumentException(
                'model [ModelInterface] config option is required'
            );
        }

        if(!$config->model instanceof ModelInterface)
        {
            throw new \UnexpectedValueException(
                'Model: '.get_class($config->model).' does not implement ModelInterface'
            );
        }

        $this->_model = $config->model;
    }

    /**
     * Initializes the options for the object
     *
     * Called from {@link __construct()} as a first step of object instantiation.
     *
     * @param   ObjectConfig $object An optional ObjectConfig object with configuration options
     * @return  void
     */
    protected function _initialize(ObjectConfig $config)
    {
        $config->append(array(
            'model' => null,
        ));

        parent::_initialize($config);
    }

    /**
     * Insert a new state
     *
     * @param   string   $name     The name of the state
     * @param   mixed    $filter   Filter(s), can be a FilterInterface object, a filter name or an array of filter names
     * @param   mixed    $default  The default value of the state
     * @param   boolean  $unique   TRUE if the state uniquely identifies an entity, FALSE otherwise. Default FALSE.
     * @param   array    $required Array of required states to determine if the state is unique. Only applicable if the
     *                             state is unqiue.
     * @return  ModelState
     */
    public function insert($name, $filter, $default = null, $unique = false, $required = array())
    {
        //Create the state
        $state = new \stdClass();
        $state->name     = $name;
        $state->filter   = $filter;
        $state->value    = null;
        $state->unique   = $unique;
        $state->required = $required;
        $state->default  = $default;
        $this->_data[$name] = $state;

        //Set the value to default
        if(isset($default)) {
            $this->offsetSet($name, $default);
        }

        return $this;
    }

    /**
     * Retrieve a configuration item and return $default if there is no element set.
     *
     * @param string $name      The state name
     * @param mixed  $default   The state default value if no state can be found
     * @return mixed
     */
    public function get($name, $default = null)
    {
        $result = $default;
        if($this->offsetExists($name)) {
            $result = $this->offsetGet($name);
        }

        return $result;
    }

    /**
     * Set the a state value
     *
     * This function only acts on existing states, if a state has changed it will call back to the model triggering the
     * onStateChange notifier.
     *
     * @param  	string 	$name  The state name.
     * @param  	mixed  	$value The state value.
     * @return  ModelAbstract
     */
    public function set($name, $value = null)
    {
        if ($this->has($name) && $this->get($name) != $value)
        {
            $this->offsetSet($name, $value);

            //Notify the model
            $this->_model->onStateChange($name);
        }

        return $this;
    }
{'fraction_non_alphanumeric': 0.10360155003419193, 'fraction_numerical': 0.002393435149304764, 'mean_word_length': 2.0542986425339365, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 57, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2619870', 'n_tokens_mistral': 2431, 'n_tokens_neox': 2360, 'n_words': 834}