Simplify SimpleType validation

This commit is contained in:
troosan 2017-07-20 07:39:21 +02:00
parent 18cb0b26f7
commit cf76b1f217
8 changed files with 35 additions and 59 deletions

View File

@ -20,7 +20,7 @@
}, },
{ {
"name": "Franck Lefevre", "name": "Franck Lefevre",
"homepage": "http://blog.rootslabs.net" "homepage": "https://rootslabs.net/blog/"
}, },
{ {
"name": "Ivan Lanin", "name": "Ivan Lanin",
@ -36,13 +36,11 @@
"ext-xml": "*", "ext-xml": "*",
"zendframework/zend-escaper": "2.4.*", "zendframework/zend-escaper": "2.4.*",
"zendframework/zend-stdlib": "2.4.*", "zendframework/zend-stdlib": "2.4.*",
"zendframework/zend-validator": "2.4.*",
"phpoffice/common": "0.2.*" "phpoffice/common": "0.2.*"
}, },
"require-dev": { "require-dev": {
"phpunit/phpunit": "3.7.*", "phpunit/phpunit": "3.7.*",
"phpdocumentor/phpdocumentor":"2.*", "phpdocumentor/phpdocumentor":"2.*",
"twig/twig":"1.27",
"squizlabs/php_codesniffer": "1.*", "squizlabs/php_codesniffer": "1.*",
"phpmd/phpmd": "2.*", "phpmd/phpmd": "2.*",
"phploc/phploc": "2.*", "phploc/phploc": "2.*",

View File

@ -19,16 +19,39 @@ abstract class AbstractEnum
return self::$constCacheArray[$calledClass]; return self::$constCacheArray[$calledClass];
} }
/**
* Returns all values for this enum
*
* @return array
*/
public static function values() public static function values()
{ {
return array_values(self::getConstants()); return array_values(self::getConstants());
} }
public static function validate($value) /**
* Returns true the value is valid for this enum
*
* @param strign $value
* @return boolean true if value is valid
*/
public static function isValid($value)
{ {
$values = array_values(self::getConstants()); $values = array_values(self::getConstants());
if (!in_array($value, $values, true)) { return in_array($value, $values, true);
}
/**
* Validates that the value passed is a valid value
*
* @param string $value
* @throws \InvalidArgumentException if the value passed is not valid for this enum
*/
public static function validate($value)
{
if (!self::isValid($value)) {
$calledClass = get_called_class(); $calledClass = get_called_class();
$values = array_values(self::getConstants());
throw new \InvalidArgumentException("$value is not a valid value for $calledClass, possible values are " . implode(', ', $values)); throw new \InvalidArgumentException("$value is not a valid value for $calledClass, possible values are " . implode(', ', $values));
} }
} }

View File

@ -17,7 +17,7 @@
namespace PhpOffice\PhpWord\SimpleType; namespace PhpOffice\PhpWord\SimpleType;
use Zend\Validator\InArray; use PhpOffice\PhpWord\Shared\AbstractEnum;
/** /**
* Horizontal Alignment Type. * Horizontal Alignment Type.
@ -28,10 +28,11 @@ use Zend\Validator\InArray;
* @since 0.13.0 * @since 0.13.0
* *
* @see \PhpOffice\PhpWord\SimpleType\JcTable For table alignment modes available since ISO/IEC-29500:2008. * @see \PhpOffice\PhpWord\SimpleType\JcTable For table alignment modes available since ISO/IEC-29500:2008.
* @link http://www.datypic.com/sc/ooxml/t-w_ST_Jc.html
* *
* @codeCoverageIgnore * @codeCoverageIgnore
*/ */
final class Jc final class Jc extends AbstractEnum
{ {
const START = 'start'; const START = 'start';
const CENTER = 'center'; const CENTER = 'center';
@ -65,34 +66,4 @@ final class Jc
* @deprecated 0.13.0 For documents based on ISO/IEC 29500:2008 and later use `BOTH` instead. * @deprecated 0.13.0 For documents based on ISO/IEC 29500:2008 and later use `BOTH` instead.
*/ */
const JUSTIFY = 'justify'; const JUSTIFY = 'justify';
/**
* @since 0.13.0
*
* @return \Zend\Validator\InArray
*/
final public static function getValidator()
{
// todo: consider caching validator instances.
return new InArray(
array (
'haystack' => array(
self::START,
self::CENTER,
self::END,
self::BOTH,
self::MEDIUM_KASHIDA,
self::DISTRIBUTE,
self::NUM_TAB,
self::HIGH_KASHIDA,
self::LOW_KASHIDA,
self::THAI_DISTRIBUTE,
self::LEFT,
self::RIGHT,
self::JUSTIFY,
),
'strict' => InArray::COMPARE_STRICT,
)
);
}
} }

View File

@ -17,7 +17,7 @@
namespace PhpOffice\PhpWord\SimpleType; namespace PhpOffice\PhpWord\SimpleType;
use Zend\Validator\InArray; use PhpOffice\PhpWord\Shared\AbstractEnum;
/** /**
* Table Alignment Type. * Table Alignment Type.
@ -28,25 +28,9 @@ use Zend\Validator\InArray;
* *
* @codeCoverageIgnore * @codeCoverageIgnore
*/ */
final class JcTable final class JcTable extends AbstractEnum
{ {
const START = 'start'; const START = 'start';
const CENTER = 'center'; const CENTER = 'center';
const END = 'end'; const END = 'end';
/**
* @since 0.13.0
*
* @return \Zend\Validator\InArray
*/
final public static function getValidator()
{
// todo: consider caching validator instances.
return new InArray(
array (
'haystack' => array(self::START, self::CENTER, self::END),
'strict' => InArray::COMPARE_STRICT,
)
);
}
} }

View File

@ -200,7 +200,7 @@ class Frame extends AbstractStyle
*/ */
public function setAlignment($value) public function setAlignment($value)
{ {
if (Jc::getValidator()->isValid($value)) { if (Jc::isValid($value)) {
$this->alignment = $value; $this->alignment = $value;
} }

View File

@ -300,7 +300,7 @@ class NumberingLevel extends AbstractStyle
*/ */
public function setAlignment($value) public function setAlignment($value)
{ {
if (Jc::getValidator()->isValid($value)) { if (Jc::isValid($value)) {
$this->alignment = $value; $this->alignment = $value;
} }

View File

@ -240,7 +240,7 @@ class Paragraph extends Border
*/ */
public function setAlignment($value) public function setAlignment($value)
{ {
if (Jc::getValidator()->isValid($value)) { if (Jc::isValid($value)) {
$this->alignment = $value; $this->alignment = $value;
} }

View File

@ -510,7 +510,7 @@ class Table extends Border
*/ */
public function setAlignment($value) public function setAlignment($value)
{ {
if (JcTable::getValidator()->isValid($value) || Jc::getValidator()->isValid($value)) { if (JcTable::isValid($value) || Jc::isValid($value)) {
$this->alignment = $value; $this->alignment = $value;
} }