<commit_before><?php
/**
 * PHPFrame/Mapper/PersistentObject.php
 * 
 * PHP version 5
 * 
 * @category  PHPFrame
 * @package   Mapper
 * @author    <NAME> <<EMAIL>>
 * @copyright 2009 E-noise.com Limited
 * @license   http://www.opensource.org/licenses/bsd-license.php New BSD License
 * @version   SVN: $Id$
 * @link      http://code.google.com/p/phpframe/source/browse/#svn/PHPFrame
 */

/**
 * The Persistent Object class is an abstract class that needs to be extended by 
 * objects that you want to use with the Mapper package.
 * 
 * To see an example of how to extend this class have a look at 
 * {@link PHPFrame_User}.
 * 
 * Persistent Objects implement the IteratorAggregate interface, which means 
 * that they can be iterated using the foreach construct and easily converted to 
 * an array by using PHPs iterator_to_array() function.
 * 
 * For example:
 * 
 * <code>
 * // Create a new user object (this object extends Persistent Object)
 * $user = new PHPFrame_User(array("username"=>"lupo"));
 * // Print the user oject as an array
 * print_r(iterator_to_array($user));
 * </code>
 * 
 * This will produce the following output:
 * 
 * <pre>
 * Array
 * (
 *  [groupid] => 0
 *  [username] => lupo
 *  [password] =>
 *  [firstname] =>
 *  [lastname] => 
 *  [email] => 
 *  [photo] =>
 *  [notifications] => 1
 *  [show_email] => 1
 *  [block] =>
 *  [last_visit] =>
 *  [activation] =>
 *  [params] => a:0:{}
 *  [deleted] => 
 *  [openid_urls] => a:0:{}
 *  [id] =>
 *  [created] =>
 *  [modified] => 
 *  )
 * <pre>
 * 
 * @category PHPFrame
 * @package  Mapper
 * @author   <NAME> <<EMAIL>>
 * @license  http://www.opensource.org/licenses/bsd-license.php New BSD License
 * @link     http://code.google.com/p/phpframe/source/browse/#svn/PHPFrame
 * @uses     IteratorAggregate
 * @since    1.0
 */
abstract class PHPFrame_PersistentObject extends PHPFrame_Object
    implements IteratorAggregate
{
    /**
     * Internal array to store fields data
     * 
     * @var array
     */
    protected $fields = array();
    /**
     * An validator object used to validate fields
     *  
     * @var PHPFrame_Validator
     */
    private $_validator;
    /**
     * Serialised string respresenting clean state. This is used to check if the
     * current state is "dirty" if it has changed since last marked clean.
     * 
     * @var string
     */
    private $_clean_state;
    
    /**
     * Constructor
     * 
     * @param array $options
     * 
     * @access public
     * @return void
     * @since  1.0
     */
    public function __construct(array $options=null)
    {
        // Add the base fields
        $this->addField("id",    null,   true,  new PHPFrame_IntFilter());
        $this->addField("atime", null,   true,  new PHPFrame_IntFilter());
        $this->addField("ctime", time(), true,  new PHPFrame_IntFilter());
        $this->addField("mtime", time(), true,  new PHPFrame_IntFilter());
        $this->addField("owner", 1,      false, new PHPFrame_IntFilter());
        $this->addField("group", 1,      false, new PHPFrame_IntFilter());
        $this->addField("perms", 664,    false, new PHPFrame_IntFilter());
        
        // Set object ownership to current user if applicable
        if (PHPFrame::getRunLevel() > 1 && PHPFrame::Session()->isAuth()) {
            $this->setOwner(PHPFrame::Session()->getUserId());
            $this->setGroup(PHPFrame::Session()->getGroupId());
            $this->setPerms(664);
        }
        
        // Process options argument if passed
        if (!is_null($options)) {
            $this->bind($options);
        }
        
        $this->markClean();
    }
    
    /**
     * Magic method to handle the cloning of Persistent Objects
     * 
     * @access public
     * @return void
     * @since  1.0
     */
    public function __clone()
    {
        $this->fields["id"]    = null;
        $this->fields["atime"] = null;
        $this->fields["ctime"] = null;
        $this->fields["mtime"] = null;
    }
    
    protected function addField(
        $name, 
        $def_value=null, 
        $allow_null=true, 
        PHPFrame_Filter $filter=null
    )
    {
        if (!is_string($name) || strlen($name) < 1) {
            $msg  = get_class($this)."::addField() expects argument ";
            $msg .= "\$name to be of type string and not empty and got value ";
            $msg .= "'".$name."' of type ".gettype($name);
            throw new InvalidArgumentException($msg);
        }
        
        // Set key with default value in internal array
        $this->fields[$name] = $def_value;
        // Store filter in validator
        $this->_getValidator()->setFilter($name, $filter, $allow_null);
    }
    
    /**
     * Validate value for a given field using validator
     * 
     * @param string $field_name
     * @param mixed  $value
     * 
     * @access protected
     * @return void
     * @since  1.0
     */
    protected function validate($field_name, $value)<commit_msg>PersistentObject updated to use StringFilter as default filter for fields and also added a couple of methods to access some of the validator data stored internally.<commit_after><?php
/**
 * PHPFrame/Mapper/PersistentObject.php
 * 
 * PHP version 5
 * 
 * @category  PHPFrame
 * @package   Mapper
 * @author    <NAME> <<EMAIL>>
 * @copyright 2009 E-noise.com Limited
 * @license   http://www.opensource.org/licenses/bsd-license.php New BSD License
 * @version   SVN: $Id$
 * @link      http://code.google.com/p/phpframe/source/browse/#svn/PHPFrame
 */

/**
 * The Persistent Object class is an abstract class that needs to be extended by 
 * objects that you want to use with the Mapper package.
 * 
 * To see an example of how to extend this class have a look at 
 * {@link PHPFrame_User}.
 * 
 * Persistent Objects implement the IteratorAggregate interface, which means 
 * that they can be iterated using the foreach construct and easily converted to 
 * an array by using PHPs iterator_to_array() function.
 * 
 * For example:
 * 
 * <code>
 * // Create a new user object (this object extends Persistent Object)
 * $user = new PHPFrame_User(array("username"=>"lupo"));
 * // Print the user oject as an array
 * print_r(iterator_to_array($user));
 * </code>
 * 
 * This will produce the following output:
 * 
 * <pre>
 * Array
 * (
 *  [groupid] => 0
 *  [username] => lupo
 *  [password] =>
 *  [firstname] =>
 *  [lastname] => 
 *  [email] => 
 *  [photo] =>
 *  [notifications] => 1
 *  [show_email] => 1
 *  [block] =>
 *  [last_visit] =>
 *  [activation] =>
 *  [params] => a:0:{}
 *  [deleted] => 
 *  [openid_urls] => a:0:{}
 *  [id] =>
 *  [created] =>
 *  [modified] => 
 *  )
 * <pre>
 * 
 * @category PHPFrame
 * @package  Mapper
 * @author   <NAME> <<EMAIL>>
 * @license  http://www.opensource.org/licenses/bsd-license.php New BSD License
 * @link     http://code.google.com/p/phpframe/source/browse/#svn/PHPFrame
 * @uses     IteratorAggregate
 * @since    1.0
 */
abstract class PHPFrame_PersistentObject extends PHPFrame_Object
    implements IteratorAggregate
{
    /**
     * Internal array to store fields data
     * 
     * @var array
     */
    protected $fields = array();
    /**
     * An validator object used to validate fields
     *  
     * @var PHPFrame_Validator
     */
    private $_validator;
    /**
     * Serialised string respresenting clean state. This is used to check if the
     * current state is "dirty" if it has changed since last marked clean.
     * 
     * @var string
     */
    private $_clean_state;
    
    /**
     * Constructor
     * 
     * @param array $options
     * 
     * @access public
     * @return void
     * @since  1.0
     */
    public function __construct(array $options=null)
    {
        // Add the base fields
        $this->addField("id",    null,   true,  new PHPFrame_IntFilter());
        $this->addField("atime", null,   true,  new PHPFrame_IntFilter());
        $this->addField("ctime", time(), true,  new PHPFrame_IntFilter());
        $this->addField("mtime", time(), true,  new PHPFrame_IntFilter());
        $this->addField("owner", 1,      false, new PHPFrame_IntFilter());
        $this->addField("group", 1,      false, new PHPFrame_IntFilter());
        $this->addField("perms", 664,    false, new PHPFrame_IntFilter());
        
        // Set object ownership to current user if applicable
        if (PHPFrame::getRunLevel() > 1 && PHPFrame::Session()->isAuth()) {
            $this->setOwner(PHPFrame::Session()->getUserId());
            $this->setGroup(PHPFrame::Session()->getGroupId());
            $this->setPerms(664);
        }
        
        // Process options argument if passed
        if (!is_null($options)) {
            $this->bind($options);
        }
        
        $this->markClean();
    }
    
    /**
     * Magic method to handle the cloning of Persistent Objects
     * 
     * @access public
     * @return void
     * @since  1.0
     */
    public function __clone()
    {
        $this->fields["id"]    = null;
        $this->fields["atime"] = null;
        $this->fields["ctime"] = null;
        $this->fields["mtime"] = null;
    }
    
    /**
     * Add a field in persistent object (stored in internal array)
     * 
     * @param string          $name
     * @param mixed           $def_value
     * @param bool            $allow_null
     * @param PHPFrame_Filter $filter
     * 
     * @access public
     * @return void
     * @since  1.0
     */
    protected function addField(
        $name, 
        $def_value=null, 
        $allow_null=true, 
        PHPFrame_Filter $filter=null
    )
    {
        if (!is_string($name) || strlen($name) < 1) {
            $msg  = get_class($this)."::addField() expects argument ";
            $msg .= "\$name to be of type string and not empty and got value ";
            $msg .= "'".$name."' of type ".gettype($name);
            throw new InvalidArgumentException($msg);
        }
        
        if (is_null($filter)) {
            $filter = new PHPFrame_StringFilter();
        }
        
        // Set key with default value in internal array
        $this->fields[$name] = $def_value;
        // Store filter in validator
        $this->_getValidator()->setFilter($name, $filter, $allow_null);
    }
    
    /**
     * Get filters
     * 
     * @access public
     * @return array
     * @since  1.0
     */
    public function getFilters()
    {
        return $this->_getValidator()->getFilters();
    }
    
    /**
     * Check whether a given field allows null value
     * 
     * @access public
     * @return bool
     * @since  1.0
     */
    public function allowsNull($field_name)
    {
        return $this->_getValidator()->allowsNull($field_name);
    }
    
    /**
     * Validate value for a given field using validator
     * 
     * @param string $field_name
     * @param mixed  $value
     * 
     * @access protected
     * @return void
     * @since  1.0
     */
    protected function validate($field_name, $value)
{'fraction_non_alphanumeric': 0.1362605461308174, 'fraction_numerical': 0.005624603102603647, 'mean_word_length': 2.239494563620335, 'pattern_counts': {'":': 2, '<': 27, '<?xml version=': 0, '>': 111, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, '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': '4879252', 'n_tokens_mistral': 3417, 'n_tokens_neox': 3315, 'n_words': 905}