|
1 | 1 | <?php
|
2 |
| - |
| 2 | + |
3 | 3 | namespace jsonDB;
|
4 |
| - |
| 4 | + |
5 | 5 | use jsonDB\dbException;
|
6 | 6 | use jsonDB\Relation;
|
7 | 7 | use jsonDB;
|
8 |
| - |
| 8 | + |
9 | 9 | /**
|
10 | 10 | * Core class of jsonDB.
|
11 | 11 | *
|
|
15 | 15 | *
|
16 | 16 | */
|
17 | 17 | class Database implements \IteratorAggregate, \Countable {
|
18 |
| - |
| 18 | + |
19 | 19 | /**
|
20 | 20 | * Contain returned data from file as object or array of objects
|
21 | 21 | * @var mixed Data from table
|
22 | 22 | */
|
23 | 23 | protected $data;
|
24 |
| - |
| 24 | + |
25 | 25 | /**
|
26 | 26 | * Name of file (table)
|
27 | 27 | * @var string Name of table
|
28 | 28 | */
|
29 | 29 | protected $name;
|
30 |
| - |
| 30 | + |
31 | 31 | /**
|
32 | 32 | * Object with setted data
|
33 | 33 | * @var object Setted data
|
34 | 34 | */
|
35 | 35 | protected $set;
|
36 |
| - |
| 36 | + |
37 | 37 | /**
|
38 | 38 | * ID of current row if setted
|
39 | 39 | * @var integer Current ID
|
40 | 40 | */
|
41 | 41 | protected $currentId;
|
42 |
| - |
| 42 | + |
43 | 43 | /**
|
44 | 44 | * Key if current row if setted
|
45 | 45 | * @var integer Current key
|
46 | 46 | */
|
47 | 47 | protected $currentKey;
|
48 |
| - |
| 48 | + |
49 | 49 | /**
|
50 | 50 | * Pending functions with values
|
51 | 51 | * @see \jsonDB\Database::setPending()
|
@@ -130,7 +130,7 @@ protected function clearKeyInfo()
|
130 | 130 | protected function setFields()
|
131 | 131 | {
|
132 | 132 | $this->set = new \stdClass();
|
133 |
| - $schema = $this->schema(); |
| 133 | + $schema = $this->schema(); |
134 | 134 |
|
135 | 135 | foreach ($schema as $field => $type)
|
136 | 136 | {
|
@@ -177,11 +177,22 @@ protected function clearQuery()
|
177 | 177 | */
|
178 | 178 | public function __set($name, $value)
|
179 | 179 | {
|
| 180 | + /* |
180 | 181 | if (Validate::table($this->name)->field($name) && Validate::table($this->name)->type($name, $value))
|
181 | 182 | {
|
182 | 183 | $this->set->{$name} = $value;
|
| 184 | + } |
| 185 | + */ |
| 186 | + try { |
| 187 | + Validate::table($this->name)->field($name); |
| 188 | + Validate::table($this->name)->type($name, $value); |
| 189 | + $this->set->{$name} = $value; |
| 190 | + } catch(dbException $error) { |
| 191 | + echo $error; |
183 | 192 | }
|
| 193 | + |
184 | 194 | }
|
| 195 | + |
185 | 196 |
|
186 | 197 | /**
|
187 | 198 | * Returning variable from Object
|
@@ -521,7 +532,7 @@ protected function wherePending()
|
521 | 532 | elseif (!is_array($value) && in_array($op, array('LIKE', 'like')))
|
522 | 533 | {
|
523 | 534 |
|
524 |
| - //$specials = '.\+*?[^]$(){}=!<>|:-'; |
| 535 | + //$specials = '.\+*?[^]$(){}=!<>|:-'; |
525 | 536 | $regex = "/^" . str_replace('%', '(.*?)', preg_quote($value)) . "$/si";
|
526 | 537 | $value = preg_match($regex, $row->{$field});
|
527 | 538 | $op = '==';
|
@@ -794,13 +805,13 @@ public function save()
|
794 | 805 | }
|
795 | 806 | else
|
796 | 807 | {
|
797 |
| - $this->set->id = $this->currentId; |
| 808 | + $this->set->id = $this->currentId; |
798 | 809 | $data[$this->currentKey] = $this->set;
|
799 | 810 | }
|
800 | 811 |
|
801 | 812 | Data::table($this->name)->put($data);
|
802 | 813 |
|
803 |
| -// $this->setFields(); |
| 814 | +// $this->setFields(); |
804 | 815 | }
|
805 | 816 |
|
806 | 817 | /**
|
@@ -856,8 +867,8 @@ public function find($id = NULL)
|
856 | 867 | {
|
857 | 868 | if ($id !== NULL)
|
858 | 869 | {
|
859 |
| - $data = $this->getData(); |
860 |
| - $this->currentId = $id; |
| 870 | + $data = $this->getData(); |
| 871 | + $this->currentId = $id; |
861 | 872 | $this->currentKey = $this->getRowKey($id);
|
862 | 873 | foreach ($data[$this->currentKey] as $field => $value)
|
863 | 874 | {
|
@@ -907,7 +918,7 @@ public function getIterator()
|
907 | 918 | */
|
908 | 919 | public function debug()
|
909 | 920 | {
|
910 |
| - $print = "jsonDB::table(" . $this->name . ")\n"; |
| 921 | + $print = "jsonDB::table(".$this->name.")\n"; |
911 | 922 | foreach ($this->pending as $function => $values)
|
912 | 923 | {
|
913 | 924 | if (!empty($values))
|
|
0 commit comments