-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.php
162 lines (146 loc) · 4.26 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
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<?php
abstract class Model extends \DB\SQL\Mapper
{
private
$candidate;
const
BCRYPT_COST = 10,
CANDIDATE_PREFIX = 'dbCandidate',
E_FIELD_MISMATCH = 'The %s does not match.',
E_FIELD_EMPTY = 'The %s cannot be empty.',
E_FIELD_INVALID = '%s is not valid.',
EX_FAILED= '%s failed.',
EX_NO_SAVE = 'This model cannot save records.';
public function __construct($table = null, $fields = NULL, $ttl = 60)
{
$class = get_called_class();
$isMySQL = preg_match('/mysql/i', $class);
if($table === null) {
$temp = explode('\\', $class);
$table = strtolower(end($temp));
}
$fw = $this->fw();
$db = (($isMySQL) ? $fw->get('MySQLDB') : $fw->get('SQLiteDB'));
parent::__construct($db, $table, $fields, $ttl);
}
/**
* You cannot override the save method. If you need to disable the save
* method on a model class, use the beforeinsert() trigger and throw an
* exception and return the EX_NO_SAVE constant.
*
* @param array $struct
* @return \DB\SQL\Mapper object
*/
final public function save(array $struct = null)
{
if($struct !== null) {
$this->candidate = $struct;
$this->reset();
$fw = $this->fw();
$pkey = $this->pkey();
if(isset($this->candidate[$pkey])) {
$id = $this->candidate[$pkey];
unset($this->candidate[$pkey]);
$this->load(array('`' . $pkey . '`=?', $id));
}
$candidate = self::CANDIDATE_PREFIX . $this->source;
$fw->set($candidate, $this->candidate);
$this->copyfrom($candidate);
$fw->clear($candidate);
}
return parent::save();
}
/**
* Returns the name of the primary key field in the database table from the
* schema stored in the mapper.
*
* @return string
*/
protected function pkey()
{
foreach((array)$this->fields as $field => $meta) {
if($meta['pkey'] !== true) {
continue;
}
return $field;
}
}
/**
* The candidate returns the raw $struct that was passed to the save()
* method. The candidate can be used in the mapper event triggers for
* comparison, and rules based on the individual model.
*
* @return null|array
*/
protected function candidate()
{
return $this->candidate;
}
protected function isEmail($email)
{
$audit = $this->audit();
return $audit->email($email, false);
}
/**
* Provides access to the Bcrypt class to hash a string.
*
* @param string $pw
* @param string $salt
* @param integer $cost
* @return string
*/
protected function hash($pw, $salt = null, $cost = self::BCRYPT_COST)
{
$crypt = $this->crypt();
return $crypt->hash($pw, $salt, $cost);
}
/**
* Provides access to the Bcrypt class to verify a hash.
*
* @param string $pw
* @param string $hash
* @return bool
*/
protected function verify($pw, $hash)
{
$crypt = $this->crypt();
return $crypt->verify($pw, $hash);
}
/**
*
* @param string $hash
* @param integer $cost
* @return bool
*/
protected function needsRehash($hash, $cost = self::BCRYPT_COST)
{
$crypt = $this->crypt();
return $crypt->needs_rehash($hash, $cost);
}
/**
* Provides access to the Fatfree framework. Data relevant to the model
* should be passed to the save() method, and is stored in the private
* $candidate member. You can access the $candidate member through the
* protected candidate() method.
*
* @return type
*/
protected function fw()
{
return \Base::instance();
}
/**
* This method should not be called directly. Models that need to hash or
* verify hashes should use the aliased methods hash() and verify().
*
* @return object
*/
private function crypt()
{
return \Bcrypt::instance();
}
private function audit()
{
return \Audit::instance();
}
}