Skip to content
This repository was archived by the owner on Jan 31, 2020. It is now read-only.

Commit c06036d

Browse files
committed
Merge branch 'hotfix/202'
Close #202
2 parents 4b58522 + 33c8c2b commit c06036d

File tree

4 files changed

+40
-5
lines changed

4 files changed

+40
-5
lines changed

CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ All notable changes to this project will be documented in this file, in reverse
66

77
### Added
88

9-
- Nothing.
9+
- [#202](https://github.com/zendframework/zend-validator/pull/202) adds the
10+
ability to use custom constant types in extensions of
11+
`Zend\Validator\CreditCard`, fixing an issue where users were unable to add
12+
new brands as they are created.
1013

1114
### Changed
1215

src/CreditCard.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -203,13 +203,20 @@ public function addType($type)
203203
}
204204

205205
foreach ($type as $typ) {
206-
if (defined('self::' . strtoupper($typ)) && ! in_array($typ, $this->options['type'])) {
207-
$this->options['type'][] = $typ;
208-
}
209-
210206
if (($typ == self::ALL)) {
211207
$this->options['type'] = array_keys($this->cardLength);
208+
continue;
209+
}
210+
211+
if (in_array($typ, $this->options['type'])) {
212+
continue;
213+
}
214+
215+
$constant = 'static::' . strtoupper($typ);
216+
if (! defined($constant) || in_array(constant($constant), $this->options['type'])) {
217+
continue;
212218
}
219+
$this->options['type'][] = constant($constant);
213220
}
214221

215222
return $this;

test/CreditCardTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,16 @@ public function testEqualsMessageTemplates()
331331
);
332332
}
333333

334+
/**
335+
* @see https://github.com/zendframework/zend-validator/pull/202
336+
*/
337+
public function testValidatorAllowsExtensionsToDefineAdditionalTypesViaConstants()
338+
{
339+
$validator = new TestAsset\CreditCardValidatorExtension();
340+
$this->assertSame($validator, $validator->addType('test_type'));
341+
$this->assertContains(TestAsset\CreditCardValidatorExtension::TEST_TYPE, $validator->getType());
342+
}
343+
334344
public static function staticCallback($value)
335345
{
336346
return false;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
/**
3+
* @see https://github.com/zendframework/zend-validator for the canonical source repository
4+
* @copyright Copyright (c) 2018 Zend Technologies USA Inc. (https://www.zend.com)
5+
* @license https://github.com/zendframework/zend-validator/blob/master/LICENSE.md New BSD License
6+
*/
7+
8+
namespace ZendTest\Validator\TestAsset;
9+
10+
use Zend\Validator\CreditCard;
11+
12+
class CreditCardValidatorExtension extends CreditCard
13+
{
14+
const TEST_TYPE = 'Test_Type';
15+
}

0 commit comments

Comments
 (0)