Skip to content

Commit 8efbaa0

Browse files
authored
Merge pull request #916 from phalcon/3.4.x
v3.4.4
2 parents 08ab1a9 + 78bdf9f commit 8efbaa0

File tree

2 files changed

+71
-18
lines changed

2 files changed

+71
-18
lines changed

Library/Phalcon/Validation/Validator/MongoId.php

+10-18
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,16 @@
1414
| to [email protected] so we can send you a copy immediately. |
1515
+------------------------------------------------------------------------+
1616
| Authors: Anton Kornilov <[email protected]> |
17+
| Maintainer: Wajdi Jurry <[email protected]>
1718
+------------------------------------------------------------------------+
1819
*/
1920

2021
namespace Phalcon\Validation\Validator;
2122

22-
use MongoId as Id;
23+
use MongoDB\BSON\ObjectId;
2324
use Phalcon\Validation;
2425
use Phalcon\Validation\Validator;
2526
use Phalcon\Validation\Message;
26-
use Phalcon\Validation\Exception as ValidationException;
2727

2828
/**
2929
* MongoId validator
@@ -36,30 +36,22 @@ class MongoId extends Validator
3636
* @param Validation $validation
3737
* @param string $attribute
3838
* @return bool
39-
* @throws ValidationException
4039
*/
4140
public function validate(Validation $validation, $attribute)
4241
{
43-
if (!extension_loaded('mongo')) {
44-
throw new ValidationException('Mongo extension is not available');
45-
}
46-
4742
$value = $validation->getValue($attribute);
4843
$allowEmpty = $this->hasOption('allowEmpty');
49-
$result = ($allowEmpty && empty($value)) ? true : Id::isValid($value);
5044

51-
if (!$result) {
52-
$message = ($this->hasOption('message')) ? $this->getOption('message') : 'MongoId is not valid';
45+
if ($allowEmpty && empty($value)) {
46+
return true;
47+
}
5348

54-
$validation->appendMessage(
55-
new Message(
56-
$message,
57-
$attribute,
58-
'MongoId'
59-
)
60-
);
49+
if ($value instanceof ObjectId || preg_match('/^[a-f\d]{24}$/i', $value)) {
50+
return true;
6151
}
6252

63-
return $result;
53+
$message = $this->hasOption('message') ? $this->getOption('message') : 'MongoId is not valid';
54+
$validation->appendMessage(new Message($message, $attribute, 'MongoId'));
55+
return false;
6456
}
6557
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
namespace Phalcon\Test\Validation\Validator;
4+
5+
6+
use MongoDB\BSON\ObjectId;
7+
use Phalcon\Test\Test\UnitTestCaseTest as Test;
8+
use Phalcon\Validation;
9+
use Phalcon\Validation\Validator\MongoId;
10+
11+
/**
12+
* \Phalcon\Test\Validation\Validator\MongoIdTest
13+
* Tests for Phalcon\Validation\Validator\MongoId component
14+
*
15+
* @copyright (c) 2011-2019 Phalcon Team
16+
* @link http://www.phalconphp.com
17+
* @author Wajdi Jurry <[email protected]>
18+
* @package Phalcon\Validation\Validator
19+
* @group Validation
20+
*
21+
* The contents of this file are subject to the New BSD License that is
22+
* bundled with this package in the file docs/LICENSE.txt
23+
*
24+
* If you did not receive a copy of the license and are unable to obtain it
25+
* through the world-wide-web, please send an email to [email protected]
26+
* so that we can send you a copy immediately.
27+
*/
28+
class MongoIdTest extends Test
29+
{
30+
private $validator;
31+
32+
public function setUp()
33+
{
34+
$this->validator = new Validation();
35+
$this->validator->add(
36+
'id',
37+
new MongoId([
38+
'allowEmpty' => true
39+
])
40+
);
41+
return parent::setUp(); // TODO: Change the autogenerated stub
42+
}
43+
44+
public function testValidMongoIds()
45+
{
46+
$stringMongoId = "5d4a0496e4895300110e3272";
47+
$objectId = new ObjectId("5d4a0496e4895300110e3272");
48+
$emptyId = '';
49+
50+
$this->assertCount(0, $this->validator->validate(['id' => $stringMongoId]));
51+
$this->assertCount(0, $this->validator->validate(['id' => $objectId]));
52+
$this->assertCount(0, $this->validator->validate(['id' => $emptyId]));
53+
}
54+
55+
public function testInvalidMongoId()
56+
{
57+
$invalidMongoId = "12345";
58+
59+
$this->assertCount(1, $this->validator->validate(['id' => $invalidMongoId]));
60+
}
61+
}

0 commit comments

Comments
 (0)