diff --git a/Library/Phalcon/Validation/Validator/MongoId.php b/Library/Phalcon/Validation/Validator/MongoId.php index 59052bf71..81d296994 100644 --- a/Library/Phalcon/Validation/Validator/MongoId.php +++ b/Library/Phalcon/Validation/Validator/MongoId.php @@ -14,16 +14,16 @@ | to license@phalconphp.com so we can send you a copy immediately. | +------------------------------------------------------------------------+ | Authors: Anton Kornilov | + | Maintainer: Wajdi Jurry +------------------------------------------------------------------------+ */ 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; } } diff --git a/tests/unit/Validation/Validator/MongoIdTest.php b/tests/unit/Validation/Validator/MongoIdTest.php new file mode 100644 index 000000000..e642a42ed --- /dev/null +++ b/tests/unit/Validation/Validator/MongoIdTest.php @@ -0,0 +1,61 @@ + + * @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 license@phalconphp.com + * 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])); + } +}