/
model.php
69 lines (59 loc) · 1.7 KB
/
model.php
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
<?php
abstract class Model {
protected abstract function mapper();
protected $fields = array();
protected $subclass_fields = array();
function __call( $method, $args ) {
//provide accessors for database fields
if ( $this->is_a_field( $method ) ) {
if ( isset( $args[0] ) ) {
$this->fields[$method] = $args[0];
}
return $this->fields[$method];
}
}
private function is_a_field( $string ) {
$fields = $this->mapper()->table_fields();
$fields[] = 'id';
$fields = array_merge( $fields, $this->subclass_fields );
return in_array( $string, $fields );
}
private function check_fields_are_valid( $array ) {
foreach ( $array as $field => $value ) {
if ( ! $this->is_a_field( $field ) ) {
return false;
}
}
return true;
}
protected function add_field( $string ) {
$this->subclass_fields[] = $string;
}
function set_fields( $array ) {
if ( ! $this->check_fields_are_valid( $array ) ) {
throw new Exception( 'Invalid field in Model::set_fields()' );
}
$this->fields = $array;
}
function to_array() {
return $this->fields;
}
function to_string() {
$string = get_class( $this ) . "\n";
foreach ( $this->fields as $field => $value ) {
$string .= "$field : $value \n";
}
return $string;
}
function save( ) {
$this->mapper()->save( $this );
}
function save_fields( $fields ) {
foreach ( $fields as $key => $value ) {
if ( $this->is_a_field( $key ) && $key != 'id' ) {
$this->fields[$key] = $value;
}
}
$this->save();
}
}