fork download
  1. class ActivityTest extends PHPUnit_Framework_TestCase
  2. {
  3.  
  4. public function testNewPost()
  5. {
  6. $userId = 60;
  7. $title = "Lorem Ipsum";
  8. $contents = "Lorem ipsum dolor sit amet";
  9. $date = 1356173771;
  10.  
  11. $a = new Activity($userId, $title, $contents, $date);
  12.  
  13. $this->assertEquals($userId, $a->getUserId());
  14. $this->assertEquals($title, $a->getTitle());
  15. $this->assertEquals($contents, $a->getContents());
  16. $this->assertEquals($date, $a->getDate());
  17. }
  18. }
  19.  
  20. Class Activity extends Article
  21. {
  22. /**
  23.   * Post a new activity
  24.   */
  25. public function newPost($userId, $title, $contents, $date)
  26. {
  27. $newInfo = array();
  28. $newInfo['userId'] = $userId;
  29. $newInfo['title'] = $title;
  30. $newInfo['contents'] = $contents;
  31. $newInfo['date'] = $date;
  32.  
  33. $this->fromArray($newInfo);
  34. return $this;
  35. }
  36. }
  37.  
  38. Class Article extends DBObject
  39. {
  40.  
  41. function __construct($source = null)
  42. {
  43. echo "Article __construct called\n";
  44. $args = func_get_args();
  45. if (count($args) > 1) {
  46. call_user_func_array(array($this, 'newPost'), $args);
  47. } else {
  48. parent::__construct($args);
  49. }
  50. }
  51. }
  52.  
  53. /**
  54.  * The class that manages all interfacing between models and the
  55.  * database. By extending this, all models can be created easily
  56.  * using their id or a sql string.
  57.  * All getters and setters are generated automatically.
  58.  */
  59. Class DBObject
  60. {
  61. public $info;
  62.  
  63. /**
  64.   * Construct a new database model object. You can pass either a key-value array
  65.   * or an sql query to the constructor.
  66.   * @param Array Key-Value based array containing all info
  67.   * @param Sql query where all values are replaced by %s %i etc., and provided
  68.   * as extra arguments.
  69.   */
  70. function __construct($source = null)
  71. {
  72. echo "DBObject constructed called\n";
  73. if (is_array($source))
  74. $this->fromArray($source);
  75. else if ($source != null) {
  76. $args = func_get_args();
  77. $this->fromSql($args);
  78. }
  79.  
  80. if (!isset($this->info))
  81. $this->info = Array();
  82.  
  83. $this->changed = false;
  84. $this->changeSet = Array();
  85. }
  86. /**
  87.   * Create an object from an sql query of the form:
  88.   * "select [] from [table] where name=%s and id=%i', 'Steenman', 60"
  89.   * Values are automatically escaped and sanitized.
  90.   */
  91. function fromSql()
  92. {
  93. $args = func_get_args();
  94. $arr = call_user_func_array(array('DB', 'queryFirstRow'), $args[0]);
  95. $this->fromArray($arr);
  96. }
  97.  
  98. /**
  99.   * Create the object from a key-value array, e.g. if you want to test
  100.   * things. Called by fromSql after querying.
  101.   */
  102. function fromArray($array)
  103. {
  104. $this->info = $array;
  105. }
  106.  
  107. /**
  108.   * Write the object to the database. Only writes if changed() is true.
  109.   */
  110. function write($table = null, $where = null)
  111. {
  112. if (!$this->changed)
  113. return;
  114.  
  115. if (!isset($this->info->id)) { // Completely new object
  116. DB::insert($table ? $table : $this->table, $this->info);
  117. $this->setId(DB::insertId());
  118. }
  119.  
  120. var_dump($this->changeSet);
  121.  
  122. DB::update($table ? $table : $this->table, $this->changeSet, "id=%i", $this->getId());
  123. }
  124.  
  125. /**
  126.   * * Dynamic getters and setters than maintain getX and setX formati. They can be overwritten
  127.   * * if custom processing is needed. Setters also mark the DBObject as changed, enabling writing
  128.   * * it.
  129.  
  130.   * * if custom processing is needed. Setters also mark the DBObject as changed, enabling writing
  131.   * * it.
  132.   * *
  133.   * * @param string $method
  134.   * * @param array $arguments
  135.   * * @return mixed
  136.   * */
  137. function __call($method, $arguments) {
  138. #Is this a get or a set
  139. $prefix = strtolower(substr($method, 0, 3));
  140.  
  141. #What is the get/set class attribute
  142. if (!($prefix == "get" || $prefix == "set")) {
  143. $prefix = "get";
  144. $property = strtolower(preg_replace('/([A-Z])/', '_$1', lcfirst($method)));
  145. } else {
  146. $property = preg_replace('/([A-Z])/', '_$1', lcfirst(substr($method, 3)));
  147. $property = strtolower($property);
  148. }
  149. if (empty($property) || empty($prefix)) { #Did not match a get/set call
  150. throw New Exception("Calling a get/set without a method: $method");
  151. }
  152.  
  153. echo "Calling $prefix $property\n";
  154. var_dump($this->info);
  155. echo "\n";
  156.  
  157. #Check if the get/set paramter exists within this class as an attribute
  158. $match=false;
  159. foreach($this->info as $class_var=>$class_var_value){
  160. echo "Comparing ".strtolower($class_var)." and ".strtolower($property)."\n";
  161. if(strtolower($class_var) == strtolower($property)){
  162. $property = $class_var;
  163. $match=true;
  164. }
  165. }
  166.  
  167. #Get attribute
  168. if ($match && $prefix == "get" && (isset($this->info[$property]) || is_null($this->info[$property]))) {
  169. return $this->info[$property];
  170. } #Set
  171. else if ($prefix == "set") {
  172. $this->info[$property] = $arguments[0];
  173. } else {
  174. throw new Exception("Calling a get/set method that does not exist: $property");
  175. }
  176. return null;
  177. }
  178. }
Success #stdin #stdout 0.02s 13064KB
stdin
new Activity(60, 'Lorem Ipsum', 'Lorem Ipsum Dolor Sit Amet', 1356173771);
stdout
class ActivityTest extends PHPUnit_Framework_TestCase
{

    public function testNewPost()
    {
        $userId = 60;
        $title = "Lorem Ipsum";
        $contents = "Lorem ipsum dolor sit amet";
        $date = 1356173771;

        $a = new Activity($userId, $title, $contents, $date);

        $this->assertEquals($userId, $a->getUserId());
        $this->assertEquals($title, $a->getTitle());
        $this->assertEquals($contents, $a->getContents());
        $this->assertEquals($date, $a->getDate());
    }
}

Class Activity extends Article
{
    /**
     * Post a new activity
     */
    public function newPost($userId, $title, $contents, $date)
    {
        $newInfo = array();
        $newInfo['userId'] = $userId;
        $newInfo['title'] = $title;
        $newInfo['contents'] = $contents;
        $newInfo['date'] = $date;

        $this->fromArray($newInfo);
        return $this;
    }
}
 
Class Article extends DBObject
{

    function __construct($source = null)
    {   
        echo "Article __construct called\n";
        $args = func_get_args();
        if (count($args) > 1) {
            call_user_func_array(array($this, 'newPost'), $args);
        } else {
            parent::__construct($args);
        }   
    }  
}

/**
 * The class that manages all interfacing between models and the
 * database. By extending this, all models can be created easily 
 * using their id or a sql string.
 * All getters and setters are generated automatically.
 */
Class DBObject
{
    public $info;

    /**
     * Construct a new database model object. You can pass either a key-value array
     * or an sql query to the constructor.
     * @param Array Key-Value based array containing all info
     * @param Sql query where all values are replaced by %s %i etc., and provided
     * as extra arguments.
     */
    function __construct($source = null)
    {
        echo "DBObject constructed called\n";
        if (is_array($source))
            $this->fromArray($source);
        else if ($source != null) {
            $args = func_get_args();
            $this->fromSql($args);
        }

        if (!isset($this->info))
            $this->info = Array();

        $this->changed = false;
        $this->changeSet = Array();
    }
 /**
     * Create an object from an sql query of the form:
     *  "select [] from [table] where name=%s and id=%i', 'Steenman', 60"
     * Values are automatically escaped and sanitized.
     */
    function fromSql()
    {
        $args = func_get_args();
        $arr = call_user_func_array(array('DB', 'queryFirstRow'), $args[0]);
        $this->fromArray($arr);
    }

    /**
     * Create the object from a key-value array, e.g. if you want to test
     * things. Called by fromSql after querying.
     */
    function fromArray($array)
    {
        $this->info = $array;
    }

    /**
     * Write the object to the database. Only writes if changed() is true.
     */
    function write($table = null, $where = null)
    {
        if (!$this->changed)
            return;

        if (!isset($this->info->id)) { // Completely new object
            DB::insert($table ? $table : $this->table, $this->info);
            $this->setId(DB::insertId());
        }

        var_dump($this->changeSet);

        DB::update($table ? $table : $this->table, $this->changeSet, "id=%i", $this->getId());
    }

    /**
     * * Dynamic getters and setters than maintain getX and setX formati. They can be overwritten
     * * if custom processing is needed. Setters also mark the DBObject as changed, enabling writing
     * * it.

     * * if custom processing is needed. Setters also mark the DBObject as changed, enabling writing
     * * it.
     * *
     * * @param string $method
     * * @param array $arguments
     * * @return mixed
     * */
    function __call($method, $arguments) {
        #Is this a get or a set
        $prefix = strtolower(substr($method, 0, 3));

        #What is the get/set class attribute
        if (!($prefix == "get" || $prefix == "set")) {
            $prefix = "get";
            $property = strtolower(preg_replace('/([A-Z])/', '_$1', lcfirst($method)));
        } else {
            $property = preg_replace('/([A-Z])/', '_$1', lcfirst(substr($method, 3)));
            $property = strtolower($property);
        }
        if (empty($property) || empty($prefix)) { #Did not match a get/set call
            throw New Exception("Calling a get/set without a method: $method");
        }

        echo "Calling $prefix $property\n";
        var_dump($this->info);
        echo "\n";

        #Check if the get/set paramter exists within this class as an attribute
        $match=false;
        foreach($this->info as $class_var=>$class_var_value){
            echo "Comparing ".strtolower($class_var)." and ".strtolower($property)."\n";
            if(strtolower($class_var) == strtolower($property)){
                $property = $class_var;
                $match=true;
            }
        }

        #Get attribute
        if ($match && $prefix == "get" && (isset($this->info[$property]) || is_null($this->info[$property]))) {
            return $this->info[$property];
        } #Set
        else if ($prefix == "set") {
            $this->info[$property] = $arguments[0];
        } else {
            throw new Exception("Calling a get/set method that does not exist: $property");
        }
        return null;
    }
}