Skip to content

Commit

Permalink
Merge pull request #5 from chadicus/fea/concat
Browse files Browse the repository at this point in the history
Add Strings::concat
  • Loading branch information
chadicus committed Jan 18, 2019
2 parents 2823b8f + 52fb6f5 commit fb1f204
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 0 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ The following checks that `$value` is a non-empty string.
\TraderInteractive\Filter\Strings::filter($value);
```

#### Strings::concat
This filter concatenates the given $value, $prefix and $suffix and returns the resulting string.
```php
$value = \TraderInteractive\Filter\Strings::concat('middle', 'begining_', '_end');
assert($value === 'begining_middle_end');
```

#### Strings::explode

This filter is essentially a wrapper around the built-in [`explode`](http://www.php.net/explode) method
Expand Down
17 changes: 17 additions & 0 deletions src/Filter/Strings.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,23 @@ public static function explode($value, string $delimiter = ',')
return explode($delimiter, $value);
}

/**
* This filter prepends $prefix and appends $suffix to the string value.
*
* @param mixed $value The string value to which $prefix and $suffix will be added.
* @param string $prefix The value to prepend to the string.
* @param string $suffix The value to append to the string.
*
* @return string
*
* @throws FilterException Thrown if $value cannot be casted to a string.
*/
public static function concat($value, string $prefix = '', string $suffix = '') : string
{
self::enforceValueCanBeCastAsString($value);
return "{$prefix}{$value}{$suffix}";
}

/**
* Strip HTML and PHP tags from a string. Unlike the strip_tags function this method will return null if a null
* value is given. The native php function will return an empty string.
Expand Down
41 changes: 41 additions & 0 deletions tests/Filter/StringsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -267,4 +267,45 @@ public function stripTagsRemoveHtmlFromString()
$expected = 'A string with paragraph tags';
$this->assertSame($expected, $actual);
}

/**
* @test
* @covers ::concat
*/
public function concat()
{
$this->assertSame('prefixstringsuffix', Strings::concat('string', 'prefix', 'suffix'));
}

/**
* Verify behavior of concat() when $value is not filterable
*
* @test
* @covers ::concat
* @expectedException \TraderInteractive\Exceptions\FilterException
*
* @return void
*/
public function concatValueNotFilterable()
{
Strings::concat(new \StdClass(), 'prefix', 'suffix');
}

/**
* @test
* @covers ::concat
*/
public function concatScalarValue()
{
$this->assertSame('prefix123suffix', Strings::concat(123, 'prefix', 'suffix'));
}

/**
* @test
* @covers ::concat
*/
public function concatObjectValue()
{
$this->assertSame('prefix' . __FILE__ . 'suffix', Strings::concat(new \SplFileInfo(__FILE__), 'prefix', 'suffix'));
}
}

0 comments on commit fb1f204

Please sign in to comment.