Commit 1ad438ec authored by Curos's avatar Curos

Add DatabasePresenceVerifier and tests

parent 3276bac2
<?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();
}
}
<?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
......@@ -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
];
}
......
......@@ -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());
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment