<commit_before><?php

/**
 * 
 * Fierce Web Framework
 * https://github.com/abhibeckert/Fierce
 *
 * This is free and unencumbered software released into the public domain.
 * For more information, please refer to http://unlicense.org
 * 
 */

namespace Fierce;

class DBRow
{
  public $id;
  protected $row;
  
  public static function tableName()
  {
    $class = get_called_class();
    return preg_replace('/(.*)\\\\/', '', $class);
  }
  
  static public function all($sort=null)
  {
    $db = Env::get('db');
    $class = get_called_class();
    $entity = $class::tableName();
    
    $rows = $db->$entity->find([], $sort);
    
    $items = array();
    foreach ($rows as $id => $row) {
      $item = new $class();
      $item->id = $id;
      $item->setData($row);
      
      $items[] = $item;
    }
    
    return $items;
  }
  
  public static function find($params=[], $sort=null, $range=null)
  {
    $db = Env::get('db');
    $class = get_called_class();
    $entity = $class::tableName();
    
    $rows = $db->$entity->find($params, $sort, $range);
    
    $items = array();
    foreach ($rows as $id => $row) {
      $item = new $class();
      $item->id = $id;
      $item->setData($row);
      
      $items[] = $item;
    }
    
    return $items;
  }
  
  static public function createById($id)
  {
    $db = Env::get('db');
    $class = get_called_class();
    $entity = $class::tableName();
    
    $id = preg_replace('/[^a-zA-Z0-9-]/', '', $id);
    
    $row = $db->$entity->byId($id);
    
    $item = new $class();
    $item->id = $id;
    $item->setData($row);
    
    return $item;
  }
  
  static public function createNew()
  {
    $db = Env::get('db');
    $class = get_called_class();
    $entity = $class::tableName();
    
    $item = new $class();
    $item->setData($db->$entity->blankRow());
    $item->id = $db->id();
    
    return $item;
  }
  
  static public function createByFind($params)
  {
    $db = Env::get('db');
    $class = get_called_class();
    $entity = $class::tableName();
    
    $rows = self::find($params);
    if (count($rows) == 0) {
    	throw new \exception("Cannot find row matching params");
    }
    if (count($rows) > 1) {
    	throw new \exception("Found too many rows matching params");
    }
    
    return array_shift($rows);
  }
  
  public function __get($key)
  {
    switch ($key) {
      case 'id':
        return $this->id;
      case 'row':
        return $this->row;
    }
    
    return $this->row->$key;
  }
  
  public function __set($key, $value)
  {
    if (!property_exists($this->row, $key)) {
      throw new \exception("Cannot set $key on " . get_called_class());
    }
    
    $this->row->$key = $value;
  }
  
  public function __isset($key)
  {
    if (method_exists($this, $key)) {
      return true;
    }
    
    return property_exists($this->row, $key);
  }
  
  public function id()
  {
    return $this->id;
  }
  
  public function setData($data)
  {
    if (is_array($data)) {
      $data = (object)$data;
    }
    if (!is_object($data)) {
      $data = (object)[];
    }
    if (!$this->row) {
      $this->row = (object)[];
    }
    
    foreach ($data as $key => $value) {
      $this->row->$key = $value;
    }
  }
  
  public function save()
  {
    $db = Env::get('db');
    $class = get_called_class();
    $entity = $class::tableName();
    
    // misc fields
    $user = Auth::loggedInUser();
    if ($user) {
      $this->row->modifiedBy = $user->id;
    }
    $this->row->modified = new \DateTime();
    
    // save
    $db->$entity->archive($this->id);
    $db->$entity->write($this->id, $this->row, true);
    
  }
  
  public function archive()
  {
    $db = Env::get('db');
    $class = get_called_class();
    $entity = $class::tableName();
    
    $db->$entity->archive($this->id);
  }
  
  public function purge()<commit_msg>bugfix modifiedBy and createNew<commit_after><?php

/**
 * 
 * Fierce Web Framework
 * https://github.com/abhibeckert/Fierce
 *
 * This is free and unencumbered software released into the public domain.
 * For more information, please refer to http://unlicense.org
 * 
 */

namespace Fierce;

class DBRow
{
  public $id;
  protected $row;
  
  public static function tableName()
  {
    $class = get_called_class();
    return preg_replace('/(.*)\\\\/', '', $class);
  }
  
  static public function all($sort=null)
  {
    $db = Env::get('db');
    $class = get_called_class();
    $entity = $class::tableName();
    
    $rows = $db->$entity->find([], $sort);
    
    $items = array();
    foreach ($rows as $id => $row) {
      $item = new $class();
      $item->id = $id;
      $item->setData($row);
      
      $items[] = $item;
    }
    
    return $items;
  }
  
  public static function find($params=[], $sort=null, $range=null)
  {
    $db = Env::get('db');
    $class = get_called_class();
    $entity = $class::tableName();
    
    $rows = $db->$entity->find($params, $sort, $range);
    
    $items = array();
    foreach ($rows as $id => $row) {
      $item = new $class();
      $item->id = $id;
      $item->setData($row);
      
      $items[] = $item;
    }
    
    return $items;
  }
  
  static public function createById($id)
  {
    $db = Env::get('db');
    $class = get_called_class();
    $entity = $class::tableName();
    
    $id = preg_replace('/[^a-zA-Z0-9-]/', '', $id);
    
    $row = $db->$entity->byId($id);
    
    $item = new $class();
    $item->id = $id;
    $item->setData($row);
    
    return $item;
  }
  
  static public function createNew()
  {
    $db = Env::get('db');
    $class = get_called_class();
    $entity = $class::tableName();
    
    $row = $db->$entity->blankRow();
    $row->id = $db->id();
    
    $item = new $class();
    $item->setData($row);
    $item->id = $row->id;
    
    return $item;
  }
  
  static public function createByFind($params)
  {
    $db = Env::get('db');
    $class = get_called_class();
    $entity = $class::tableName();
    
    $rows = self::find($params);
    if (count($rows) == 0) {
    	throw new \exception("Cannot find row matching params");
    }
    if (count($rows) > 1) {
    	throw new \exception("Found too many rows matching params");
    }
    
    return array_shift($rows);
  }
  
  public function __get($key)
  {
    switch ($key) {
      case 'id':
        return $this->id;
      case 'row':
        return $this->row;
    }
    
    return $this->row->$key;
  }
  
  public function __set($key, $value)
  {
    if (!property_exists($this->row, $key)) {
      throw new \exception("Cannot set $key on " . get_called_class());
    }
    
    $this->row->$key = $value;
  }
  
  public function __isset($key)
  {
    if (method_exists($this, $key)) {
      return true;
    }
    
    return property_exists($this->row, $key);
  }
  
  public function id()
  {
    return $this->id;
  }
  
  public function setData($data)
  {
    if (is_array($data)) {
      $data = (object)$data;
    }
    if (!is_object($data)) {
      $data = (object)[];
    }
    if (!$this->row) {
      $this->row = (object)[];
    }
    
    foreach ($data as $key => $value) {
      $this->row->$key = $value;
    }
  }
  
  public function save()
  {
    $db = Env::get('db');
    $class = get_called_class();
    $entity = $class::tableName();
    
    // misc fields
    $user = Auth::loggedInUser();
    if ($user) {
      $this->row->modifiedBy = $user->id;
    } else {
      $this->row->modifiedBy = 'none';
    }
    $this->row->modified = new \DateTime();
    
    // save
    $db->$entity->archive($this->id);
    $db->$entity->write($this->id, $this->row, true);
    
  }
  
  public function archive()
  {
    $db = Env::get('db');
    $class = get_called_class();
    $entity = $class::tableName();
    
    $db->$entity->archive($this->id);
  }
  
  public function purge()
{'fraction_non_alphanumeric': 0.19347022063512306, 'fraction_numerical': 0.0010202780257620202, 'mean_word_length': 2.274321503131524, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 105, 'https://': 2, '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': '5957602', 'n_tokens_mistral': 2803, 'n_tokens_neox': 2786, 'n_words': 581}