Skip to content

Commit 6024e94

Browse files
danielthiemowmde
authored andcommitted
Introduce StringParser and StringNormalizer
Bug:T104874
1 parent 41c305f commit 6024e94

File tree

4 files changed

+173
-0
lines changed

4 files changed

+173
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace ValueParsers\Normalizers;
4+
5+
use InvalidArgumentException;
6+
7+
/**
8+
* Null implementation of StringNormalizer.
9+
*
10+
* @license GPL 2+
11+
* @author Daniel Kinzler
12+
*/
13+
class NullStringNormalizer implements StringNormalizer {
14+
15+
/**
16+
* @param string $value the value to normalize
17+
*
18+
* @return string the normalized value
19+
*/
20+
public function normalize( $value ) {
21+
if ( !is_string( $value ) ) {
22+
throw new InvalidArgumentException( 'Parameter $value must be a string' );
23+
}
24+
25+
return $value;
26+
}
27+
28+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace ValueParsers\Normalizers;
4+
5+
use InvalidArgumentException;
6+
7+
/**
8+
* Interface for string normalization
9+
*
10+
* @license GPL 2+
11+
* @author Daniel Kinzler
12+
*/
13+
interface StringNormalizer {
14+
15+
/**
16+
* @param string $value the value to normalize
17+
*
18+
* @return string the normalized value
19+
*
20+
* @throws InvalidArgumentException if $value is not a string
21+
*/
22+
public function normalize( $value );
23+
24+
}

src/ValueParsers/StringParser.php

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

0 commit comments

Comments
 (0)