Skip to content

Commit cf76b1f

Browse files
committed
Simplify SimpleType validation
1 parent 18cb0b2 commit cf76b1f

File tree

8 files changed

+35
-59
lines changed

8 files changed

+35
-59
lines changed

composer.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
},
2121
{
2222
"name": "Franck Lefevre",
23-
"homepage": "http://blog.rootslabs.net"
23+
"homepage": "https://rootslabs.net/blog/"
2424
},
2525
{
2626
"name": "Ivan Lanin",
@@ -36,13 +36,11 @@
3636
"ext-xml": "*",
3737
"zendframework/zend-escaper": "2.4.*",
3838
"zendframework/zend-stdlib": "2.4.*",
39-
"zendframework/zend-validator": "2.4.*",
4039
"phpoffice/common": "0.2.*"
4140
},
4241
"require-dev": {
4342
"phpunit/phpunit": "3.7.*",
4443
"phpdocumentor/phpdocumentor":"2.*",
45-
"twig/twig":"1.27",
4644
"squizlabs/php_codesniffer": "1.*",
4745
"phpmd/phpmd": "2.*",
4846
"phploc/phploc": "2.*",

src/PhpWord/Shared/AbstractEnum.php

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,39 @@ private static function getConstants()
1919
return self::$constCacheArray[$calledClass];
2020
}
2121

22+
/**
23+
* Returns all values for this enum
24+
*
25+
* @return array
26+
*/
2227
public static function values()
2328
{
2429
return array_values(self::getConstants());
2530
}
2631

27-
public static function validate($value)
32+
/**
33+
* Returns true the value is valid for this enum
34+
*
35+
* @param strign $value
36+
* @return boolean true if value is valid
37+
*/
38+
public static function isValid($value)
2839
{
2940
$values = array_values(self::getConstants());
30-
if (!in_array($value, $values, true)) {
41+
return in_array($value, $values, true);
42+
}
43+
44+
/**
45+
* Validates that the value passed is a valid value
46+
*
47+
* @param string $value
48+
* @throws \InvalidArgumentException if the value passed is not valid for this enum
49+
*/
50+
public static function validate($value)
51+
{
52+
if (!self::isValid($value)) {
3153
$calledClass = get_called_class();
54+
$values = array_values(self::getConstants());
3255
throw new \InvalidArgumentException("$value is not a valid value for $calledClass, possible values are " . implode(', ', $values));
3356
}
3457
}

src/PhpWord/SimpleType/Jc.php

Lines changed: 3 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
namespace PhpOffice\PhpWord\SimpleType;
1919

20-
use Zend\Validator\InArray;
20+
use PhpOffice\PhpWord\Shared\AbstractEnum;
2121

2222
/**
2323
* Horizontal Alignment Type.
@@ -28,10 +28,11 @@
2828
* @since 0.13.0
2929
*
3030
* @see \PhpOffice\PhpWord\SimpleType\JcTable For table alignment modes available since ISO/IEC-29500:2008.
31+
* @link http://www.datypic.com/sc/ooxml/t-w_ST_Jc.html
3132
*
3233
* @codeCoverageIgnore
3334
*/
34-
final class Jc
35+
final class Jc extends AbstractEnum
3536
{
3637
const START = 'start';
3738
const CENTER = 'center';
@@ -65,34 +66,4 @@ final class Jc
6566
* @deprecated 0.13.0 For documents based on ISO/IEC 29500:2008 and later use `BOTH` instead.
6667
*/
6768
const JUSTIFY = 'justify';
68-
69-
/**
70-
* @since 0.13.0
71-
*
72-
* @return \Zend\Validator\InArray
73-
*/
74-
final public static function getValidator()
75-
{
76-
// todo: consider caching validator instances.
77-
return new InArray(
78-
array (
79-
'haystack' => array(
80-
self::START,
81-
self::CENTER,
82-
self::END,
83-
self::BOTH,
84-
self::MEDIUM_KASHIDA,
85-
self::DISTRIBUTE,
86-
self::NUM_TAB,
87-
self::HIGH_KASHIDA,
88-
self::LOW_KASHIDA,
89-
self::THAI_DISTRIBUTE,
90-
self::LEFT,
91-
self::RIGHT,
92-
self::JUSTIFY,
93-
),
94-
'strict' => InArray::COMPARE_STRICT,
95-
)
96-
);
97-
}
9869
}

src/PhpWord/SimpleType/JcTable.php

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
namespace PhpOffice\PhpWord\SimpleType;
1919

20-
use Zend\Validator\InArray;
20+
use PhpOffice\PhpWord\Shared\AbstractEnum;
2121

2222
/**
2323
* Table Alignment Type.
@@ -28,25 +28,9 @@
2828
*
2929
* @codeCoverageIgnore
3030
*/
31-
final class JcTable
31+
final class JcTable extends AbstractEnum
3232
{
3333
const START = 'start';
3434
const CENTER = 'center';
3535
const END = 'end';
36-
37-
/**
38-
* @since 0.13.0
39-
*
40-
* @return \Zend\Validator\InArray
41-
*/
42-
final public static function getValidator()
43-
{
44-
// todo: consider caching validator instances.
45-
return new InArray(
46-
array (
47-
'haystack' => array(self::START, self::CENTER, self::END),
48-
'strict' => InArray::COMPARE_STRICT,
49-
)
50-
);
51-
}
5236
}

src/PhpWord/Style/Frame.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ public function getAlignment()
200200
*/
201201
public function setAlignment($value)
202202
{
203-
if (Jc::getValidator()->isValid($value)) {
203+
if (Jc::isValid($value)) {
204204
$this->alignment = $value;
205205
}
206206

src/PhpWord/Style/NumberingLevel.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ public function getAlignment()
300300
*/
301301
public function setAlignment($value)
302302
{
303-
if (Jc::getValidator()->isValid($value)) {
303+
if (Jc::isValid($value)) {
304304
$this->alignment = $value;
305305
}
306306

src/PhpWord/Style/Paragraph.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ public function getAlignment()
240240
*/
241241
public function setAlignment($value)
242242
{
243-
if (Jc::getValidator()->isValid($value)) {
243+
if (Jc::isValid($value)) {
244244
$this->alignment = $value;
245245
}
246246

src/PhpWord/Style/Table.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,7 @@ public function getAlignment()
510510
*/
511511
public function setAlignment($value)
512512
{
513-
if (JcTable::getValidator()->isValid($value) || Jc::getValidator()->isValid($value)) {
513+
if (JcTable::isValid($value) || Jc::isValid($value)) {
514514
$this->alignment = $value;
515515
}
516516

0 commit comments

Comments
 (0)