-
-
Notifications
You must be signed in to change notification settings - Fork 339
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #916 from phalcon/3.4.x
v3.4.4
- Loading branch information
Showing
2 changed files
with
71 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,16 +14,16 @@ | |
| to [email protected] so we can send you a copy immediately. | | ||
+------------------------------------------------------------------------+ | ||
| Authors: Anton Kornilov <[email protected]> | | ||
| Maintainer: Wajdi Jurry <[email protected]> | ||
+------------------------------------------------------------------------+ | ||
*/ | ||
|
||
namespace Phalcon\Validation\Validator; | ||
|
||
use MongoId as Id; | ||
use MongoDB\BSON\ObjectId; | ||
use Phalcon\Validation; | ||
use Phalcon\Validation\Validator; | ||
use Phalcon\Validation\Message; | ||
use Phalcon\Validation\Exception as ValidationException; | ||
|
||
/** | ||
* MongoId validator | ||
|
@@ -36,30 +36,22 @@ class MongoId extends Validator | |
* @param Validation $validation | ||
* @param string $attribute | ||
* @return bool | ||
* @throws ValidationException | ||
*/ | ||
public function validate(Validation $validation, $attribute) | ||
{ | ||
if (!extension_loaded('mongo')) { | ||
throw new ValidationException('Mongo extension is not available'); | ||
} | ||
|
||
$value = $validation->getValue($attribute); | ||
$allowEmpty = $this->hasOption('allowEmpty'); | ||
$result = ($allowEmpty && empty($value)) ? true : Id::isValid($value); | ||
|
||
if (!$result) { | ||
$message = ($this->hasOption('message')) ? $this->getOption('message') : 'MongoId is not valid'; | ||
if ($allowEmpty && empty($value)) { | ||
return true; | ||
} | ||
|
||
$validation->appendMessage( | ||
new Message( | ||
$message, | ||
$attribute, | ||
'MongoId' | ||
) | ||
); | ||
if ($value instanceof ObjectId || preg_match('/^[a-f\d]{24}$/i', $value)) { | ||
return true; | ||
} | ||
|
||
return $result; | ||
$message = $this->hasOption('message') ? $this->getOption('message') : 'MongoId is not valid'; | ||
$validation->appendMessage(new Message($message, $attribute, 'MongoId')); | ||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
|
||
namespace Phalcon\Test\Validation\Validator; | ||
|
||
|
||
use MongoDB\BSON\ObjectId; | ||
use Phalcon\Test\Test\UnitTestCaseTest as Test; | ||
use Phalcon\Validation; | ||
use Phalcon\Validation\Validator\MongoId; | ||
|
||
/** | ||
* \Phalcon\Test\Validation\Validator\MongoIdTest | ||
* Tests for Phalcon\Validation\Validator\MongoId component | ||
* | ||
* @copyright (c) 2011-2019 Phalcon Team | ||
* @link http://www.phalconphp.com | ||
* @author Wajdi Jurry <[email protected]> | ||
* @package Phalcon\Validation\Validator | ||
* @group Validation | ||
* | ||
* The contents of this file are subject to the New BSD License that is | ||
* bundled with this package in the file docs/LICENSE.txt | ||
* | ||
* If you did not receive a copy of the license and are unable to obtain it | ||
* through the world-wide-web, please send an email to [email protected] | ||
* so that we can send you a copy immediately. | ||
*/ | ||
class MongoIdTest extends Test | ||
{ | ||
private $validator; | ||
|
||
public function setUp() | ||
{ | ||
$this->validator = new Validation(); | ||
$this->validator->add( | ||
'id', | ||
new MongoId([ | ||
'allowEmpty' => true | ||
]) | ||
); | ||
return parent::setUp(); // TODO: Change the autogenerated stub | ||
} | ||
|
||
public function testValidMongoIds() | ||
{ | ||
$stringMongoId = "5d4a0496e4895300110e3272"; | ||
$objectId = new ObjectId("5d4a0496e4895300110e3272"); | ||
$emptyId = ''; | ||
|
||
$this->assertCount(0, $this->validator->validate(['id' => $stringMongoId])); | ||
$this->assertCount(0, $this->validator->validate(['id' => $objectId])); | ||
$this->assertCount(0, $this->validator->validate(['id' => $emptyId])); | ||
} | ||
|
||
public function testInvalidMongoId() | ||
{ | ||
$invalidMongoId = "12345"; | ||
|
||
$this->assertCount(1, $this->validator->validate(['id' => $invalidMongoId])); | ||
} | ||
} |