aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorWilly Micieli <micieli@laposte.net>2019-11-28 17:49:01 +0100
committerWilly Micieli <micieli@laposte.net>2019-11-28 17:49:01 +0100
commit2487b19cfcdccb60322a00161350b28f8452cb0a (patch)
treeded2b2413139a77a427804aab49db3c4d5dde611
parent7b8e0f14d786f63a61edf04bfa11236a5c3d7a6c (diff)
downloadpandora-2487b19cfcdccb60322a00161350b28f8452cb0a.zip
pandora-2487b19cfcdccb60322a00161350b28f8452cb0a.tar.gz
clean codeHEADmaster
-rw-r--r--.gitignore1
-rw-r--r--Makefile7
-rw-r--r--composer.json3
-rw-r--r--config/pagination.yaml4
-rw-r--r--eywa/Config/Config.php97
-rw-r--r--eywa/Database/Connexion/Connect.php2
-rw-r--r--eywa/Database/Model/Model.php72
-rw-r--r--eywa/Database/Query/Sql.php619
-rw-r--r--eywa/Debug/Dumper.php40
-rw-r--r--eywa/Debug/HtmlDumper.php28
-rw-r--r--eywa/Html/Pagination.php108
-rw-r--r--eywa/Http/Routing/Router.php2
-rw-r--r--eywa/Testing/Unit.php6
-rw-r--r--helpers/admin.php511
-rw-r--r--helpers/const.php16
-rw-r--r--helpers/web.php25
-rw-r--r--tests/Helpers/AdminHelpersTest.php28
-rw-r--r--tests/Http/ResponseTest.php1
-rw-r--r--tests/Sql/SqlTest.php145
19 files changed, 1470 insertions, 245 deletions
diff --git a/.gitignore b/.gitignore
index d369672..aa1f774 100644
--- a/.gitignore
+++ b/.gitignore
@@ -4,3 +4,4 @@
.phpunit.result.cache
coverage
web
+*.sql \ No newline at end of file
diff --git a/Makefile b/Makefile
index 07597b1..21c2a26 100644
--- a/Makefile
+++ b/Makefile
@@ -1,3 +1,6 @@
-all:
+all: db
phpunit --coverage-html coverage
-
+db:
+ mysql -uroot -proot -e "DROP DATABASE IF EXISTS pandora"
+ mysql -uroot -proot -e "CREATE DATABASE pandora"
+ mysql -uroot -proot pandora < app.sql \ No newline at end of file
diff --git a/composer.json b/composer.json
index 9beee2f..36de883 100644
--- a/composer.json
+++ b/composer.json
@@ -30,7 +30,8 @@
"vlucas/phpdotenv": "^3.6",
"nesbot/carbon": "^2.27",
"ext-PDO": "*",
- "codedungeon/phpunit-result-printer": "^0.26.2"
+ "codedungeon/phpunit-result-printer": "^0.26.2",
+ "symfony/http-foundation": "^5.0"
},
"require-dev": {
}
diff --git a/config/pagination.yaml b/config/pagination.yaml
new file mode 100644
index 0000000..9b93dc8
--- /dev/null
+++ b/config/pagination.yaml
@@ -0,0 +1,4 @@
+url: '?page='
+link_class: 'page-link'
+ul_class: 'd-flex justify-content-end pagination mt-5'
+li_class: 'page-item'
diff --git a/eywa/Config/Config.php b/eywa/Config/Config.php
new file mode 100644
index 0000000..a9785ec
--- /dev/null
+++ b/eywa/Config/Config.php
@@ -0,0 +1,97 @@
+<?php
+
+
+namespace Eywa\Config {
+
+
+ use Eywa\Collection\Collect;
+ use Eywa\Exception\Kedavra;
+ use Symfony\Component\Yaml\Yaml;
+
+ class Config extends Yaml
+ {
+ /**
+ * @var string
+ *
+ */
+ const EXT = '.yaml';
+
+ /**
+ *
+ * The config filename
+ *
+ * @var string
+ *
+ */
+ private $file;
+
+ /**
+ *
+ * The config key
+ *
+ * @var mixed
+ *
+ */
+ private $key;
+
+ /**
+ * @var Collect
+ */
+ private $values;
+
+ /**
+ *
+ * Config constructor.
+ *
+ *
+ * @param string $file
+ * @param mixed $key
+
+ * @throws Kedavra
+ *
+ */
+ public function __construct(string $file, $key)
+ {
+
+ $file = $this->path() . DIRECTORY_SEPARATOR . collect(explode('.', $file))->first() . self::EXT;
+
+ is_false(file_exists($file), true, "The $file file was not found at " . $this->path());
+
+ $this->values = collect(self::parseFile($file));
+
+ is_false($this->values->has($key), true, "The $key key was not found in the $file at " . $this->path());
+
+ $this->file = $file;
+
+ $this->key = $key;
+ }
+
+ /**
+ * Get config path
+ *
+ * @method path
+ *
+ * @return string
+ *
+ */
+ public function path() : string
+ {
+ return base('config');
+ }
+
+ /**
+ *
+ * Get the config value
+ *
+ * @method value
+ *
+ * @return mixed
+ *
+ */
+ public function value()
+ {
+ return $this->values->get($this->key);
+ }
+
+ }
+} \ No newline at end of file
diff --git a/eywa/Database/Connexion/Connect.php b/eywa/Database/Connexion/Connect.php
index 7afb6c3..50e9c7e 100644
--- a/eywa/Database/Connexion/Connect.php
+++ b/eywa/Database/Connexion/Connect.php
@@ -103,7 +103,7 @@ namespace Eywa\Database\Connexion {
public function __construct(string $driver, string $base, string $username, string $password, string $host, string $dump_path)
{
- $this->dump_path = base('db') . DIRECTORY_SEPARATOR . $dump_path;
+ $this->dump_path = $dump_path;
$this->driver = $driver;
$this->database = $base;
$this->username = $username;
diff --git a/eywa/Database/Model/Model.php b/eywa/Database/Model/Model.php
index fb7d8d8..1172c92 100644
--- a/eywa/Database/Model/Model.php
+++ b/eywa/Database/Model/Model.php
@@ -4,8 +4,76 @@ namespace Eywa\Database\Model
{
abstract class Model
{
-
- public static function create(array $record)
+ /**
+ *
+ * The table associated with the model.
+ *
+ * @var string
+ *
+ */
+ protected $table;
+
+ /**
+ *
+ * The primary key for the model.
+ *
+ * @var string
+ *
+ */
+ protected $primary = 'id';
+
+ /**
+ *
+ * The column name used to find records
+ *
+ * @var string
+ *
+ */
+ protected static $by = 'id';
+
+ /**
+ *
+ * The per page limit
+ *
+ * @var int
+ *
+ */
+ protected static $limit = 20;
+
+ /**
+ *
+ * The sql query to create the table
+ *
+ * @var string
+ *
+ */
+ protected static $create_route_table_query = "CREATE TABLE IF NOT EXISTS routes ( id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT(255) NOT NULL UNIQUE,url TEXT(255) NOT NULL UNIQUE, controller TEXT(255) NOT NULL,action TEXT(255) NOT NULL,method TEXT(255) NOT NULL);";
+
+
+ protected static $create_todo = "CREATE TABLE IF NOT EXISTS todo ( id INTEGER PRIMARY KEY AUTOINCREMENT , task TEXT(255) NOT NULL UNIQUE , description TEXT(255) NOT NULL , priority TEXT(255) NOT NULL ,due TEXT(255) NOT NULL);";
+
+ /**
+ * @var bool
+ */
+ protected $routes = false;
+
+ /**
+ * @var bool
+ */
+ protected $admin = false;
+
+ /**
+ * @var bool
+ */
+ protected $todo = false;
+
+ /**
+ * @var bool
+ */
+ protected $task = false;
+
+
+ public static function create(array $record)
{
}
diff --git a/eywa/Database/Query/Sql.php b/eywa/Database/Query/Sql.php
new file mode 100644
index 0000000..1912ba4
--- /dev/null
+++ b/eywa/Database/Query/Sql.php
@@ -0,0 +1,619 @@
+<?php
+
+
+namespace Eywa\Database\Query {
+
+ use Eywa\Database\Connexion\Connect;
+ use Eywa\Exception\Kedavra;
+ use Eywa\Html\Pagination\Pagination;
+
+
+ class Sql
+ {
+ /**
+ *
+ * The connexion
+ *
+ * @var Connect
+ *
+ */
+ private $connect;
+
+ /**
+ *
+ * The where clause
+ *
+ * @var string
+ *
+ */
+ private $where;
+
+ /**
+ *
+ * The selected columns
+ *
+ * @var string
+ *
+ */
+ private $columns;
+
+ /**
+ *
+ * The order by
+ *
+ * @var string
+ *
+ */
+ private $order;
+
+ /**
+ *
+ * The from clause
+ *
+ * @var string
+ *
+ */
+ private $from;
+
+ /**
+ * @var bool
+ */
+ private $use_fetch;
+
+ /**
+ *
+ * The limit clause
+ *
+ * @var string
+ *
+ */
+ private $limit;
+
+ /**
+ *
+ * The pagination
+ *
+ * @var string
+ *
+ */
+ private $pagination;
+
+ /**
+ *
+ * The query results
+ *
+ * @var string
+ *
+ */
+ private $results;
+
+ /**
+ *
+ * The primary key
+ *
+ * @var string
+ *
+ */
+ private $primary;
+
+ /**
+ * The table name
+ *
+ * @var string
+ */
+ private $table;
+
+ /**
+ *
+ * The mode of the query
+ *
+ * @var string
+ *
+ */
+ private $mode;
+
+ /**
+ *
+ * The and clause
+ *
+ * @var string
+ *
+ */
+ private $and;
+
+ /**
+ *
+ * The or clause
+ *
+ * @var string
+ *
+ */
+ private $or;
+
+ /**
+ *
+ * The join clause
+ *
+ * @var string
+ *
+ */
+ private $join;
+
+ /**
+ *
+ * The union clause
+ *
+ * @var string
+ */
+ private $union;
+
+ /**
+ * @var string
+ */
+ private $first_table;
+ /**
+ * @var string
+ */
+ private $second_table;
+ /**
+ * @var string
+ */
+ private $first_param;
+ /**
+ * @var string
+ */
+ private $second_param;
+
+ /**
+ *
+ * Sql constructor.
+ *
+ * @param Connect $connect
+ * @param string $table
+ *
+ * @throws Kedavra
+ */
+ public function __construct(Connect $connect,string $table)
+ {
+ $this->from = "FROM $table";
+
+ $this->table = $table;
+
+ $this->connect = $connect;
+
+ $this->primary = $this->key();
+ }
+
+ /**
+ *
+ * Create a pagination
+ *
+ * @param $callable
+ * @param int $page
+ * @param int $limit
+ *
+ *
+ * @param string $url
+ * @param string $column
+ * @param string $order
+ * @return Sql
+ *
+ * @throws Kedavra
+ *
+ */
+ public function paginate($callable, int $page, int $limit,string $url='?page=',$column ='id',string $order =DESC) : Sql
+ {
+
+ $this->pagination = (new Pagination($page, $limit, $this->sum(),$url))->paginate();
+
+ $this->results = collect($this->take($limit, (($page) - 1) * $limit)->by($column,$order)->get())->for($callable)->join('');
+
+ return $this;
+ }
+
+ /**
+ *
+ * Get the pagination
+ *
+ * @return string
+ *
+ */
+ public function pagination(): string
+ {
+ return $this->pagination;
+ }
+
+
+ /**
+ *
+ * Get the primary key
+ *
+ * @throws Kedavra
+ *
+ * @return string
+ *
+ */
+ public function key()
+ {
+ return $this->primary_key();
+ }
+
+ /**
+ *
+ * Destroy record or records
+ *
+ * @return bool
+ *
+ * @throws Kedavra
+ *
+ */
+ public function destroy(): bool
+ {
+ $result = collect();
+ $id = $this->primary;
+
+ if (not_def($this->where))
+ {
+ foreach ($this->get() as $item)
+ {
+ $x = $item->$id;
+
+ $result->push($this->connect->execute("DELETE {$this->from} WHERE $id = $x" ));
+ }
+ }
+ $result->push($this->connect->execute("DELETE {$this->from} {$this->where}"));
+
+
+ return $result->ok();
+ }
+
+ /**
+ *
+ * Update columns value
+ *
+ * @param string $column
+ * @param $where
+ * @param string $expected
+ * @param $value
+ * @return bool
+ *
+ * @throws Kedavra
+ */
+ public function refresh(string $column, $where,string $expected, $value): bool
+ {
+ return $this->connect->execute("UPDATE {$this->table} SET $column = {$this->secure($value)} WHERE $where ={$this->secure($expected)}");
+ }
+
+ /**
+ *
+ * An union clause
+ *
+ * @param string $type
+ * @param string $first_table
+ * @param string $second_table
+ * @param string $first_column
+ * @param string $second_column
+ *
+ * @return Sql
+ *
+ * @throws Kedavra
+ *
+ */
+ public function union(string $type,string $first_table, string $second_table, string $first_column ='', string $second_column ='') : Sql
+ {
+ is_false(collect([UNION,UNION_ALL])->exist($type),true,"The type must be UNION or UNION_ALL");
+
+ $this->union = not_def($first_column, $second_column) ? "SELECT * FROM $first_table $type SELECT * FROM $second_table": "SELECT $first_column FROM $first_table $type SELECT $second_column FROM $second_table";
+
+ return $this;
+ }
+
+ /**
+ * @param string $type
+ * @param string $condition
+ * @param string $first_table
+ * @param string $second_table
+ * @param string $first_param
+ * @param string $second_param
+ * @param string ...$columns
+ *
+ * @return Sql
+ *
+ * @throws Kedavra
+ *
+ */
+ public function join(string $type,string $condition, string $first_table, string $second_table, string $first_param, string $second_param, string ...$columns): Sql
+ {
+ is_false(collect([INNER_JOIN,CROSS_JOIN,LEFT_JOIN,RIGHT_JOIN,FULL_JOIN])->exist($type),true,"The type must be INNER_JOIN or CROSS_JOIN or LEFT_JOIN or RIGHT_JOIN or FULL_JOIN");
+
+ $select = '';
+ $columns_define = def($columns);
+
+ if($columns_define)
+ {
+ $end = collect($columns)->last();
+ foreach($columns as $column)
+ different($column, $end) ? append($select, "$first_table.$column, $second_table.$column, ") : append($select, "$first_table.$column, $second_table.$column");
+ }
+
+ $select = $columns_define ? $select : '*';
+
+ $this->join = equal($type,CROSS_JOIN) ? "SELECT $select FROM $first_table CROSS JOIN $second_table" : "SELECT $select FROM $first_table $type $second_table ON $first_table.$first_param $condition $second_table.$second_param";
+
+ return $this;
+ }
+ /**
+ *
+ * Return the pagination
+ *
+ * @return string
+ *
+ */
+ public function results(): string
+ {
+ return $this->results;
+ }
+
+ /**
+ *
+ * Generate a where clause
+ *
+ * @param string $column
+ * @param string $condition
+ * @param $expected
+ *
+ * @return Sql
+ *
+ * @throws Kedavra
+ *
+ */
+ public function where(string $column,string $condition,$expected): Sql
+ {
+
+ $condition = html_entity_decode($condition);
+
+ $this->where = "WHERE $column $condition {$this->secure($expected)}";
+
+ return $this;
+ }
+
+ /**
+ *
+ * Order by clause
+ *
+ * @param string $column
+ * @param string $order
+ * @return Sql
+ *
+ * @throws Kedavra
+ *
+ */
+ public function by(string $column,string $order =DESC): Sql
+ {
+ $this->order = "ORDER BY {$this->secure($column)} $order";
+
+ return $this;
+ }
+
+ /**
+ *
+ * THe different clause
+ *
+ * @param string $column
+ * @param $expected
+ *
+ * @return Sql
+ *
+ * @throws Kedavra
+ *
+ */
+ public function different(string $column,$expected): Sql
+ {
+ return $this->where($column,DIFFERENT,$expected);
+ }
+
+
+ /**
+ *
+ * Select only columns
+ *
+ * @param string ...$columns
+ *
+ * @return Sql
+ *
+ */
+ public function only(string ...$columns): Sql
+ {
+ $this->columns = collect($columns)->join();
+ return $this;
+ }
+
+
+
+ /**
+ *
+ * limit clause
+ *
+ * @param int $limit
+ * @param int $offset
+ *
+ * @return Sql
+ *
+ */
+ public function take(int $limit,int $offset= 0): Sql
+ {
+ $this->limit = $this->connect->mysql() ? "LIMIT $offset,$limit" : "LIMIT $limit OFFSET $offset";
+
+ return $this;
+ }
+
+ /**
+ *
+ * Get the query result
+ *
+ * @return array|object
+ *
+ * @throws Kedavra
+ *
+ */
+ public function get()
+ {
+ return $this->use_fetch ? $this->connect->fetch($this->sql()) : $this->connect->request($this->sql());
+ }
+
+ /**
+ *
+ * Return the query generated
+ *
+ * @method sql
+ *
+ * @return string
+ *
+ */
+ public function sql() : string
+ {
+
+ $where = def($this->where) ? $this->where : '';
+ $order = def($this->order) ? $this->order : '';
+ $table = def($this->from) ? $this->from : '';
+ $limit = def($this->limit) ? $this->limit : '';
+ $join = def($this->join) ? $this->join : '';
+ $union = def($this->union) ? $this->union : '';
+ $or = def($this->or) ? $this->or : '';
+ $and = def($this->and) ? $this->and : '';
+ $columns = def($this->columns) ? $this->columns : "*";
+
+ if (def($union))
+ return "$union $where $and $or $order $limit ";
+ elseif (def($join))
+ return "$join $order $limit";
+
+ return "SELECT $columns $table $where $and $or $order $limit";
+ }
+
+
+ /**
+ *
+ * Generate an and clause
+ *
+ * @param string $column
+ * @param string $condition
+ * @param string $expected
+ *
+ * @return Sql
+ *
+ * @throws Kedavra
+ *
+ */
+ public function and(string $column, string $condition, string $expected) : Sql
+ {
+
+ append($this->and, " AND $column $condition {$this->secure($expected)}");
+
+ return $this;
+ }
+
+
+ /**
+ *
+ * Generate a or clause
+ *
+ * @param string $value
+ * @param string $condition
+ * @param string $expected
+ * @return Sql
+ *
+ * @throws Kedavra
+ *
+ */
+ public function or(string $value, string $condition, string $expected) : Sql
+ {
+ append($this->or, " OR $value $condition {$this->secure($expected)}");
+
+ return $this;
+ }
+
+ /**
+ *
+ * Disable or enable the fetch
+ *
+ *
+ * @method fetch
+ *
+ *
+ * @param bool $fetch
+ *
+ * @return Sql
+ */
+ public function fetch(bool $fetch = false) : Sql
+ {
+ $this->use_fetch = $fetch;
+
+ return $this;
+ }
+
+
+ /**
+ *
+ * Secure the string
+ *
+ * @param string $x
+ *
+ * @return string
+ *
+ * @throws Kedavra
+ */
+ private function secure(string $x): string
+ {
+ return $this->connect->pdo()->quote($x);
+ }
+
+ /**
+ *
+ * Get the sum of record
+ *
+ * @return int
+ *
+ * @throws Kedavra
+ *
+ */
+ public function sum(): int
+ {
+ return count($this->fetch()->get());
+ }
+
+ /**
+ *
+ * Found the primary key
+ *
+ * @throws Kedavra
+ *
+ * @return string
+ *
+ */
+ private function primary_key() : string
+ {
+
+ switch($this->connect->driver())
+ {
+ case MYSQL:
+ foreach($this->connect->request("show columns {$this->from} where `Key` = 'PRI';") as $key)
+ return $key->Field;
+ break;
+ case POSTGRESQL:
+ foreach($this->connect->request("select column_name FROM information_schema.key_column_usage WHERE table_name = '{$this->table}';") as $key)
+ return $key->column_name;
+ break;
+ case SQLITE:
+ foreach($this->connect->request("PRAGMA table_info({$this->table})") as $field)
+ {
+ if(def($field->pk))
+ return $field->name;
+ }
+ break;
+
+ }
+ throw new Kedavra('We have not found a primary key');
+ }
+ }
+} \ No newline at end of file
diff --git a/eywa/Debug/Dumper.php b/eywa/Debug/Dumper.php
new file mode 100644
index 0000000..ba2c64a
--- /dev/null
+++ b/eywa/Debug/Dumper.php
@@ -0,0 +1,40 @@
+<?php
+
+ namespace Eywa\Debug
+ {
+
+ use Symfony\Component\VarDumper\Cloner\VarCloner;
+ use Symfony\Component\VarDumper\Dumper\CliDumper;
+
+ /**
+ *
+ * Class Dumper
+ *
+ * @author Willy Micieli
+ *
+ * @package Imperium\Debug
+ *
+ * @license GPL
+ *
+ * @version 10
+ *
+ */
+ class Dumper
+ {
+
+ /**
+ * Dump a value with elegance.
+ *
+ * @param mixed $value
+ *
+ * @return void
+ */
+ public function dump($value)
+ {
+
+ $dumper = 'cli' === PHP_SAPI ? new CliDumper : new HtmlDumper;
+ $dumper->dump((new VarCloner)->cloneVar($value));
+ }
+
+ }
+ }
diff --git a/eywa/Debug/HtmlDumper.php b/eywa/Debug/HtmlDumper.php
new file mode 100644
index 0000000..1ae5715
--- /dev/null
+++ b/eywa/Debug/HtmlDumper.php
@@ -0,0 +1,28 @@
+<?php
+
+ namespace Eywa\Debug
+ {
+
+ use Symfony\Component\VarDumper\Dumper\HtmlDumper as SymfonyHtmlDumper;
+
+ /**
+ *
+ * Class HtmlDumper
+ *
+ * @author Willy Micieli
+ *
+ * @package Imperium\Debug
+ *
+ * @license GPL
+ *
+ * @version 10
+ *
+ */
+ class HtmlDumper extends SymfonyHtmlDumper
+ {
+
+ protected static $themes = [
+ 'dark' => [ 'default' => 'background:none; color:#222; line-height:1.2em; font:12px Monaco, Consolas, monospace; word-wrap: break-word; white-space: pre-wrap; position:relative; z-index:99999; word-break: break-all', 'ellipsis' => 'color:#CC7832', 'ns' => 'user-select:none;', 'num' => 'color:#a71d5d', 'const' => 'color:#795da3', 'str' => 'color:#df5000', 'cchr' => 'color:#222', 'note' => 'color:#a71d5d', 'ref' => 'color:#a0a0a0', 'public' => 'color:#795da3', 'protected' => 'color:#795da3', 'private' => 'color:#795da3', 'meta' => 'color:#795da3', 'key' => 'color:#df5000', 'index' => 'color:#a71d5d', ], ];
+
+ }
+ }
diff --git a/eywa/Html/Pagination.php b/eywa/Html/Pagination.php
new file mode 100644
index 0000000..88051a8
--- /dev/null
+++ b/eywa/Html/Pagination.php
@@ -0,0 +1,108 @@
+<?php
+
+ namespace Eywa\Html\Pagination
+ {
+
+ use Eywa\Exception\Kedavra;
+
+
+ /**
+ * Class Pagination
+ *
+ * @package Imperium\Html\Pagination
+ *
+ */
+ class Pagination
+ {
+
+ /**
+ * The current page
+ *
+ * @var int
+ *
+ */
+ private $current_page;
+
+ /**
+ * The per page limit
+ *
+ * @var int
+ *
+ */
+ private $limit;
+
+ /**
+ *
+ * The number of records
+ *
+ * @var int
+ *
+ */
+ private $total;
+
+ /**
+ *
+ * The number of pages
+ *
+ * @var float
+ *
+ */
+ private $pages;
+
+ /**
+ *
+ * The url
+ *
+ * @var string
+ *
+ */
+ private $url;
+
+ /**
+ *
+ * @param int $current_page
+ * @param int $limit
+ * @param int $total
+ * @param string $url
+ *
+ */
+ public function __construct(int $current_page, int $limit, int $total,string $url ='?page=')
+ {
+
+ $limit = $limit === 0 ? 1 : $limit;
+
+ $this->current_page = $current_page;
+
+ $this->limit = $limit;
+
+ $this->total = $total;
+
+ $this->pages = intval(ceil($total / $limit)) + 1;
+
+ $this->url = $url;
+
+ }
+
+ /**
+ * @return string
+ *
+ * @throws Kedavra
+ *
+ */
+ public function paginate() : string
+ {
+
+ if (superior_or_equal($this->limit,$this->total))
+ return '';
+
+ $html = '<ul class="' . config('pagination', 'ul_class') . '">';
+
+ for($i = 1; $i != $this->pages; $i++)
+ $i === $this->current_page ? append($html, '<li class="' . config('pagination', 'li_class') . ' active"><a href="' . $this->url . $i . '" class="' . config('pagination', 'link_class') . '">' . $i . '</a></li>') : append($html, '<li class="' . config('pagination', 'li_class') . '"><a href="' . $this->url . $i . '" class="' . config('pagination', 'link_class') . '">' . $i . '</a></li>');
+ append($html, '</ul>');
+
+ return $html;
+ }
+
+ }
+ } \ No newline at end of file
diff --git a/eywa/Http/Routing/Router.php b/eywa/Http/Routing/Router.php
index 8ca55f0..1c6b1f3 100644
--- a/eywa/Http/Routing/Router.php
+++ b/eywa/Http/Routing/Router.php
@@ -4,8 +4,8 @@
{
use Eywa\Exception\Kedavra;
- use Eywa\Http\Response\RedirectResponse;
use Psr\Http\Message\ServerRequestInterface;
+ use Symfony\Component\HttpFoundation\RedirectResponse;
/**
diff --git a/eywa/Testing/Unit.php b/eywa/Testing/Unit.php
index ecf14cc..89da9fe 100644
--- a/eywa/Testing/Unit.php
+++ b/eywa/Testing/Unit.php
@@ -3,10 +3,11 @@
namespace Eywa\Testing {
- use Exception;
+ use Eywa\Exception\Kedavra;
use Eywa\Http\Response\Response;
use PHPUnit\Framework\TestCase;
+
abstract class Unit extends TestCase
{
/**
@@ -19,8 +20,7 @@ namespace Eywa\Testing {
*
* @return Response
*
- * @throws Exception
- *
+ * @throws Kedavra
*/
public function response(?string $content ='' ,int $status = 200,array $headers = []): Response
{
diff --git a/helpers/admin.php b/helpers/admin.php
index 818b2d3..687a677 100644
--- a/helpers/admin.php
+++ b/helpers/admin.php
@@ -1,8 +1,12 @@
<?php
+use Eywa\Config\Config;
+use Eywa\Database\Connexion\Connect;
+use Eywa\Debug\Dumper;
use Eywa\Exception\Kedavra;
+use Symfony\Component\HttpFoundation\Request;
-if( ! function_exists('equal'))
+if (!function_exists('equal'))
{
/**
*
@@ -10,261 +14,324 @@ if( ! function_exists('equal'))
*
* @method equal
*
- * @param mixed $parameter
- * @param mixed $expected
- * @param bool $run_exception
- * @param string $message
+ * @param mixed $parameter
+ * @param mixed $expected
+ * @param bool $run_exception
+ * @param string $message
+ *
+ * @return bool
*
* @throws Kedavra
*
+ */
+ function equal($parameter, $expected, bool $run_exception = false, string $message = ''): bool
+ {
+
+ $x = strcmp($parameter, $expected) === 0;
+
+ is_true($x, $run_exception, $message);
+
+ return $x;
+ }
+}
+
+if (!function_exists('superior_or_equal'))
+{
+ /**
+ * @param $parameter
+ * @param int $expected
+ * @param bool $run_exception
+ * @param string $message
* @return bool
*
+ * @throws Kedavra
*/
- function equal($parameter, $expected, bool $run_exception = false, string $message = '') : bool
+ function superior_or_equal($parameter, int $expected, bool $run_exception = false, string $message = ''): bool
{
- $x = strcmp($parameter, $expected) === 0;
+ $x = is_array($parameter) ? count($parameter) >= $expected : $parameter >= $expected;
+
is_true($x, $run_exception, $message);
return $x;
}
- if( ! function_exists('is_true'))
+}
+if (!function_exists('is_true'))
+{
+ /**
+ *
+ * Check if data equal true
+ *
+ * @method is_true
+ *
+ * @param mixed $data
+ * @param bool $run_exception
+ * @param string $message
+ *
+ * @return bool
+ *
+ * @throws Kedavra
+ *
+ */
+ function is_true($data, bool $run_exception = false, string $message = ''): bool
{
- /**
- *
- * Check if data equal true
- *
- * @method is_true
- *
- * @param mixed $data
- * @param bool $run_exception
- * @param string $message
- *
- * @throws Kedavra
- *
- * @return bool
- *
- */
- function is_true($data, bool $run_exception = false, string $message = '') : bool
- {
-
- $x = $data === true;
-
- if($run_exception && $x)
- throw new Kedavra($message);
-
- return $x;
- }
+
+ $x = $data === true;
+
+ if ($run_exception && $x)
+ throw new Kedavra($message);
+
+ return $x;
}
- if( ! function_exists('different'))
+}
+if (!function_exists('different'))
+{
+ /**
+ *
+ * Check if values are different
+ *
+ * @method different
+ *
+ * @param mixed $parameter
+ * @param mixed $expected
+ * @param bool $run_exception
+ * @param string $message
+ *
+ * @return bool
+ *
+ * @throws Kedavra
+ *
+ */
+ function different($parameter, $expected, $run_exception = false, string $message = ''): bool
{
- /**
- *
- * Check if values are different
- *
- * @method different
- *
- * @param mixed $parameter
- * @param mixed $expected
- * @param bool $run_exception
- * @param string $message
- *
- * @throws Kedavra
- *
- * @return bool
- *
- */
- function different($parameter, $expected, $run_exception = false, string $message = '') : bool
- {
-
- $x = strcmp($parameter, $expected) !== 0;
-
- is_true($x, $run_exception, $message);
-
- return $x;
- }
+
+ $x = strcmp($parameter, $expected) !== 0;
+
+ is_true($x, $run_exception, $message);
+
+ return $x;
}
+}
- if( ! function_exists('is_false'))
+if (!function_exists('is_false'))
+{
+ /**
+ *
+ * Check if data equal false
+ *
+ * @method is_false
+ *
+ * @param mixed $data
+ * @param bool $run_exception
+ * @param string $message
+ *
+ * @return bool
+ *
+ * @throws Kedavra
+ *
+ */
+ function is_false($data, bool $run_exception = false, string $message = ''): bool
{
- /**
- *
- * Check if data equal false
- *
- * @method is_false
- *
- * @param mixed $data
- * @param bool $run_exception
- * @param string $message
- *
- * @throws Kedavra
- *
- * @return bool
- *
- */
- function is_false($data, bool $run_exception = false, string $message = '') : bool
- {
-
- $x = $data === false;
-
- is_true($x, $run_exception, $message);
-
- return $x;
- }
+
+ $x = $data === false;
+
+ is_true($x, $run_exception, $message);
+
+ return $x;
}
+}
- if( ! function_exists('is_not_true'))
+if (!function_exists('is_not_true'))
+{
+ /**
+ *
+ * Check if data is not equal to true
+ *
+ * @method is_not_true
+ *
+ * @param mixed $data
+ * @param bool $run_exception
+ * @param string $message
+ *
+ * @return bool
+ *
+ * @throws Kedavra
+ *
+ */
+ function is_not_true($data, bool $run_exception = false, string $message = ''): bool
{
- /**
- *
- * Check if data is not equal to true
- *
- * @method is_not_true
- *
- * @param mixed $data
- * @param bool $run_exception
- * @param string $message
- *
- * @throws Kedavra
- *
- * @return bool
- *
- */
- function is_not_true($data, bool $run_exception = false, string $message = '') : bool
- {
-
- $x = $data !== true;
-
- is_true($x, $run_exception, $message);
-
- return $x;
- }
+
+ $x = $data !== true;
+
+ is_true($x, $run_exception, $message);
+
+ return $x;
+ }
+}
+if (!function_exists('is_not_false'))
+{
+ /**
+ *
+ *
+ * Check if data is not equal to false
+ *
+ * @method is_not_false
+ *
+ * @param mixed $data
+ * @param bool $run_exception
+ * @param string $message
+ *
+ * @return bool
+ *
+ * @throws Kedavra
+ *
+ */
+ function is_not_false($data, bool $run_exception = false, string $message = ''): bool
+ {
+
+ $x = $data !== false;
+
+ is_true($x, $run_exception, $message);
+
+ return $x;
}
- if( ! function_exists('is_not_false'))
+}
+if (!function_exists('config'))
+{
+
+ /**
+ *
+ * Get a config value
+ *
+ * @param string $file
+ * @param $key
+ *
+ * @return mixed
+ *
+ * @throws Kedavra
+ *
+ */
+ function config(string $file,$key)
{
- /**
- *
- *
- * Check if data is not equal to false
- *
- * @method is_not_false
- *
- * @param mixed $data
- * @param bool $run_exception
- * @param string $message
- *
- * @throws Kedavra
- *
- * @return bool
- *
- */
- function is_not_false($data, bool $run_exception = false, string $message = '') : bool
- {
-
- $x = $data !== false;
-
- is_true($x, $run_exception, $message);
-
- return $x;
- }
+ return (new Config($file,$key))->value();
}
- if( ! function_exists('equal'))
+}
+
+if (!function_exists('def'))
+{
+ /**
+ *
+ * Check if all values are define
+ *
+ * @method def
+ *
+ * @param mixed $values
+ *
+ * @return bool
+ *
+ */
+ function def(...$values): bool
{
- /**
- *
- * Check if variables are equals
- *
- * @method equal
- *
- * @param mixed $parameter
- * @param mixed $expected
- * @param bool $run_exception
- * @param string $message
- *
- * @throws Kedavra
- *
- * @return bool
- *
- */
- function equal($parameter, $expected, bool $run_exception = false, string $message = '') : bool
- {
-
- $x = strcmp($parameter, $expected) === 0;
- is_true($x, $run_exception, $message);
-
- return $x;
+
+ foreach ($values as $value) {
+ if (!isset($value) || empty($value))
+ return false;
}
+
+ return true;
}
+}
- if( ! function_exists('def'))
+
+if (!function_exists('not_def'))
+{
+ /**
+ *
+ * Check if all values are not define
+ *
+ * @method not_def
+ *
+ * @param mixed $values
+ *
+ * @return bool
+ *
+ */
+ function not_def(...$values): bool
{
- /**
- *
- * Check if all values are define
- *
- * @method def
- *
- * @param mixed $values
- *
- * @return bool
- *
- */
- function def(...$values) : bool
- {
-
- foreach($values as $value)
- {
- if( ! isset($value) || empty($value))
- return false;
- }
-
- return true;
- }
+
+ foreach ($values as $value)
+ if (def($value))
+ return false;
+
+ return true;
}
+}
- if( ! function_exists('not_def'))
+if (!function_exists('connect'))
+{
+ /**
+ *
+ * Connect to the database
+ *
+ * @param string $driver
+ * @param string $base
+ * @param string $username
+ * @param string $password
+ * @param string $host
+ * @param string $dump
+ *
+ * @return Connect
+ *
+ */
+ function connect(string $driver,string $base,string $username ='',string $password = '',string $host = LOCALHOST ,string $dump ='dump'): Connect
{
- /**
- *
- * Check if all values are not define
- *
- * @method not_def
- *
- * @param mixed $values
- *
- * @return bool
- *
- */
- function not_def(...$values) : bool
- {
-
- foreach($values as $value)
- if(def($value))
- return false;
-
- return true;
- }
+ return new Connect($driver,$base,$username,$password,$host,$dump);
+ }
+}
+if (!function_exists('d'))
+{
+ function d(...$args)
+ {
+ foreach ($args as $arg)
+ (new Dumper())->dump($arg);
+
+ die();
}
+}
+if (!function_exists('base'))
+{
+ /**
+ *
+ * Get absolute path
+ *
+ * @param string $dir
+ *
+ * @return string
+ *
+ */
+ function base(string $dir = ''): string
+ {
+
+ if (def($dir))
+ return def(request()->server->get('DOCUMENT_ROOT')) ? dirname(request()->server->get('DOCUMENT_ROOT')) . DIRECTORY_SEPARATOR . $dir : request()->server->get('PWD') . DIRECTORY_SEPARATOR . $dir;
- if( ! function_exists('base'))
+ return def(request()->server->get('DOCUMENT_ROOT')) ? dirname(request()->server->get('DOCUMENT_ROOT')) : request()->server->get('PWD');
+ }
+}
+
+
+if (!function_exists('request'))
+{
+ /**
+ *
+ * Request
+ *
+ * @return Request
+ *
+ */
+ function request(): Request
{
- /**
- *
- * Get absolute path
- *
- * @param string $dir
- *
- * @return string
- *
- */
- function base(string $dir = '') : string
- {
-
- if(def($dir))
- return def(request()->server->get('DOCUMENT_ROOT')) ? dirname(request()->server->get('DOCUMENT_ROOT')) . DIRECTORY_SEPARATOR . $dir : request()->server->get('PWD') . DIRECTORY_SEPARATOR . $dir;
-
- return def(request()->server->get('DOCUMENT_ROOT')) ? dirname(request()->server->get('DOCUMENT_ROOT')) : request()->server->get('PWD');
- }
+ return Request::createFromGlobals();
}
-} \ No newline at end of file
+}
diff --git a/helpers/const.php b/helpers/const.php
index a19887f..6c9ca16 100644
--- a/helpers/const.php
+++ b/helpers/const.php
@@ -35,14 +35,14 @@ define('POSTGRESQL', 'pgsql');
define('SQLITE', 'sqlite');
define('UNION', 'UNION');
-define('UNION_ALL', 'UNION_ALL');
-define('INNER_JOIN', 'INNER_JOIN');
-define('CROSS_JOIN', 'CROSS_JOIN');
-define('LEFT_JOIN', 'LEFT_JOIN');
-define('RIGHT_JOIN', 'RIGHT_JOIN');
-define('FULL_JOIN', 'FULL_JOIN');
-define('SELF_JOIN', 'SELF_JOIN');
-define('NATURAL_JOIN', 'NATURAL_JOIN');
+define('UNION_ALL', 'UNION ALL');
+define('INNER_JOIN', 'INNER JOIN');
+define('CROSS_JOIN', 'CROSS JOIN');
+define('LEFT_JOIN', 'LEFT JOIN');
+define('RIGHT_JOIN', 'RIGHT JOIN');
+define('FULL_JOIN', 'FULL JOIN');
+define('SELF_JOIN', 'SELF JOIN');
+define('NATURAL_JOIN', 'NATURAL JOIN');
define('SELECT', 'SELECT');
define('UPDATE', 'UPDATE');
define('INSERT', 'INSERT');
diff --git a/helpers/web.php b/helpers/web.php
index 6926412..301ed91 100644
--- a/helpers/web.php
+++ b/helpers/web.php
@@ -17,4 +17,29 @@ if (!function_exists('collect'))
{
return new Collect($data);
}
+}
+
+if (!function_exists('append'))
+{
+
+ /**
+ *
+ * Append contents to the variable
+ *
+ * @method append
+ *
+ * @param mixed $variable
+ * @param mixed $contents
+ *
+ * @return void
+ *
+ */
+ function append(&$variable, ...$contents): void
+ {
+
+ foreach ($contents as $content)
+ $variable .= $content;
+
+ }
+
} \ No newline at end of file
diff --git a/tests/Helpers/AdminHelpersTest.php b/tests/Helpers/AdminHelpersTest.php
index dd37f50..59df687 100644
--- a/tests/Helpers/AdminHelpersTest.php
+++ b/tests/Helpers/AdminHelpersTest.php
@@ -18,13 +18,29 @@ class AdminHelpersTest extends Unit
}
+ public function test_is_not_true()
+ {
+ $this->assertFalse(is_not_true(true));
+ $this->assertTrue(is_not_true(false));
+ }
+
+ public function test_is_not_false()
+ {
+ $this->assertTrue(is_not_false(true));
+ $this->assertFalse(is_not_false(false));
+ }
+
public function test_equal()
{
- $a = 'a';
- $b = 'b';
- $c = 'a';
- $this->assertTrue(equal($a,$c));
- $this->assertFalse(equal($a,$b));
+ $this->assertTrue(equal('a','a'));
+ $this->assertFalse(equal('a','aa'));
+ }
+
+ public function test_base()
+ {
+ $this->assertNotEmpty(base('eywa'));
+ $this->assertNotEmpty(base('web'));
+ $this->assertNotEmpty(base(''));
}
public function test_different()
{
@@ -34,4 +50,6 @@ class AdminHelpersTest extends Unit
$this->assertFalse(different($a,$c));
$this->assertTrue(different($a,$b));
}
+
+
} \ No newline at end of file
diff --git a/tests/Http/ResponseTest.php b/tests/Http/ResponseTest.php
index fa9dd8a..44f808b 100644
--- a/tests/Http/ResponseTest.php
+++ b/tests/Http/ResponseTest.php
@@ -20,6 +20,7 @@ class ResponseTest extends Unit
$this->assertEquals(500,$this->response('a',500)->send()->status());
}
+
public function test_header()
{
diff --git a/tests/Sql/SqlTest.php b/tests/Sql/SqlTest.php
new file mode 100644
index 0000000..539a4a3
--- /dev/null
+++ b/tests/Sql/SqlTest.php
@@ -0,0 +1,145 @@
+<?php
+
+
+use Eywa\Database\Query\Sql;
+use Eywa\Exception\Kedavra;
+use PHPUnit\Framework\TestCase;
+
+class SqlTest extends TestCase
+{
+
+ /**
+ * @var Sql
+ */
+ private $sql;
+
+ /**
+ * @throws Kedavra
+ */
+ public function setUp(): void
+ {
+ $this->sql = new Sql(connect('mysql','pandora','root','root'),'articles');
+ }
+
+ public function tearDown(): void
+ {
+ system("mysql -uroot -proot pandora < app.sql");
+ }
+
+ public function test_union()
+ {
+ $this->assertNotEmpty($this->sql->union(UNION,'articles','articles')->get());
+ $this->assertNotEmpty($this->sql->union(UNION_ALL,'articles','articles')->get());
+ }
+
+ public function test_join()
+ {
+ $this->assertNotEmpty($this->sql->join(LEFT_JOIN,EQUAL,'articles','courses','id','id')->get());
+ $this->assertNotEmpty($this->sql->join(RIGHT_JOIN,EQUAL,'articles','courses','id','id')->get());
+ $this->assertNotEmpty($this->sql->join(CROSS_JOIN,EQUAL,'articles','courses','id','id')->get());
+ $this->assertNotEmpty($this->sql->join(INNER_JOIN,EQUAL,'articles','courses','id','id')->get());
+
+ $this->assertNotEmpty($this->sql->join(LEFT_JOIN,EQUAL,'articles','courses','id','id','id')->get());
+ $this->assertNotEmpty($this->sql->join(RIGHT_JOIN,EQUAL,'articles','courses','id','id','id')->get());
+ $this->assertNotEmpty($this->sql->join(CROSS_JOIN,EQUAL,'articles','courses','id','id','id')->get());
+ $this->assertNotEmpty($this->sql->join(INNER_JOIN,EQUAL,'articles','courses','id','id','id')->get());
+
+
+ }
+ public function test_get()
+ {
+ $this->assertNotEmpty($this->sql->get());
+ $this->assertNotEmpty($this->sql->where('id',EQUAL,1)->get());
+ $this->assertNotEmpty($this->sql->where('id',DIFFERENT,1)->get());
+ $this->assertNotEmpty($this->sql->where('id',SUPERIOR_OR_EQUAL,1)->get());
+ $this->assertNotEmpty($this->sql->where('id',SUPERIOR,1)->get());
+ $this->assertEmpty($this->sql->where('id',INFERIOR,1)->get());
+ $this->assertNotEmpty($this->sql->where('id',INFERIOR_OR_EQUAL,1)->get());
+ }
+
+ public function test_get_limit()
+ {
+ $this->assertNotEmpty($this->sql->take(5)->get());
+ $this->assertNotEmpty($this->sql->take(5,1)->get());
+ }
+
+ public function test_get_destroy()
+ {
+ $this->assertTrue($this->sql->destroy());
+
+ }
+
+ public function test_destroy_by_where()
+ {
+ $this->assertTrue($this->sql->where('id',INFERIOR_OR_EQUAL,5)->destroy());
+ for ($i=1;$i<5;$i++)
+ $this->assertEmpty($this->sql->where('id',EQUAL,$i)->get());
+ }
+
+ public function test_only()
+ {
+ $this->assertEquals("SELECT slug FROM articles WHERE id <= '5' ",$this->sql->where('id',INFERIOR_OR_EQUAL,5)->only('slug')->sql());
+ $this->assertEquals("SELECT slug,title FROM articles WHERE id <= '5' ",$this->sql->where('id',INFERIOR_OR_EQUAL,5)->only('slug','title')->sql());
+
+ $this->assertNotEmpty($this->sql->only('title')->get());
+
+ }
+ public function test_different()
+ {
+ $x = function ($item){return $item->id ;};
+ $this->assertNotEmpty($this->sql->different('id',4)->get());
+ $this->assertStringContainsString('4',$this->sql->different('id',4)->paginate($x,1,20)->results());
+ }
+
+ public function test_refresh()
+ {
+ $this->assertTrue($this->sql->refresh('title','id',4,'A title'));
+ $this->assertEquals('A title',$this->sql->where('id',EQUAL,4)->fetch(true)->only('title')->get()->title);
+ }
+ public function test_sql()
+ {
+ $this->assertNotEmpty($this->sql->sql());
+
+ $this->assertStringContainsString('*',$this->sql->sql());
+
+ $this->assertNotEmpty($this->sql->where('id','=',4)->sql());
+
+ $this->assertStringContainsString("id = '4'",$this->sql->where('id','=',4)->sql());
+
+ $this->assertNotEmpty($this->sql->where('id','=',4)->only('id')->sql());
+
+ $this->assertNotEmpty($this->sql->where('id','=',4)->only('id')->and('id',DIFFERENT,4)->sql());
+
+ $this->assertStringContainsString('AND',$this->sql->where('id','=',4)->only('id')->and('id',DIFFERENT,4)->sql());
+
+ $this->assertStringContainsString(',',$this->sql->where('id','=',4)->only('id','title')->and('id',DIFFERENT,4)->sql());
+
+ $this->assertStringContainsString('OR',$this->sql->where('id','=',4)->only('id')->or('id',EQUAL,5)->sql());
+
+ $this->assertNotEmpty($this->sql->where('id','=',4)->or('id',EQUAL,5)->get());
+ }
+
+ public function test_sum()
+ {
+ $this->assertIsInt($this->sql->sum());
+ $this->assertEquals(31,$this->sql->sum());
+ }
+
+ public function test_paginate()
+ {
+ $x = function ($item){return $item->id ;};
+
+ $y = $this->sql->paginate($x,1,20);
+ $this->assertNotEmpty($y->pagination());
+ $this->assertNotEmpty($y->results());
+
+ $y = $this->sql->paginate($x,1,200000);
+ $this->assertEmpty($y->pagination());
+ $this->assertNotEmpty($y->results());
+
+ }
+ public function test_primary_key()
+ {
+ $this->assertEquals('id',$this->sql->key());
+ }
+} \ No newline at end of file