๐Ÿ“ฆ jclapp23 / setfive_apply

๐Ÿ“„ User.class.php ยท 124 lines
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124<?php 

require_once "DB.class.php";

class User {		
	
	/**
	 * Array containing this user's db values
	 * @var array
	 */
	private $props;
	
	/**
	 * DB class passed in through the construtor
	 * @var DB
	 */
	private $db;
	
	public function __construct(DB $db){
		$this->db = $db;
		$this->props = array_fill_keys(array("id", "first_name", "last_name", "email"), null);    
	}
	
	/**
	 * Persists the user to the database. 
	 * If the $this->props["id"] field is set, it'll do an update otherwise an INSERT is performed.
	 * @return User 
	 */
	public function save(){		
    	if (isset($this->props["id"]))
    	{
    		$user = $this->db->updateRow('user', $this->props , array("id" => $this->props["id"]) );
    	}else{
    		$user = $this->db->insertRow('user', $this->props);
    	}
		return $this;
	}
	
	/**
	 * Sets the user's first name field
	 * @param string $val
	 * @return User
	 */
	public function setFirstName($val){
		$this->props["first_name"] = $val;
		return $this;
	}
	
	/**
	 * Returns the user's first name
	 * @return string
	 */
	public function getFirstName(){
    return $this->props['first_name'];
	}
	
	
  /**
   * Sets the user's first last field
   * @param string $val
   * @return User
   */
  public function setLastName($val){
    $this->props["last_name"] = $val;
    return $this;
  }
  
  /**
   * Returns the user's last name
   * @return string
   */
  public function getLastName(){
	return $this->props['last_name'];
  }
  
  /**
   * Sets the user's email field
   * @param string $val
   * @return User
   */
  public function setEmail($val){
    $this->props["email"] = $val;
    return $this;
  }
  
  /**
   * Returns the user's email
   * @return string
   */
  public function getEmail(){
    return $this->props["email"];
  }
  
  /**
   * Sets the user's id field
   * @param string $val
   * @return User
   */
  public function setId($val){
    $this->props["id"] = $val;
    return $this;
  }
  
  /**
   * Returns the user's id id
   * @return string
   */
  public function getId(){
    $row = $this->db->findById('user',$this->props['id']);
    echo $this->props['id'];
    return $row->id;
  }
  
	/**
	 * Only used internally. Sets the $this->prop array with values from the database.  
   * Use this when hydrating this object from a database row.
	 * @param array $properties
	 * @return User
	 */
	public function hydrate(array $properties){		
		$this->props=$properties;
	}
}