├── .gitignore ├── LICENSE ├── LICENSE.txt ├── README.md ├── composer.json ├── composer.lock ├── data └── test.json ├── src ├── Components │ ├── ArrayAccess.php │ ├── Data.php │ ├── Different.php │ ├── Dump.php │ ├── ParentClass.php │ ├── Status.php │ └── Tool.php ├── Data.php ├── Drivers │ └── LocalDriver.php ├── Exceptions │ ├── Base.php │ ├── CoreException.php │ ├── DataException.php │ └── DriverException.php ├── Interfaces │ ├── Driver.php │ └── Sorter.php ├── NonDB.php ├── Sorter │ ├── DefaultSort.php │ ├── NaturalSort.php │ └── QuickSort.php └── Table.php └── test.php /.gitignore: -------------------------------------------------------------------------------- 1 | composer.phar 2 | /vendor/ 3 | /.vscode/ 4 | 5 | # Commit your application's lock file http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file 6 | # You may choose to ignore a library lock file http://getcomposer.org/doc/02-libraries.md#lock-file 7 | # composer.lock 8 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Tianle Xu 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Tianle Xu 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # NonDB 2 | NonDB Stands for None DB. 3 | 4 | ## Install 安装 5 | ```sh 6 | composer install xtlsoft/nondb 7 | ``` 8 | 9 | ## Usage 使用 10 | ```php 11 | table('test')->getAll()[0]->another[0]->name; 19 | 20 | $person = $db->table('person')->create(); 21 | $person->id = $person->parent()->autoincrement(); 22 | $person->name = $name; 23 | $person->hobbies = ['swimming', 'dancing']; 24 | $person->location = $db->table('location')[10]->name; 25 | $person->save(); 26 | 27 | $anotherPerson = $person->parent()->find(NonDB::where('location', '%China%')); 28 | 29 | $anotherPerson->setMode('id', NonDB::Auto)->save(); 30 | echo $anotherPerson->name; 31 | 32 | echo $db->dump(\NonDB\Components\Dump::DUMP_JSON_PRETTY); 33 | 34 | ``` 35 | 36 | ## Documents 文档 37 | https://docs.xapps.top 38 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "xtlsoft/nondb", 3 | "description": "A NoSQL Database Based On Json Storage.", 4 | "type": "library", 5 | "require": { 6 | "php": ">=5.4" 7 | }, 8 | "license": "MIT", 9 | "authors": [ 10 | { 11 | "name": "xtl", 12 | "email": "xtl@xtlsoft.top" 13 | } 14 | ], 15 | "autoload": { 16 | "psr-4": { 17 | "NonDB\\" : "./src/" 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /composer.lock: -------------------------------------------------------------------------------- 1 | { 2 | "_readme": [ 3 | "This file locks the dependencies of your project to a known state", 4 | "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", 5 | "This file is @generated automatically" 6 | ], 7 | "content-hash": "923b7bcdabd6ad03bc0b62bc9a1a2e01", 8 | "packages": [], 9 | "packages-dev": [], 10 | "aliases": [], 11 | "minimum-stability": "stable", 12 | "stability-flags": [], 13 | "prefer-stable": false, 14 | "prefer-lowest": false, 15 | "platform": { 16 | "php": ">=5.4" 17 | }, 18 | "platform-dev": [] 19 | } 20 | -------------------------------------------------------------------------------- /data/test.json: -------------------------------------------------------------------------------- 1 | {"test1":[[1,2],[2,1],[221,123],[1332,0],[321,123]],"files":{"4":"100-swoole.ini","1":"50-mysql.ini","2":"45-datagird.ini","0":"10-mbstring.ini","3":"3-php.ini"},"_NonDB_System_":{"_AutoIncrement_":10002}} -------------------------------------------------------------------------------- /src/Components/ArrayAccess.php: -------------------------------------------------------------------------------- 1 | 9 | * @package NonDB 10 | * @license MIT 11 | * @category database 12 | * @link https://github.com/xtlsoft/NonDB/ 13 | * 14 | */ 15 | 16 | namespace NonDB\Components; 17 | 18 | trait ArrayAccess { 19 | 20 | /** 21 | * Check whether an offset defined 22 | * 23 | * @param mixed $offset 24 | * 25 | * @return bool 26 | * 27 | */ 28 | public function offsetExists ( $offset ){ 29 | return isset($this->data[$offset]); 30 | } 31 | 32 | /** 33 | * Get the value 34 | * 35 | * @param mixed $offset 36 | * 37 | * @return mixed 38 | * 39 | */ 40 | public function offsetGet ( $offset ) { 41 | 42 | return $this->data[$offset]; 43 | 44 | } 45 | 46 | /** 47 | * Set the value 48 | * 49 | * @param mixed $offset 50 | * @param mixed $value 51 | * 52 | * @return void 53 | * 54 | */ 55 | public function offsetSet ( $offset , $value ){ 56 | if($value instanceof \NonDB\Data){ 57 | $value = $value->getArray(); 58 | } 59 | $this->data[$offset] = $value; 60 | } 61 | 62 | /** 63 | * Unset the data 64 | * 65 | * @param mixed $offset 66 | * 67 | * @return void 68 | * 69 | */ 70 | public function offsetUnset ( $offset ){ 71 | unset($this->data[$offset]); 72 | } 73 | 74 | } -------------------------------------------------------------------------------- /src/Components/Data.php: -------------------------------------------------------------------------------- 1 | 9 | * @package NonDB 10 | * @license MIT 11 | * @category database 12 | * @link https://github.com/xtlsoft/NonDB/ 13 | * 14 | */ 15 | 16 | namespace NonDB\Components; 17 | 18 | trait Data { 19 | 20 | /** 21 | * Data 22 | * 23 | * @var mixed $data 24 | * 25 | */ 26 | public $data; 27 | protected $key; 28 | 29 | public function get($key) {return $this->__get($key);} 30 | public function set($key, $val) {$this->__set($key, $val); return $this;} 31 | public function unset($key) {$this->__unset($key); return $this;} 32 | 33 | /** 34 | * Get the data 35 | * 36 | * @param mixed $key 37 | * 38 | * @return mixed 39 | * 40 | */ 41 | public function __get($key){ 42 | 43 | if(is_array($this->data[$key])){ 44 | return (new \NonDB\Data($this->data[$key], $key))->setParent($this); 45 | }else{ 46 | return $this->data[$key]; 47 | } 48 | 49 | } 50 | 51 | /** 52 | * Unset A Key 53 | * 54 | * @param mixed $key 55 | * 56 | */ 57 | public function __unset($key){ 58 | 59 | unset($this->data[$key]); 60 | 61 | } 62 | 63 | /** 64 | * Set the data 65 | * 66 | * @param mixed $key 67 | * @param mixed $val 68 | * 69 | */ 70 | public function __set($key, $val){ 71 | 72 | if($val instanceof \NonDB\Data){ 73 | $val = $val->getArray(); 74 | } 75 | 76 | $this->data[$key] = $val; 77 | 78 | } 79 | 80 | /** 81 | * Get the array. 82 | * 83 | * @return array 84 | * 85 | */ 86 | public function getArray(){ 87 | 88 | return $this->data; 89 | 90 | } 91 | 92 | /** 93 | * Make it countable. 94 | * 95 | * @return int 96 | * 97 | */ 98 | public function count(){ 99 | 100 | return count($this->data); 101 | 102 | } 103 | 104 | /** 105 | * Create a data collection 106 | * 107 | * @param string $name 108 | * 109 | * @return \NonDB\Data 110 | * 111 | */ 112 | public function create($name){ 113 | 114 | $this->data[$name] = []; 115 | 116 | return $this->{$name}; 117 | 118 | } 119 | 120 | /** 121 | * Get one of the data. 122 | * 123 | * @return \NonDB\data 124 | * 125 | */ 126 | public function findOne(){ 127 | 128 | foreach($this->data as $k=>$v){ 129 | $key = $k; 130 | break; 131 | } 132 | 133 | if(is_array($this->data[$key])){ 134 | return (new \NonDB\Data($this->data[$key], $key))->setParent($this); 135 | }else{ 136 | return $this->data[$key]; 137 | } 138 | 139 | } 140 | 141 | /** 142 | * Find something 143 | * 144 | * @param Callable $rule 145 | * 146 | * @return array 147 | * 148 | */ 149 | public function find($rule){ 150 | 151 | $r = []; 152 | 153 | foreach($this->data as $k=>$v){ 154 | if($rule($this->{$k})){ 155 | $r[] = $this->{$k}; 156 | } 157 | } 158 | 159 | return $r; 160 | 161 | } 162 | 163 | /** 164 | * Find something by the key 165 | * 166 | * @param Callable $rule 167 | * 168 | * @return array 169 | * 170 | */ 171 | public function findByKey($rule){ 172 | 173 | $r = []; 174 | 175 | foreach($this->data as $k=>$v){ 176 | if($rule($k)){ 177 | $r[] = $this->{$k}; 178 | } 179 | } 180 | 181 | return $r; 182 | 183 | } 184 | 185 | /** 186 | * Sort The Data 187 | * 188 | * @param Callable $rule 189 | * @param string $algorithm 190 | * 191 | * @throws \NonDB\Exceptions\DataException 192 | * 193 | * @return \NonDB\Data 194 | * 195 | */ 196 | public function sort($rule, $algorithm = "DefaultSort"){ 197 | 198 | $class = "\\NonDB\\Sorter\\" . $algorithm; 199 | 200 | if(!class_exists($class) || !( \NonDB\Components\Tool::checkImplement($class, "NonDB\\Interfaces\\Sorter") )){ 201 | throw new \NonDB\Exceptions\DataException("Sorter $class didn't exists.", "0011"); 202 | return false; 203 | } 204 | 205 | $sorter = eval("return new ". $class . "(\$rule);"); 206 | 207 | $sorted = $sorter->sort($this->getArray()); 208 | 209 | $result = (new \NonDB\Data($sorted, $this->key))->setParent($this->parent); 210 | 211 | return $result; 212 | 213 | } 214 | 215 | /** 216 | * Dump It! 217 | * 218 | * @param int $type 219 | * 220 | * @return mixed 221 | * 222 | */ 223 | public function dump($type = \NonDB\Components\Dump::DUMP_VAR_DUMP){ 224 | 225 | return \NonDB\Components\Dump::dump($this->getArray(), $type); 226 | 227 | } 228 | 229 | } 230 | -------------------------------------------------------------------------------- /src/Components/Different.php: -------------------------------------------------------------------------------- 1 | 9 | * @package NonDB 10 | * @license MIT 11 | * @category database 12 | * @link https://github.com/xtlsoft/NonDB/ 13 | * 14 | */ 15 | 16 | namespace NonDB\Components; 17 | 18 | class Different { 19 | 20 | 21 | 22 | } -------------------------------------------------------------------------------- /src/Components/Dump.php: -------------------------------------------------------------------------------- 1 | 9 | * @package NonDB 10 | * @license MIT 11 | * @category database 12 | * @link https://github.com/xtlsoft/NonDB/ 13 | * 14 | */ 15 | 16 | namespace NonDB\Components; 17 | 18 | class Dump { 19 | 20 | /** 21 | * Constants 22 | * 23 | */ 24 | const DUMP_JSON = 9000; 25 | const DUMP_JSON_PRETTY = 9001; 26 | const DUMP_XML = 9010; 27 | const DUMP_VAR_EXPORT = 9020; 28 | const DUMP_VAR_DUMP = 9030; 29 | const DUMP_SERIALIZE = 9040; 30 | 31 | /** 32 | * Dump it! 33 | * 34 | * @param array $data 35 | * @param int $type 36 | * 37 | * @return mixed 38 | * 39 | */ 40 | public static function dump($data, $type = self::DUMP_VAR_DUMP){ 41 | 42 | switch($type){ 43 | 44 | case self::DUMP_JSON: 45 | return json_encode($data); 46 | case self::DUMP_JSON_PRETTY: 47 | return json_encode($data, JSON_PRETTY_PRINT); 48 | case self::DUMP_VAR_EXPORT: 49 | return var_export($data); 50 | case self::DUMP_VAR_DUMP: 51 | return var_dump($data); 52 | case self::DUMP_XML: 53 | return self::arrayToXML($data); 54 | case self::DUMP_SERIALIZE: 55 | return serialize($data); 56 | 57 | } 58 | 59 | } 60 | 61 | /** 62 | * Turn array to XML 63 | * 64 | * @param array $data 65 | * @param \SimpleXMLElement &$xml 66 | * 67 | * @return string 68 | * 69 | */ 70 | protected static function arrayToXML($data, &$xml = null){ 71 | 72 | if($xml === null) 73 | $xml = new \SimpleXMLElement(""); 74 | foreach($data as $k=>$v){ 75 | if(is_array($v)){ 76 | $sub = $xml->addChild("$k"); 77 | self::arrayToXML($v, $sub); 78 | }else{ 79 | $xml->addChild("$k", htmlspecialchars("$v")); 80 | } 81 | } 82 | 83 | return $xml->asXML(); 84 | 85 | } 86 | 87 | } 88 | -------------------------------------------------------------------------------- /src/Components/ParentClass.php: -------------------------------------------------------------------------------- 1 | 9 | * @package NonDB 10 | * @license MIT 11 | * @category database 12 | * @link https://github.com/xtlsoft/NonDB/ 13 | * 14 | */ 15 | 16 | namespace NonDB\Components; 17 | 18 | trait ParentClass { 19 | 20 | /** 21 | * Parent Class 22 | * 23 | * @var mixed 24 | */ 25 | private $parent; 26 | 27 | /** 28 | * Callback after set parentClass 29 | * 30 | * @var callable 31 | */ 32 | private $parentCallback = null; 33 | 34 | /** 35 | * Set the parent 36 | * 37 | * @param mixed $parent 38 | * 39 | * @return self 40 | * 41 | */ 42 | public function setParent($parent){ 43 | 44 | $this->parent = $parent; 45 | 46 | if($this->parentCallback){ 47 | call_user_func($this->parentCallback); 48 | } 49 | 50 | return $this; 51 | 52 | } 53 | 54 | /** 55 | * Set a callback 56 | * 57 | * @param Callable $callback 58 | * @param bool $force 59 | * 60 | * @return self 61 | * 62 | */ 63 | public function setCallback(Callable $callback, bool $force = false){ 64 | 65 | if($force || !$this->parentCallback){ 66 | $this->parentCallback = $callback; 67 | } 68 | 69 | return $this; 70 | 71 | } 72 | 73 | /** 74 | * Get the parent 75 | * 76 | * @return mixed 77 | * 78 | */ 79 | public function parent(){ 80 | 81 | return $this->parent; 82 | 83 | } 84 | 85 | } -------------------------------------------------------------------------------- /src/Components/Status.php: -------------------------------------------------------------------------------- 1 | 9 | * @package NonDB 10 | * @license MIT 11 | * @category database 12 | * @link https://github.com/xtlsoft/NonDB/ 13 | * 14 | */ 15 | 16 | namespace NonDB\Components; 17 | 18 | class Status { 19 | 20 | /** 21 | * Status 22 | * 23 | * @var mixed 24 | * 25 | */ 26 | public $status; 27 | 28 | /** 29 | * Message 30 | * 31 | * @var string 32 | * 33 | */ 34 | public $msg; 35 | 36 | /** 37 | * Constructor 38 | * 39 | * @param mixed $status 40 | * @param string $msg 41 | * 42 | */ 43 | public function __construct($status, string $msg = ""){ 44 | $this->status = $status; 45 | $this->msg = $msg; 46 | } 47 | 48 | } -------------------------------------------------------------------------------- /src/Components/Tool.php: -------------------------------------------------------------------------------- 1 | 9 | * @package NonDB 10 | * @license MIT 11 | * @category database 12 | * @link https://github.com/xtlsoft/NonDB/ 13 | * 14 | */ 15 | 16 | namespace NonDB\Components; 17 | 18 | class Tool { 19 | 20 | /** 21 | * Check the class implement 22 | * 23 | * @param mixed $class 24 | * @param string $interface 25 | * 26 | * @return void 27 | * 28 | */ 29 | public static function checkImplement($class, string $interface){ 30 | $ref = new \ReflectionClass($class); 31 | $names = $ref->getInterfaceNames(); 32 | if(in_array($interface, $names)){ 33 | return true; 34 | }else{ 35 | return false; 36 | } 37 | } 38 | 39 | } -------------------------------------------------------------------------------- /src/Data.php: -------------------------------------------------------------------------------- 1 | 9 | * @package NonDB 10 | * @license MIT 11 | * @category database 12 | * @link https://github.com/xtlsoft/NonDB/ 13 | * 14 | */ 15 | 16 | namespace NonDB; 17 | 18 | class Data implements \ArrayAccess, \Countable{ 19 | 20 | use \NonDB\Components\ParentClass; 21 | use \NonDB\Components\Data; 22 | use \NonDB\Components\ArrayAccess; 23 | 24 | /** 25 | * Constructor 26 | * 27 | * @param mixed $data 28 | * @param mixed $key 29 | * 30 | */ 31 | public function __construct($data, $key){ 32 | 33 | $this->data = $data; 34 | $this->key = $key; 35 | 36 | } 37 | 38 | /** 39 | * Save the data 40 | * 41 | * @return self 42 | */ 43 | public function save(){ 44 | 45 | $this->parent->__set($this->key, $this->data); 46 | $this->parent->save(); 47 | 48 | return $this; 49 | 50 | } 51 | 52 | } 53 | -------------------------------------------------------------------------------- /src/Drivers/LocalDriver.php: -------------------------------------------------------------------------------- 1 | 9 | * @package NonDB 10 | * @license MIT 11 | * @category database 12 | * @link https://github.com/xtlsoft/NonDB/ 13 | * 14 | */ 15 | 16 | namespace NonDB\Drivers; 17 | 18 | class LocalDriver implements \NonDB\Interfaces\Driver { 19 | 20 | /** 21 | * BaseDir 22 | * 23 | * @var string 24 | * 25 | */ 26 | protected $base = './data/'; 27 | 28 | /** 29 | * Constructor 30 | * 31 | * @throws \NonDB\Exceptions\DriverException 32 | * @param string $param 33 | * 34 | */ 35 | public function __construct(string $param){ 36 | 37 | if(is_dir($param)) 38 | $this->base = $param; 39 | else 40 | throw new \NonDB\Exceptions\DriverException('Base Not Exists.', 1001); 41 | 42 | } 43 | 44 | /** 45 | * Get a table's data 46 | * 47 | * @param string $table 48 | * @param mixed $name = "" 49 | * @throws \NonDB\Exceptions\DriverException 50 | * 51 | * @return mixed 52 | * 53 | */ 54 | public function getData(string $table, $name = ""){ 55 | 56 | $file = $this->base . '/' . $table . '.json'; 57 | if(file_exists($file)){ 58 | return json_decode(file_get_contents($file), true); 59 | }else{ 60 | throw new \NonDB\Exceptions\DriverException('Table Not Exists.', 1002); 61 | } 62 | 63 | } 64 | 65 | /** 66 | * Set a table's data 67 | * 68 | * @param string $table 69 | * @param mixed[] $data 70 | * @throws \NonDB\Exceptions\DriverException 71 | * 72 | * @return \NonDB\Components\Status 73 | * 74 | */ 75 | public function setData(string $table, $data){ 76 | 77 | $file = $this->base . '/' . $table . '.json'; 78 | if(file_exists($file)){ 79 | if(file_put_contents($file, json_encode($data))){ 80 | $status = true; 81 | }else{ 82 | $status = false; 83 | } 84 | return new \NonDB\Components\Status($status); 85 | }else{ 86 | throw new \NonDB\Exceptions\DriverException('Table Not Exists.', 1002); 87 | } 88 | 89 | } 90 | 91 | /** 92 | * Add a table 93 | * 94 | * @param string $name 95 | * @throws \NonDB\Exceptions\DriverException 96 | * 97 | * @return \NonDB\Components\Status 98 | * 99 | */ 100 | public function newTable(string $name){ 101 | 102 | $status = file_put_contents($file = $this->base . '/' . $name . '.json', "[]"); 103 | 104 | return new \NonDB\Components\Status($status); 105 | 106 | } 107 | 108 | /** 109 | * Remove a table 110 | * 111 | * @param string $name 112 | * @throws \NonDB\Exceptions\DriverException 113 | * 114 | * @return \NonDB\Components\Status 115 | * 116 | */ 117 | public function removeTable(string $name){ 118 | 119 | $status = unlink($file = $this->base . '/' . $name . '.json'); 120 | 121 | return new \NonDB\Components\Status($status); 122 | 123 | } 124 | 125 | } -------------------------------------------------------------------------------- /src/Exceptions/Base.php: -------------------------------------------------------------------------------- 1 | 9 | * @package NonDB 10 | * @license MIT 11 | * @category database 12 | * @link https://github.com/xtlsoft/NonDB/ 13 | * 14 | */ 15 | 16 | namespace NonDB\Exceptions; 17 | 18 | trait Base { 19 | 20 | /** 21 | * Get the type of the exception. 22 | * 23 | * @return string 24 | * 25 | */ 26 | public function getExceptionType(){ 27 | 28 | return $this->type; 29 | 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /src/Exceptions/CoreException.php: -------------------------------------------------------------------------------- 1 | 9 | * @package NonDB 10 | * @license MIT 11 | * @category database 12 | * @link https://github.com/xtlsoft/NonDB/ 13 | * 14 | */ 15 | 16 | namespace NonDB\Exceptions; 17 | 18 | class CoreException extends \Exception { 19 | 20 | use \NonDB\Exceptions\Base; 21 | 22 | /** 23 | * Type of the exception. 24 | * 25 | * @var string 26 | * 27 | */ 28 | protected $type = "CoreException"; 29 | 30 | } -------------------------------------------------------------------------------- /src/Exceptions/DataException.php: -------------------------------------------------------------------------------- 1 | 9 | * @package NonDB 10 | * @license MIT 11 | * @category database 12 | * @link https://github.com/xtlsoft/NonDB/ 13 | * 14 | */ 15 | 16 | namespace NonDB\Exceptions; 17 | 18 | class DataException extends \Exception { 19 | 20 | use \NonDB\Exceptions\Base; 21 | 22 | /** 23 | * Type of the exception. 24 | * 25 | * @var string 26 | * 27 | */ 28 | protected $type = "DataException"; 29 | 30 | } -------------------------------------------------------------------------------- /src/Exceptions/DriverException.php: -------------------------------------------------------------------------------- 1 | 9 | * @package NonDB 10 | * @license MIT 11 | * @category database 12 | * @link https://github.com/xtlsoft/NonDB/ 13 | * 14 | */ 15 | 16 | namespace NonDB\Exceptions; 17 | 18 | class DriverException extends \Exception { 19 | 20 | use \NonDB\Exceptions\Base; 21 | 22 | /** 23 | * Type of the exception. 24 | * 25 | * @var string 26 | * 27 | */ 28 | protected $type = "DriverException"; 29 | 30 | } -------------------------------------------------------------------------------- /src/Interfaces/Driver.php: -------------------------------------------------------------------------------- 1 | 9 | * @package NonDB 10 | * @license MIT 11 | * @category database 12 | * @link https://github.com/xtlsoft/NonDB/ 13 | * 14 | */ 15 | 16 | namespace NonDB\Interfaces; 17 | 18 | /** 19 | * Driver Interface 20 | * 21 | * @method void __construct() 22 | * @method mixed getData() 23 | * @method \NonDB\Components\Status setData() 24 | * @method \NonDB\Components\Status addTable() 25 | * @method \NonDB\Components\Status removeTable() 26 | * 27 | */ 28 | interface Driver { 29 | 30 | /** 31 | * Constructor 32 | * 33 | * @param string $param 34 | * 35 | */ 36 | public function __construct(string $param); 37 | 38 | /** 39 | * Get a table's data 40 | * 41 | * @param string $table 42 | * @param mixed $name = "" 43 | * @throws \NonDB\Exceptions\DriverException 44 | * 45 | * @return mixed 46 | * 47 | */ 48 | public function getData(string $table, $name = ""); 49 | 50 | /** 51 | * Set a table's data 52 | * 53 | * @param string $table 54 | * @param mixed[] $data 55 | * @throws \NonDB\Exceptions\DriverException 56 | * 57 | * @return \NonDB\Components\Status 58 | * 59 | */ 60 | public function setData(string $table, $data); 61 | 62 | /** 63 | * Add a table 64 | * 65 | * @param string $name 66 | * @throws \NonDB\Exceptions\DriverException 67 | * 68 | * @return \NonDB\Components\Status 69 | * 70 | */ 71 | public function newTable(string $name); 72 | 73 | /** 74 | * Remove a table 75 | * 76 | * @param string $name 77 | * @throws \NonDB\Exceptions\DriverException 78 | * 79 | * @return \NonDB\Components\Status 80 | * 81 | */ 82 | public function removeTable(string $name); 83 | 84 | } -------------------------------------------------------------------------------- /src/Interfaces/Sorter.php: -------------------------------------------------------------------------------- 1 | 9 | * @package NonDB 10 | * @license MIT 11 | * @category database 12 | * @link https://github.com/xtlsoft/NonDB/ 13 | * 14 | */ 15 | 16 | namespace NonDB\Interfaces; 17 | 18 | /** 19 | * Sorter Interface 20 | * 21 | * @method void __construct() 22 | * @method mixed sort() 23 | * 24 | */ 25 | interface Sorter { 26 | 27 | /** 28 | * Constructor 29 | * 30 | * @param Callable $rule 31 | * 32 | */ 33 | public function __construct($rule); 34 | 35 | /** 36 | * Sort it. 37 | * 38 | * @param mixed $data 39 | * 40 | * @return mixed 41 | * 42 | */ 43 | public function sort($data); 44 | 45 | } -------------------------------------------------------------------------------- /src/NonDB.php: -------------------------------------------------------------------------------- 1 | 9 | * @package NonDB 10 | * @license MIT 11 | * @category database 12 | * @link https://github.com/xtlsoft/NonDB/ 13 | * 14 | */ 15 | 16 | namespace NonDB; 17 | 18 | class NonDB { 19 | 20 | /** 21 | * NonDB Version 22 | */ 23 | const VERSION = "0.1.0"; 24 | 25 | /** 26 | * Create a driver 27 | * 28 | * @static 29 | * @param string $name Example: "Json:./data/" 30 | * @throws \NonDB\Exceptions\CoreException 31 | * @return \NonDB\Interfaces\Driver 32 | * 33 | */ 34 | public static function driver(string $name){ 35 | 36 | //Get the name. 37 | $name = explode(':', $name); 38 | //Get the classname. 39 | $class = "\\NonDB\\Drivers\\" . $name[0]; 40 | //Get the parameter. 41 | $param = $name[1]; 42 | 43 | //See if the driver exists. 44 | if(!class_exists($class) || !( \NonDB\Components\Tool::checkImplement($class, "NonDB\\Interfaces\\Driver") )){ 45 | throw new \NonDB\Exceptions\CoreException("Driver $class wasn't exists.", "0011"); 46 | return false; 47 | } 48 | 49 | //Get driver. 50 | $driver = eval("return new $class(urldecode(\$param));"); 51 | 52 | return $driver; 53 | 54 | } 55 | 56 | /** 57 | * Find something 58 | * 59 | * @param string $key 60 | * @param string $value 61 | * 62 | * @return Callable 63 | * 64 | */ 65 | public static function where(string $key, string $value){ 66 | 67 | return function($compare) use ($key, $value){ 68 | 69 | return (@$compare[$key] == $value); 70 | 71 | }; 72 | 73 | } 74 | 75 | /** 76 | * Find A Value By Key 77 | * 78 | * @param string $key 79 | * 80 | * @return Callable 81 | * 82 | */ 83 | public static function whereByKey(string $key){ 84 | 85 | return function($compare) use ($key) { 86 | 87 | return ($compare == $key); 88 | 89 | }; 90 | 91 | } 92 | 93 | /** 94 | * Configure 95 | * 96 | * @var array 97 | * 98 | */ 99 | public $config = []; 100 | 101 | /** 102 | * Driver 103 | * 104 | * @var \NonDB\Interfaces\Driver 105 | * 106 | */ 107 | public $driver; 108 | 109 | /** 110 | * Constructor 111 | * 112 | * @param \NonDB\Interfaces\Driver $driver 113 | * @param array $config 114 | * 115 | */ 116 | public function __construct(\NonDB\Interfaces\Driver $driver, $config = []){ 117 | 118 | $this->setDriver($driver); 119 | $this->config($config); 120 | 121 | } 122 | 123 | /** 124 | * Get the table. 125 | * 126 | * @param string $table 127 | * @return \NonDB\Table 128 | * 129 | */ 130 | public function table(string $table = "default"){ 131 | 132 | return (new \NonDB\Table($table))->setParent($this); 133 | 134 | } 135 | 136 | /** 137 | * Set The DB Driver 138 | * 139 | * @param \NonDB\Interfaces\Driver $driver 140 | * @return self 141 | * 142 | */ 143 | public function setDriver(\NonDB\Interfaces\Driver $driver){ 144 | 145 | $this->driver = $driver; 146 | 147 | return $this; 148 | 149 | } 150 | 151 | /** 152 | * Configure NonDB 153 | * 154 | * @param mixed $config 155 | * @param string $val 156 | * 157 | * @return self 158 | * 159 | */ 160 | public function config($config, $val = ""){ 161 | 162 | if(is_array($config)){ 163 | $this->config = array_merge($this->config, $config); 164 | }else{ 165 | $this->config[$config] = $val; 166 | } 167 | 168 | return $this; 169 | 170 | } 171 | 172 | } -------------------------------------------------------------------------------- /src/Sorter/DefaultSort.php: -------------------------------------------------------------------------------- 1 | 9 | * @package NonDB 10 | * @license MIT 11 | * @category database 12 | * @link https://github.com/xtlsoft/NonDB/ 13 | * 14 | */ 15 | 16 | namespace NonDB\Sorter; 17 | 18 | class DefaultSort implements \NonDB\Interfaces\Sorter { 19 | 20 | /** 21 | * Rule 22 | * 23 | * @var Callable 24 | */ 25 | protected $rule; 26 | 27 | /** 28 | * Constructor 29 | * 30 | * @param Callable $rule 31 | * 32 | * @return void 33 | * 34 | */ 35 | public function __construct($rule){ 36 | 37 | $this->rule = $rule; 38 | 39 | } 40 | 41 | /** 42 | * Sort it. 43 | * 44 | * @param mixed $data 45 | * 46 | * @return mixed 47 | * 48 | */ 49 | public function sort($data){ 50 | 51 | uasort($data, $this->rule); 52 | 53 | return $data; 54 | 55 | } 56 | 57 | } -------------------------------------------------------------------------------- /src/Sorter/NaturalSort.php: -------------------------------------------------------------------------------- 1 | 9 | * @package NonDB 10 | * @license MIT 11 | * @category database 12 | * @link https://github.com/xtlsoft/NonDB/ 13 | * 14 | */ 15 | 16 | namespace NonDB\Sorter; 17 | 18 | class NaturalSort implements \NonDB\Interfaces\Sorter { 19 | 20 | /** 21 | * Rule 22 | * 23 | * @var Callable 24 | */ 25 | protected $rule; 26 | 27 | /** 28 | * Constructor 29 | * 30 | * @param Callable $rule 31 | * 32 | * @return void 33 | * 34 | */ 35 | public function __construct($rule){ 36 | 37 | $this->rule = $rule; 38 | 39 | } 40 | 41 | /** 42 | * Sort it. 43 | * 44 | * @param mixed $data 45 | * 46 | * @return mixed 47 | * 48 | */ 49 | public function sort($data){ 50 | 51 | $rule = call_user_func($this->rule); 52 | if($rule === "desc"){ 53 | arsort($data, SORT_NATURAL); 54 | }else{ 55 | asort($data, SORT_NATURAL); 56 | } 57 | 58 | return $data; 59 | 60 | } 61 | 62 | } -------------------------------------------------------------------------------- /src/Sorter/QuickSort.php: -------------------------------------------------------------------------------- 1 | 9 | * @package NonDB 10 | * @license MIT 11 | * @category database 12 | * @link https://github.com/xtlsoft/NonDB/ 13 | * 14 | */ 15 | 16 | namespace NonDB\Sorter; 17 | 18 | class QuickSort implements \NonDB\Interfaces\Sorter { 19 | 20 | /** 21 | * Rule 22 | * 23 | * @var Callable 24 | */ 25 | protected $rule; 26 | 27 | /** 28 | * Constructor 29 | * 30 | * @param Callable $rule 31 | * 32 | * @return void 33 | * 34 | */ 35 | public function __construct($rule){ 36 | 37 | $this->rule = $rule; 38 | 39 | } 40 | 41 | /** 42 | * Sort it. 43 | * 44 | * @param mixed $data 45 | * 46 | * @return mixed 47 | * 48 | */ 49 | public function sort($data){ 50 | 51 | $count = count($data); 52 | 53 | if($count <= 1) return $data; 54 | 55 | $left = $right = []; 56 | 57 | return $sorted; 58 | 59 | } 60 | 61 | } -------------------------------------------------------------------------------- /src/Table.php: -------------------------------------------------------------------------------- 1 | 9 | * @package NonDB 10 | * @license MIT 11 | * @category database 12 | * @link https://github.com/xtlsoft/NonDB/ 13 | * 14 | */ 15 | 16 | namespace NonDB; 17 | 18 | class Table implements \ArrayAccess, \Countable { 19 | 20 | use \NonDB\Components\ParentClass; 21 | use \NonDB\Components\Data; 22 | use \NonDB\Components\ArrayAccess; 23 | 24 | /** 25 | * Name of the table 26 | * 27 | * @var string 28 | * 29 | */ 30 | protected $name; 31 | 32 | /** 33 | * The data 34 | * 35 | * @var array 36 | * 37 | */ 38 | public $data; 39 | 40 | /** 41 | * Constructor 42 | * 43 | * @param mixed $name 44 | * 45 | */ 46 | public function __construct(string $name){ 47 | 48 | $this->name = $name; 49 | $this->parentCallback = function(){ 50 | $this->sync(); 51 | }; 52 | 53 | } 54 | 55 | /** 56 | * Sync the data 57 | * 58 | * @return self 59 | * 60 | */ 61 | public function sync(){ 62 | 63 | $this->data = $this->parent->driver->getData($this->name); 64 | 65 | return $this; 66 | 67 | } 68 | 69 | /** 70 | * Save the data 71 | * 72 | * @return self 73 | * 74 | */ 75 | public function save(){ 76 | 77 | $this->parent->driver->setData($this->name, $this->data); 78 | 79 | return $this; 80 | 81 | } 82 | 83 | /** 84 | * Get Auto-Increment Number 85 | * 86 | * @return int 87 | * 88 | */ 89 | function autoincrement(){ 90 | 91 | if(!isset($this->data['_NonDB_System_']['_AutoIncrement_'])){ 92 | $this->data['_NonDB_System_']['_AutoIncrement_'] = 1; 93 | } 94 | 95 | $number = ($this->data['_NonDB_System_']['_AutoIncrement_'] ++); 96 | $this->save(); 97 | 98 | return $number; 99 | 100 | } 101 | 102 | } 103 | -------------------------------------------------------------------------------- /test.php: -------------------------------------------------------------------------------- 1 | table("test")->dump(\NonDB\Components\Dump::DUMP_VAR_EXPORT); 17 | 18 | 19 | --------------------------------------------------------------------------------