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

[WIP] [ZF3] Validators refactoring #5067

Closed
Closed
573 changes: 64 additions & 509 deletions library/Zend/Validator/AbstractValidator.php

Large diffs are not rendered by default.

14 changes: 10 additions & 4 deletions library/Zend/Validator/Barcode.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,19 @@

class Barcode extends AbstractValidator
{
/**
* Error codes
*/
const INVALID = 'barcodeInvalid';
const FAILED = 'barcodeFailed';
const INVALID_CHARS = 'barcodeInvalidChars';
const INVALID_LENGTH = 'barcodeInvalidLength';

/**
* Validation error messages templates
*
* @var array
*/
protected $messageTemplates = array(
self::FAILED => "The input failed checksum validation",
self::INVALID_CHARS => "The input contains invalid characters",
Expand All @@ -26,13 +34,11 @@ class Barcode extends AbstractValidator
);

/**
* Additional variables available for validation failure messages
* Variables that can get injected
*
* @var array
*/
protected $messageVariables = array(
'length' => array('options' => 'length'),
);
protected $messageVariables = array('length');

protected $options = array(
'adapter' => null, // Barcode adapter Zend\Validator\Barcode\AbstractAdapter
Expand Down
113 changes: 63 additions & 50 deletions library/Zend/Validator/Barcode/AbstractAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,32 @@
abstract class AbstractAdapter implements AdapterInterface
{
/**
* Allowed options for this adapter
* @var array
* Allowed barcode lengths
*
* @var int
*/
protected $length;

/**
* Allowed barcode characters
*
* @var int|string|array
*/
protected $characters;

/**
* Callback to checksum function
*
* @var callable
*/
protected $checksum;

/**
* Is a checksum value included?
*
* @var bool
*/
protected $options = array(
'length' => null, // Allowed barcode lengths, integer, array, string
'characters' => null, // Allowed barcode characters
'checksum' => null, // Callback to checksum function
'useChecksum' => true, // Is a checksum value included?, boolean
);
protected $useChecksum = true;

/**
* Checks the length of a barcode
Expand Down Expand Up @@ -102,94 +119,90 @@ public function hasValidCharacters($value)
public function hasValidChecksum($value)
{
$checksum = $this->getChecksum();

if (!empty($checksum)) {
if (method_exists($this, $checksum)) {
return $this->$checksum($value);
}
return $this->$checksum($value);
}

return false;
}

/**
* Returns the allowed barcode length
* Sets the length of this barcode
*
* @return int|array
* @param int|array $length
* @return void
*/
public function getLength()
protected function setLength($length)
{
return $this->options['length'];
$this->length = $length;
}

/**
* Returns the allowed characters
* Returns the allowed barcode length
*
* @return int|string|array
* @return int|array
*/
public function getCharacters()
public function getLength()
{
return $this->options['characters'];
return $this->length;
}

/**
* Returns the checksum function name
* Sets the allowed characters of this barcode
*
* @param int $characters
* @return void
*/
public function getChecksum()
protected function setCharacters($characters)
{
return $this->options['checksum'];
$this->characters = $characters;
}

/**
* Sets the checksum validation method
* Returns the allowed characters
*
* @param callable $checksum Checksum method to call
* @return AbstractAdapter
* @return int|string|array
*/
protected function setChecksum($checksum)
public function getCharacters()
{
$this->options['checksum'] = $checksum;
return $this;
return $this->characters;
}

/**
* Sets the checksum validation, if no value is given, the actual setting is returned
* Returns the checksum callable
*
* @param bool $check
* @return AbstractAdapter|bool
* @return callable
*/
public function useChecksum($check = null)
public function getChecksum()
{
if ($check === null) {
return $this->options['useChecksum'];
}

$this->options['useChecksum'] = (bool) $check;
return $this;
return $this->checksum;
}

/**
* Sets the length of this barcode
* Sets the checksum validation method
*
* @param int|array $length
* @return AbstractAdapter
* @param callable $checksum Checksum method to call
* @return void
*/
protected function setLength($length)
protected function setChecksum(callable $checksum)
{
$this->options['length'] = $length;
return $this;
$this->checksum = $checksum;
}

/**
* Sets the allowed characters of this barcode
* Sets the checksum validation, if no value is given, the actual setting is returned
*
* @param int $characters
* @return AbstractAdapter
* @param bool $useChecksum
* @return bool
*/
protected function setCharacters($characters)
public function useChecksum($useChecksum = null)
{
$this->options['characters'] = $characters;
return $this;
if (null !== $useChecksum) {
$this->useChecksum = $useChecksum;
}

return $this->useChecksum;
}

/**
Expand Down
18 changes: 0 additions & 18 deletions library/Zend/Validator/Barcode/Identcode.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,6 @@

class Identcode extends AbstractAdapter
{
/**
* Allowed barcode lengths
* @var int
*/
protected $length = 12;

/**
* Allowed barcode characters
* @var string
*/
protected $characters = '0123456789';

/**
* Checksum function
* @var string
*/
protected $checksum = 'identcode';

/**
* Constructor for this barcode adapter
*/
Expand Down
2 changes: 1 addition & 1 deletion library/Zend/Validator/Barcode/Royalmail.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ protected function royalmail($value)

$rowchkvalue = array_keys($this->rows, $rowvalue);
$colchkvalue = array_keys($this->columns, $colvalue);
$intersect = array_intersect($rowchkvalue, $colchkvalue);
$intersect = array_intersect($rowchkvalue, $colchkvalue);
$chkvalue = current($intersect);
if ($chkvalue == $checksum) {
return true;
Expand Down
Loading