Skip to content

Commit 7d59053

Browse files
committed
Merge pull request #30 from DataValues/StringParser
Introduce StringParser and StringNormalizer
2 parents 41c305f + 336e128 commit 7d59053

File tree

6 files changed

+226
-0
lines changed

6 files changed

+226
-0
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ DataValues Common has been written by the Wikidata team, as [Wikimedia Germany]
5252
### 0.3.0 (2015-08-11)
5353

5454
* Added `DispatchingValueParser`
55+
* Added `StringNormalizer` interface
56+
* Added `NullStringNormalizer`
57+
* Added `StringParser`
5558
* Dropped deprecated constant `DataValuesCommon_VERSION`, use `DATAVALUES_COMMON_VERSION` instead
5659
* Dropped `ValueParserTestBase::getParserClass`
5760
* Dropped `ValueParserTestBase::newParserOptions`
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace ValueParsers\Normalizers;
4+
5+
use InvalidArgumentException;
6+
7+
/**
8+
* Null implementation of StringNormalizer.
9+
*
10+
* @since 0.3
11+
*
12+
* @license GPL 2+
13+
* @author Daniel Kinzler
14+
*/
15+
class NullStringNormalizer implements StringNormalizer {
16+
17+
/**
18+
* @param string $value
19+
*
20+
* @throws InvalidArgumentException if $value is not a string
21+
* @return string the normalized value
22+
*/
23+
public function normalize( $value ) {
24+
if ( !is_string( $value ) ) {
25+
throw new InvalidArgumentException( 'Parameter $value must be a string' );
26+
}
27+
28+
return $value;
29+
}
30+
31+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace ValueParsers\Normalizers;
4+
5+
use InvalidArgumentException;
6+
7+
/**
8+
* Interface for string normalization.
9+
*
10+
* @since 0.3
11+
*
12+
* @license GPL 2+
13+
* @author Daniel Kinzler
14+
*/
15+
interface StringNormalizer {
16+
17+
/**
18+
* @param string $value
19+
*
20+
* @throws InvalidArgumentException if $value is not a string
21+
* @return string the normalized value
22+
*/
23+
public function normalize( $value );
24+
25+
}

src/ValueParsers/StringParser.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
namespace ValueParsers;
4+
5+
use DataValues\StringValue;
6+
use InvalidArgumentException;
7+
use ValueParsers\Normalizers\NullStringNormalizer;
8+
use ValueParsers\Normalizers\StringNormalizer;
9+
10+
/**
11+
* Implementation of the ValueParser interface for StringValues.
12+
*
13+
* @since 0.3
14+
*
15+
* @licence GNU GPL v2+
16+
* @author Daniel Kinzler
17+
*/
18+
class StringParser implements ValueParser {
19+
20+
/**
21+
* @var StringNormalizer
22+
*/
23+
private $normalizer;
24+
25+
/**
26+
* @param StringNormalizer $normalizer
27+
*/
28+
public function __construct( StringNormalizer $normalizer = null ) {
29+
$this->normalizer = $normalizer ?: new NullStringNormalizer();
30+
}
31+
32+
/**
33+
* @see ValueParser::parse
34+
*
35+
* @param string $value
36+
*
37+
* @throws InvalidArgumentException if $value is not a string
38+
* @return StringValue
39+
*/
40+
public function parse( $value ) {
41+
if ( !is_string( $value ) ) {
42+
throw new InvalidArgumentException( 'Parameter $value must be a string' );
43+
}
44+
45+
$value = $this->normalizer->normalize( $value );
46+
return new StringValue( $value );
47+
}
48+
49+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
namespace ValueParsers\Normalizers\Test;
4+
5+
use DataValues\DataValue;
6+
use DataValues\StringValue;
7+
use PHPUnit_Framework_TestCase;
8+
use ValueParsers\Normalizers\NullStringNormalizer;
9+
10+
/**
11+
* @covers ValueParsers\Normalizers\NullStringNormalizer
12+
*
13+
* @group ValueParsers
14+
* @group DataValueExtensions
15+
*
16+
* @licence GNU GPL v2+
17+
* @author Thiemo Mättig
18+
*/
19+
class NullStringNormalizerTest extends PHPUnit_Framework_TestCase {
20+
21+
/**
22+
* @dataProvider stringProvider
23+
*/
24+
public function testNormalize( $value ) {
25+
$normalizer = new NullStringNormalizer();
26+
$this->assertSame( $value, $normalizer->normalize( $value ) );
27+
}
28+
29+
public function stringProvider() {
30+
return array(
31+
array( '' ),
32+
array( 'a' ),
33+
array( ' a ' ),
34+
);
35+
}
36+
37+
/**
38+
* @dataProvider invalidValueProvider
39+
*/
40+
public function testNormalizeException( $value ) {
41+
$normalizer = new NullStringNormalizer();
42+
$this->setExpectedException( 'InvalidArgumentException' );
43+
$normalizer->normalize( $value );
44+
}
45+
46+
public function invalidValueProvider() {
47+
return array(
48+
array( null ),
49+
array( true ),
50+
array( 1 ),
51+
array( new StringValue( '' ) ),
52+
);
53+
}
54+
55+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
namespace ValueParsers\Test;
4+
5+
use DataValues\DataValue;
6+
use DataValues\StringValue;
7+
use ValueParsers\Normalizers\StringNormalizer;
8+
use ValueParsers\StringParser;
9+
10+
/**
11+
* @covers ValueParsers\StringParser
12+
*
13+
* @group ValueParsers
14+
* @group DataValueExtensions
15+
*
16+
* @licence GNU GPL v2+
17+
* @author Daniel Kinzler
18+
*/
19+
class StringParserTest extends \PHPUnit_Framework_TestCase {
20+
21+
public function provideParse() {
22+
$normalizer = $this->getMock( 'ValueParsers\Normalizers\StringNormalizer' );
23+
$normalizer->expects( $this->once() )
24+
->method( 'normalize' )
25+
->will( $this->returnCallback( function( $value ) {
26+
return strtolower( trim( $value ) );
27+
} ) );
28+
29+
return array(
30+
'simple' => array( 'hello world', null, new StringValue( 'hello world' ) ),
31+
'normalize' => array( ' Hello World ', $normalizer, new StringValue( 'hello world' ) ),
32+
);
33+
}
34+
35+
/**
36+
* @dataProvider provideParse
37+
*/
38+
public function testParse( $input, StringNormalizer $normalizer = null, DataValue $expected ) {
39+
$parser = new StringParser( $normalizer );
40+
$value = $parser->parse( $input );
41+
42+
$this->assertInstanceOf( 'DataValues\StringValue', $value );
43+
$this->assertEquals( $expected->toArray(), $value->toArray() );
44+
}
45+
46+
public function nonStringProvider() {
47+
return array(
48+
'null' => array( null ),
49+
'array' => array( array() ),
50+
'int' => array( 7 ),
51+
);
52+
}
53+
54+
/**
55+
* @dataProvider nonStringProvider
56+
*/
57+
public function testGivenNonString_parseThrowsException( $input ) {
58+
$parser = new StringParser();
59+
$this->setExpectedException( 'InvalidArgumentException' );
60+
$parser->parse( $input );
61+
}
62+
63+
}

0 commit comments

Comments
 (0)