Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
Snaju Open Source Software
inception
Commits
72f579d2
Commit
72f579d2
authored
3 years ago
by
Othneil Drew
Browse files
Options
Download
Email Patches
Plain Diff
Update: add unique rule to validator
parent
81cd8eec
master
cell
v1.1.0
v1.0.17
v1.0.16
v0.1.15
v0.1.14
v0.1.13
v0.1.12
v0.1.11
v0.1.10
v0.1.9
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
Inception/Http/Request/Validators/Rules/UniqueRule.php
+72
-0
Inception/Http/Request/Validators/Rules/UniqueRule.php
Inception/Http/Request/Validators/Validator.php
+2
-0
Inception/Http/Request/Validators/Validator.php
with
74 additions
and
0 deletions
+74
-0
Inception/Http/Request/Validators/Rules/UniqueRule.php
0 → 100644
View file @
72f579d2
<?php
namespace
Snaju\Inception\Http\Request\Validators\Rules
;
use
Snaju\Inception\Exception\InvalidArgumentException
;
use
Snaju\Inception\Http\Request\Validators\ValidatorRule
;
use
Snaju\Inception\ORM\DataModel
;
use
Snaju\Inception\ORM\ORM
;
class
UniqueRule
extends
ValidatorRule
{
public
function
isValid
(
$dataArray
):
bool
{
$valid
=
$this
->
optional
;
$bodyData
=
$dataArray
[
'body'
];
if
(
array_key_exists
(
$this
->
field
,
$bodyData
))
{
$count
=
0
;
$parts
=
explode
(
','
,
$this
->
ruleValue
);
$tableName
=
$parts
[
0
];
// If namespace is provided, get the table name from the class
if
(
str_contains
(
$this
->
ruleValue
,
'\\'
))
{
$namespace
=
$parts
[
0
];
$class
=
new
$namespace
;
if
(
!
$class
instanceof
DataModel
)
{
throw
new
InvalidArgumentException
(
"
$namespace
is not a valid namespace. Model must extend Snaju\Inception\ORM\DataModal"
);
}
$tableName
=
ORM
::
getEntityManager
()
->
getClassMetadata
(
$namespace
)
->
getTableName
();
}
// Get list of table names and ensure a correct table name is used
$sm
=
ORM
::
getEntityManager
()
->
getConnection
()
->
getSchemaManager
();
$tables
=
array_map
(
function
(
$table
)
{
return
$table
->
getname
();
},
$sm
->
listTables
());
if
(
!
in_array
(
$tableName
,
$tables
))
{
throw
new
InvalidArgumentException
(
"`
$tableName
` is not a valid table name. Check model to ensure correct table name is used."
);
}
// Create and run the query to check the count
$columnName
=
isset
(
$parts
[
1
])
?
$parts
[
1
]
:
$this
->
field
;
$q
=
"SELECT COUNT(*) FROM
$tableName
WHERE
$columnName
= '
{
$bodyData
[
$this
->
field
]
}
'"
;
$stmt
=
ORM
::
getEntityManager
()
->
getConnection
()
->
prepare
(
$q
);
$count
=
(
int
)
$stmt
->
executeQuery
()
->
fetchNumeric
()[
0
];
if
(
$count
<=
0
)
{
$valid
=
true
;
}
else
{
$valid
=
false
;
}
}
return
$valid
;
}
public
function
getErrorMessage
():
string
{
return
"`
{
$this
->
field
}
` must be unique. Another record has the same value for
{
$this
->
field
}
."
;
}
public
function
stopOnError
():
bool
{
return
false
;
}
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Inception/Http/Request/Validators/Validator.php
View file @
72f579d2
...
...
@@ -19,6 +19,7 @@ use Snaju\Inception\Http\Request\Validators\Rules\RequiredRule;
use
Snaju\Inception\Http\Request\Validators\Rules\StringRule
;
use
Snaju\Inception\Exception\InvalidArgumentException
;
use
Snaju\Inception\Exception\InvalidKeyException
;
use
Snaju\Inception\Http\Request\Validators\Rules\UniqueRule
;
use
Snaju\Inception\Http\Request\WebRequest
;
use
Snaju\Inception\InceptionCore
;
use
Snaju\Inception\Util\Loaders\Collection
;
...
...
@@ -61,6 +62,7 @@ class Validator implements ValidatorInterface
'present'
=>
PresentRule
::
class
,
// should be first in validator string, bails if fail
'required'
=>
RequiredRule
::
class
,
// should be first in validator string, bails if fail
'string'
=>
StringRule
::
class
,
'unique'
=>
UniqueRule
::
class
,
]);
$this
->
rules
=
$rules
;
...
...
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment
Menu
Projects
Groups
Snippets
Help