Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
MaarchCourrier
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Redmine
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Harbor Registry
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Maarch
MaarchCourrier
Commits
ba41d2aa
Commit
ba41d2aa
authored
7 years ago
by
Florian Azizian
Browse files
Options
Downloads
Plain Diff
Merge branch 'develop' of labs.maarch.org:maarch/MaarchCourrier into develop
parents
d7d83c63
2997ffde
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
core/Models/DatabaseModel.php
+206
-0
206 additions, 0 deletions
core/Models/DatabaseModel.php
core/Models/ValidatorModel.php
+12
-6
12 additions, 6 deletions
core/Models/ValidatorModel.php
with
218 additions
and
6 deletions
core/Models/DatabaseModel.php
0 → 100644
+
206
−
0
View file @
ba41d2aa
<?php
/**
* Copyright Maarch since 2008 under licence GPLv3.
* See LICENCE.txt file at the root folder for more details.
* This file is part of Maarch software.
*
*/
/**
* @brief Database Model
* @author dev@maarch.org
* @ingroup core
*/
namespace
Core\Models
;
require_once
'core/class/class_db_pdo.php'
;
class
DatabaseModel
{
/**
* Database Select Function
* @param array $args
* @throws \Exception if number of tables is different from number of joins
*
* @return array
*/
public
static
function
select
(
array
$args
)
{
ValidatorModel
::
notEmpty
(
$args
,
[
'select'
,
'table'
]);
ValidatorModel
::
arrayType
(
$args
,
[
'select'
,
'table'
]);
$tmpTable
=
$args
[
'table'
];
$args
[
'table'
]
=
$args
[
'table'
][
0
];
if
(
!
empty
(
$args
[
'left_join'
]))
{
ValidatorModel
::
arrayType
(
$args
,
[
'left_join'
]);
if
(
count
(
$tmpTable
)
-
1
!=
count
(
$args
[
'left_join'
]))
{
throw
new
\Exception
(
'Number of tables doesn\'t match with number of joins'
);
}
$i
=
1
;
foreach
(
$args
[
'left_join'
]
as
$value
)
{
$args
[
'table'
]
.
=
" LEFT JOIN
{
$tmpTable
[
$i
]
}
ON
{
$value
}
"
;
$i
++
;
}
}
$select
=
implode
(
', '
,
$args
[
'select'
]);
if
(
empty
(
$args
[
'where'
]))
{
$args
[
'where'
]
=
[];
$where
=
''
;
}
else
{
ValidatorModel
::
arrayType
(
$args
,
[
'where'
]);
$where
=
' WHERE '
.
implode
(
' AND '
,
$args
[
'where'
]);
}
if
(
empty
(
$args
[
'group_by'
]))
{
$groupBy
=
''
;
}
else
{
ValidatorModel
::
arrayType
(
$args
,
[
'group_by'
]);
$groupBy
=
' GROUP BY '
.
implode
(
', '
,
$args
[
'group_by'
]);
}
if
(
empty
(
$args
[
'order_by'
]))
{
$orderBy
=
''
;
}
else
{
ValidatorModel
::
arrayType
(
$args
,
[
'order_by'
]);
$orderBy
=
' ORDER BY '
.
implode
(
', '
,
$args
[
'order_by'
]);
}
if
(
empty
(
$args
[
'limit'
]))
{
$limit
=
''
;
}
else
{
ValidatorModel
::
intType
(
$args
,
[
'limit'
]);
$limit
=
$args
[
'limit'
];
}
if
(
empty
(
$args
[
'data'
]))
{
$args
[
'data'
]
=
[];
}
ValidatorModel
::
arrayType
(
$args
,
[
'data'
]);
$db
=
new
\Database
();
if
(
!
empty
(
$limit
))
{
$where
=
implode
(
' AND '
,
$args
[
'where'
]);
$query
=
$db
->
limit_select
(
0
,
$limit
,
$select
,
$args
[
'table'
],
$where
,
$groupBy
,
''
,
$orderBy
);
}
else
{
$query
=
"SELECT
{
$select
}
FROM
{
$args
[
'table'
]
}
{
$where
}
{
$groupBy
}
{
$orderBy
}
"
;
}
$stmt
=
$db
->
query
(
$query
,
$args
[
'data'
]);
$rowset
=
[];
while
(
$row
=
$stmt
->
fetch
(
\PDO
::
FETCH_ASSOC
))
{
$rowset
[]
=
$row
;
}
return
$rowset
;
}
/**
* Database Insert Into Function
* @param array $args
*
* @return bool
*/
public
static
function
insert
(
array
$args
)
{
ValidatorModel
::
notEmpty
(
$args
,
[
'table'
,
'columnsValues'
]);
ValidatorModel
::
stringType
(
$args
,
[
'table'
]);
ValidatorModel
::
arrayType
(
$args
,
[
'columnsValues'
]);
$data
=
[];
$columnsQuery
=
[];
$valuesQuery
=
[];
foreach
(
$args
[
'columnsValues'
]
as
$key
=>
$value
)
{
if
(
$value
==
'SYSDATE'
||
$value
==
'CURRENT_TIMESTAMP'
)
{
$valuesQuery
[]
=
$value
;
}
else
{
$valuesQuery
[]
=
'?'
;
$data
[]
=
$value
;
}
$columnsQuery
[]
=
$key
;
}
$columns
=
implode
(
', '
,
$columnsQuery
);
$values
=
implode
(
', '
,
$valuesQuery
);
$query
=
"INSERT INTO
{
$args
[
'table'
]
}
(
{
$columns
}
) VALUES (
{
$values
}
)"
;
$db
=
new
\Database
();
$db
->
query
(
$query
,
$data
);
if
(
!
empty
(
$args
[
'getLastId'
]))
{
return
$db
->
lastInsertId
(
$args
[
'getLastId'
]);
}
return
true
;
}
/**
* Database Update Function
* @param array $args
*
* @return bool
*/
public
static
function
update
(
array
$args
)
{
ValidatorModel
::
notEmpty
(
$args
,
[
'table'
,
'set'
,
'where'
]);
ValidatorModel
::
stringType
(
$args
,
[
'table'
]);
ValidatorModel
::
arrayType
(
$args
,
[
'set'
,
'where'
]);
if
(
empty
(
$args
[
'data'
]))
{
$args
[
'data'
]
=
[];
}
ValidatorModel
::
arrayType
(
$args
,
[
'data'
]);
$querySet
=
[];
$dataSet
=
[];
foreach
(
$args
[
'set'
]
as
$key
=>
$value
)
{
$querySet
[]
=
"
{
$key
}
= ?"
;
$dataSet
[]
=
$value
;
}
$args
[
'data'
]
=
array_merge
(
$dataSet
,
$args
[
'data'
]);
$set
=
implode
(
', '
,
$querySet
);
$where
=
implode
(
' AND '
,
$args
[
'where'
]);
$query
=
"UPDATE
{
$args
[
'table'
]
}
SET
{
$set
}
WHERE
{
$where
}
"
;
$db
=
new
\Database
();
$db
->
query
(
$query
,
$args
[
'data'
]);
return
true
;
}
/**
* Database Delete From Function
* @param array $args
*
* @return bool
*/
public
static
function
delete
(
array
$args
)
{
ValidatorModel
::
notEmpty
(
$args
,
[
'table'
,
'where'
]);
ValidatorModel
::
stringType
(
$args
,
[
'table'
]);
ValidatorModel
::
arrayType
(
$args
,
[
'where'
]);
if
(
empty
(
$args
[
'data'
]))
{
$args
[
'data'
]
=
[];
}
ValidatorModel
::
arrayType
(
$args
,
[
'data'
]);
$where
=
implode
(
' AND '
,
$args
[
'where'
]);
$query
=
"DELETE FROM
{
$args
[
'table'
]
}
WHERE
{
$where
}
"
;
$db
=
new
\Database
();
$db
->
query
(
$query
,
$args
[
'data'
]);
return
true
;
}
}
This diff is collapsed.
Click to expand it.
core/Models/ValidatorModel.php
+
12
−
6
View file @
ba41d2aa
...
@@ -19,7 +19,8 @@ use Respect\Validation\Validator;
...
@@ -19,7 +19,8 @@ use Respect\Validation\Validator;
class
ValidatorModel
class
ValidatorModel
{
{
public
static
function
notEmpty
(
array
$aArgs
,
$aKeys
)
{
public
static
function
notEmpty
(
array
$aArgs
,
$aKeys
)
{
if
(
!
Validator
::
arrayType
()
->
notEmpty
()
->
validate
(
$aArgs
))
{
if
(
!
Validator
::
arrayType
()
->
notEmpty
()
->
validate
(
$aArgs
))
{
throw
new
\Exception
(
'First argument must be a non empty array'
);
throw
new
\Exception
(
'First argument must be a non empty array'
);
}
}
...
@@ -30,7 +31,8 @@ class ValidatorModel
...
@@ -30,7 +31,8 @@ class ValidatorModel
}
}
}
}
public
static
function
intVal
(
array
$aArgs
,
$aKeys
)
{
public
static
function
intVal
(
array
$aArgs
,
$aKeys
)
{
if
(
!
Validator
::
arrayType
()
->
notEmpty
()
->
validate
(
$aArgs
))
{
if
(
!
Validator
::
arrayType
()
->
notEmpty
()
->
validate
(
$aArgs
))
{
throw
new
\Exception
(
'First argument must be a non empty array'
);
throw
new
\Exception
(
'First argument must be a non empty array'
);
}
}
...
@@ -44,7 +46,8 @@ class ValidatorModel
...
@@ -44,7 +46,8 @@ class ValidatorModel
}
}
}
}
public
static
function
intType
(
array
$aArgs
,
$aKeys
)
{
public
static
function
intType
(
array
$aArgs
,
$aKeys
)
{
if
(
!
Validator
::
arrayType
()
->
notEmpty
()
->
validate
(
$aArgs
))
{
if
(
!
Validator
::
arrayType
()
->
notEmpty
()
->
validate
(
$aArgs
))
{
throw
new
\Exception
(
'First argument must be a non empty array'
);
throw
new
\Exception
(
'First argument must be a non empty array'
);
}
}
...
@@ -58,7 +61,8 @@ class ValidatorModel
...
@@ -58,7 +61,8 @@ class ValidatorModel
}
}
}
}
public
static
function
stringType
(
array
$aArgs
,
$aKeys
)
{
public
static
function
stringType
(
array
$aArgs
,
$aKeys
)
{
if
(
!
Validator
::
arrayType
()
->
notEmpty
()
->
validate
(
$aArgs
))
{
if
(
!
Validator
::
arrayType
()
->
notEmpty
()
->
validate
(
$aArgs
))
{
throw
new
\Exception
(
'First argument must be a non empty array'
);
throw
new
\Exception
(
'First argument must be a non empty array'
);
}
}
...
@@ -72,7 +76,8 @@ class ValidatorModel
...
@@ -72,7 +76,8 @@ class ValidatorModel
}
}
}
}
public
static
function
arrayType
(
array
$aArgs
,
$aKeys
)
{
public
static
function
arrayType
(
array
$aArgs
,
$aKeys
)
{
if
(
!
Validator
::
arrayType
()
->
notEmpty
()
->
validate
(
$aArgs
))
{
if
(
!
Validator
::
arrayType
()
->
notEmpty
()
->
validate
(
$aArgs
))
{
throw
new
\Exception
(
'First argument must be a non empty array'
);
throw
new
\Exception
(
'First argument must be a non empty array'
);
}
}
...
@@ -86,7 +91,8 @@ class ValidatorModel
...
@@ -86,7 +91,8 @@ class ValidatorModel
}
}
}
}
public
static
function
boolType
(
array
$aArgs
,
$aKeys
)
{
public
static
function
boolType
(
array
$aArgs
,
$aKeys
)
{
if
(
!
Validator
::
arrayType
()
->
notEmpty
()
->
validate
(
$aArgs
))
{
if
(
!
Validator
::
arrayType
()
->
notEmpty
()
->
validate
(
$aArgs
))
{
throw
new
\Exception
(
'First argument must be a non empty array'
);
throw
new
\Exception
(
'First argument must be a non empty array'
);
}
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment