-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModel.php
101 lines (85 loc) · 2.85 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
<?php
/**
* Bread PHP Framework (http://github.com/saiv/Bread)
* Copyright 2010-2012, SAIV Development Team <[email protected]>
*
* Licensed under a Creative Commons Attribution 3.0 Unported License.
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2010-2012, SAIV Development Team <[email protected]>
* @link http://github.com/saiv/Bread Bread PHP Framework
* @package Bread
* @since Bread PHP Framework
* @license http://creativecommons.org/licenses/by/3.0/
*/
namespace Bread;
use Bread\Promise;
use Bread\Caching\Cache;
use Bread\Configuration;
use Bread\Model\Database;
use Exception;
use IteratorAggregate, ArrayIterator;
use JsonSerializable;
abstract class Model implements IteratorAggregate, JsonSerializable {
public function __construct($attributes = array()) {
foreach (array_intersect_key($attributes, get_object_vars($this)) as $attribute => $value) {
$this->__set($attribute, $value);
}
}
public function __set($attribute, $value) {
$this->validate($attribute, $value);
$this->$attribute = $value;
}
public function __get($attribute) {
return $this->$attribute;
}
public function getIterator() {
return new ArrayIterator(get_object_vars($this));
}
public function jsonSerialize() {
return get_object_vars($this);
}
public function validate($attribute, $value) {
}
public function store() {
$class = get_class($this);
return Database::driver($class)->store($this);
}
public function delete() {
$class = get_class($this);
return Database::driver($class)->delete($this);
}
public static function count($search = array(), $options = array()) {
return static::cached(__FUNCTION__, $search, $options);
}
public static function first($search = array(), $options = array()) {
return static::cached(__FUNCTION__, $search, $options);
}
public static function fetch($search = array(), $options = array()) {
return static::cached(__FUNCTION__, $search, $options);
}
public static function purge($search = array(), $options = array()) {
return static::cached(__FUNCTION__, $search, $options);
}
public static function get($key = null) {
$class = get_called_class();
return Configuration\Manager::get($class, $key);
}
protected static function cached($function, $search = array(),
$options = array()) {
$class = get_called_class();
$key = implode('::', array(
$class,
$function,
json_encode($search + $options)
));
return Cache::instance()->fetch($key)->then(null, function ($key) use (
$class, $function, $search, $options) {
return Database::driver($class)->$function($class, $search, $options)->then(function (
$result) use ($key) {
Cache::instance()->store($key, $result);
return $result;
});
});
}
}