Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
L
laravel-mongodb
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
sinan
laravel-mongodb
Commits
1ad438ec
Commit
1ad438ec
authored
May 21, 2017
by
Curos
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add DatabasePresenceVerifier and tests
parent
3276bac2
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
116 additions
and
0 deletions
+116
-0
DatabasePresenceVerifier.php
...enssegers/Mongodb/Validation/DatabasePresenceVerifier.php
+54
-0
ValidationServiceProvider.php
...nssegers/Mongodb/Validation/ValidationServiceProvider.php
+13
-0
TestCase.php
tests/TestCase.php
+1
-0
ValidationTest.php
tests/ValidationTest.php
+48
-0
No files found.
src/Jenssegers/Mongodb/Validation/DatabasePresenceVerifier.php
0 → 100644
View file @
1ad438ec
<?php
namespace
Jenssegers\Mongodb\Validation
;
class
DatabasePresenceVerifier
extends
\Illuminate\Validation\DatabasePresenceVerifier
{
/**
* Count the number of objects in a collection having the given value.
*
* @param string $collection
* @param string $column
* @param string $value
* @param int $excludeId
* @param string $idColumn
* @param array $extra
* @return int
*/
public
function
getCount
(
$collection
,
$column
,
$value
,
$excludeId
=
null
,
$idColumn
=
null
,
array
$extra
=
[])
{
$query
=
$this
->
table
(
$collection
)
->
where
(
$column
,
'regex'
,
"/
$value
/i"
);
if
(
!
is_null
(
$excludeId
)
&&
$excludeId
!=
'NULL'
)
{
$query
->
where
(
$idColumn
?:
'id'
,
'<>'
,
$excludeId
);
}
foreach
(
$extra
as
$key
=>
$extraValue
)
{
$this
->
addWhere
(
$query
,
$key
,
$extraValue
);
}
return
$query
->
count
();
}
/**
* Count the number of objects in a collection with the given values.
*
* @param string $collection
* @param string $column
* @param array $values
* @param array $extra
* @return int
*/
public
function
getMultiCount
(
$collection
,
$column
,
array
$values
,
array
$extra
=
[])
{
foreach
(
$values
as
&
$value
)
{
$value
=
new
\MongoRegex
(
"/
$value
/i"
);
}
$query
=
$this
->
table
(
$collection
)
->
whereIn
(
$column
,
$values
);
foreach
(
$extra
as
$key
=>
$extraValue
)
{
$this
->
addWhere
(
$query
,
$key
,
$extraValue
);
}
return
$query
->
count
();
}
}
src/Jenssegers/Mongodb/Validation/ValidationServiceProvider.php
0 → 100644
View file @
1ad438ec
<?php
namespace
Jenssegers\Mongodb\Validation
;
use
Illuminate\Validation\ValidationServiceProvider
as
BaseProvider
;
class
ValidationServiceProvider
extends
BaseProvider
{
protected
function
registerPresenceVerifier
()
{
$this
->
app
->
singleton
(
'validation.presence'
,
function
(
$app
)
{
return
new
DatabasePresenceVerifier
(
$app
[
'db'
]);
});
}
}
\ No newline at end of file
tests/TestCase.php
View file @
1ad438ec
...
...
@@ -29,6 +29,7 @@ class TestCase extends Orchestra\Testbench\TestCase
return
[
Jenssegers\Mongodb\MongodbServiceProvider
::
class
,
Jenssegers\Mongodb\Auth\PasswordResetServiceProvider
::
class
,
Jenssegers\Mongodb\Validation\ValidationServiceProvider
::
class
];
}
...
...
tests/ValidationTest.php
View file @
1ad438ec
...
...
@@ -22,5 +22,53 @@ class ValidationTest extends TestCase
[
'name'
=>
'required|unique:users'
]
);
$this
->
assertTrue
(
$validator
->
fails
());
$validator
=
Validator
::
make
(
[
'name'
=>
'John doe'
],
[
'name'
=>
'required|unique:users'
]
);
$this
->
assertTrue
(
$validator
->
fails
());
$validator
=
Validator
::
make
(
[
'name'
=>
'john doe'
],
[
'name'
=>
'required|unique:users'
]
);
$this
->
assertTrue
(
$validator
->
fails
());
$validator
=
Validator
::
make
(
[
'name'
=>
'test doe'
],
[
'name'
=>
'required|unique:users'
]
);
$this
->
assertFalse
(
$validator
->
fails
());
}
public
function
testExists
()
{
$validator
=
Validator
::
make
(
[
'name'
=>
'John Doe'
],
[
'name'
=>
'required|exists:users'
]
);
$this
->
assertTrue
(
$validator
->
fails
());
User
::
create
([
'name'
=>
'John Doe'
]);
User
::
create
([
'name'
=>
'Test Name'
]);
$validator
=
Validator
::
make
(
[
'name'
=>
'John Doe'
],
[
'name'
=>
'required|exists:users'
]
);
$this
->
assertFalse
(
$validator
->
fails
());
$validator
=
Validator
::
make
(
[
'name'
=>
'john Doe'
],
[
'name'
=>
'required|exists:users'
]
);
$this
->
assertFalse
(
$validator
->
fails
());
$validator
=
Validator
::
make
(
[
'name'
=>
[
'test name'
,
'john doe'
]],
[
'name'
=>
'required|exists:users'
]
);
$this
->
assertFalse
(
$validator
->
fails
());
}
}
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