1
0
mirror of https://github.com/danielstjules/Stringy.git synced 2025-09-02 09:33:10 +02:00

16 Commits
1.0.0 ... 1.1.0

Author SHA1 Message Date
Daniel St. Jules
545a5aec5f Updated changelog for 1.1.0 2013-08-31 19:34:28 -04:00
Daniel St. Jules
63298f36a8 Cleanup comments, release 1.1.0 2013-08-31 19:30:47 -04:00
Daniel St. Jules
a667c4aa6a Fix and merge branch 'isJson' 2013-08-31 19:12:46 -04:00
Lucas
cd5e897ac3 isJson() function, with tests 2013-08-31 18:43:16 -04:00
Daniel St. Jules
390671e131 Cleanup and merge branch 'isserialized' 2013-08-31 18:37:58 -04:00
Lucas
0b76c56333 function isSerialized(), with tests 2013-08-31 18:20:39 -04:00
Daniel St. Jules
5f35226926 Merge branch 'better-construct', update testConstruct and create 2013-08-27 01:34:44 -04:00
Daniel St. Jules
11f961cae6 Fixed testShuffle. It now tests that each mb char has the same number of occurrences before and after 2013-08-24 11:19:33 -04:00
Lucas
660b1d6de8 Added simple constructor test 2013-08-24 14:32:43 +02:00
Lucas
3d81e2ef70 Update create() and added __construct() 2013-08-24 14:17:16 +02:00
Daniel St. Jules
454e8e2f87 Added isHexadecimal to Readme 2013-08-23 01:08:31 -04:00
Daniel St. Jules
89c292c041 Merge pull request #2 from semalead/isHeaxdecimal
Added isHexadecimal
2013-08-22 21:51:27 -07:00
Lucas
a1d9787309 Added isHexadecimal 2013-08-22 15:40:43 +02:00
Daniel St. Jules
f1abc38c17 Fix for collapseWhitespace() not being able to handle multibyte space characters 2013-08-06 22:39:47 -04:00
Daniel St. Jules
7a64dad935 Remove dummy test from CommonTest.php 2013-08-06 22:16:53 -04:00
Daniel St. Jules
9d4ca96528 Removed packagist icon 2013-08-01 01:04:21 -04:00
7 changed files with 323 additions and 40 deletions

View File

@@ -1,3 +1,11 @@
### 1.1.0 (2013-08-31)
* Fix for collapseWhitespace()
* Added isHexadecimal()
* Added constructor to Stringy\Stringy
* Added isSerialized()
* Added isJson()
### 1.0.0 (2013-08-1)
* 1.0.0 release

View File

@@ -3,7 +3,6 @@
A PHP library with a variety of string manipulation functions with multibyte support. Offers both OO method chaining and a procedural-style static wrapper. Compatible with PHP 5.3+. Inspired by underscore.string.js.
[![Build Status](https://travis-ci.org/danielstjules/Stringy.png)](https://travis-ci.org/danielstjules/Stringy)
[![Latest Stable Version](https://poser.pugx.org/danielstjules/Stringy/v/stable.png)](https://packagist.org/packages/danielstjules/stringy)
* [Requiring/Loading](#requiringloading)
* [OO and Procedural](#oo-and-procedural)
@@ -24,7 +23,10 @@ A PHP library with a variety of string manipulation functions with multibyte sup
* [isAlpha](#isalpha)
* [isAlphanumeric](#isalphanumeric)
* [isBlank](#isblank)
* [isHexadecimal](#ishexadecimal)
* [isJson](#isjson)
* [isLowerCase](#islowercase)
* [isSerialized](#isserialized)
* [isUpperCase](#isuppercase)
* [last](#last)
* [length](#length)
@@ -98,7 +100,7 @@ of the former is the following:
```php
use Stringy\Stringy as S;
echo S::create("Fòô Bàř", 'UTF-8')->collapseWhitespace()->swapCase(); // 'fÒÔ bÀŘ'
echo S::create('Fòô Bàř', 'UTF-8')->collapseWhitespace()->swapCase(); // 'fÒÔ bÀŘ'
```
`Stringy\Stringy` contains a __toString() method, which returns the current
@@ -109,8 +111,8 @@ Using the static wrapper, an alternative is the following:
```php
use Stringy\StaticStringy as S;
$string = S::collapseWhitespace("Fòô Bàř", 'UTF-8');
echo S::swapCase($string, 'UTF-8'); // 'fÒÔ bÀŘ''
$string = S::collapseWhitespace('Fòô Bàř', 'UTF-8');
echo S::swapCase($string, 'UTF-8'); // 'fÒÔ bÀŘ'
```
## Methods
@@ -152,10 +154,11 @@ S::camelize('Camel-Case'); // 'camelCase'
$stringy->collapseWhitespace()
S::collapseWhitespace(string $str)
S::collapseWhitespace(string $str [, string $encoding ])
Trims the string and replaces consecutive whitespace characters with a
single space. This includes tabs and newline characters.
single space. This includes tabs and newline characters, as well as
multibyte whitespace such as the thin space and ideographic space.
```php
S::create(' Ο συγγραφέας ')->collapseWhitespace();
@@ -337,6 +340,32 @@ S::create("\n\t \v\f")->isBlank();
S::isBlank("\n\t \v\f"); // true
```
#### isHexadecimal
$stringy->isHexadecimal()
S::isHexadecimal(string $str [, string $encoding ])
Returns true if the string contains only hexadecimal chars, false otherwise.
```php
S::create('A102F')->isHexadecimal();
S::isHexadecimal('A102F'); // true
```
#### isJson
$stringy->isJson()
S::isJson(string $str [, string $encoding ])
Returns true if the string is JSON, false otherwise.
```php
S::create('{"foo":"bar"}')->isJson();
S::isJson('{"foo":"bar"}'); // true
```
#### isLowerCase
$stringy->isLowerCase()
@@ -350,6 +379,19 @@ S::create('fòô bàř', 'UTF-8')->isLowerCase();
S::isLowerCase('fòô bàř', 'UTF-8'); // true
```
#### isSerialized
$stringy->isSerialized()
S::isSerialized(string $str [, string $encoding ])
Returns true if the string is serialized, false otherwise.
```php
S::create('a:1:{s:3:"foo";s:3:"bar";}', 'UTF-8')->isSerialized();
S::isSerialized('a:1:{s:3:"foo";s:3:"bar";}', 'UTF-8'); // true
```
#### isUpperCase
$stringy->isUpperCase()
@@ -359,8 +401,8 @@ S::isUpperCase(string $str [, string $encoding ])
Returns true if the string contains only upper case chars, false otherwise.
```php
S::create('FÒÔBÀŘ',, 'UTF-8')->isUpperCase();
S::isUpperCase('FÒÔBÀŘ',, 'UTF-8'); // true
S::create('FÒÔBÀŘ', 'UTF-8')->isUpperCase();
S::isUpperCase('FÒÔBÀŘ', 'UTF-8'); // true
```
#### last

View File

@@ -142,14 +142,15 @@ class StaticStringy
/**
* Trims the string and replaces consecutive whitespace characters with a
* single space. This includes tabs and newline characters.
* single space. This includes tabs and newline characters, as well as
* multibyte whitespace such as the thin space and ideographic space.
*
* @param string $str The string to cleanup whitespace
* @return string The trimmed string with condensed whitespace
*/
public static function collapseWhitespace($str)
public static function collapseWhitespace($str, $encoding = null)
{
return $result = Stringy::create($str)->collapseWhitespace()->str;
return $result = Stringy::create($str, $encoding)->collapseWhitespace()->str;
}
/**
@@ -623,6 +624,18 @@ class StaticStringy
return Stringy::create($str, $encoding)->isBlank();
}
/**
* Returns true if the string is JSON, false otherwise.
*
* @param string $str String to check
* @param string $encoding The character encoding
* @return bool Whether or not $str is JSON
*/
public static function isJson($str, $encoding = null)
{
return Stringy::create($str, $encoding)->isJson();
}
/**
* Returns true if the string contains only lower case chars, false otherwise.
*
@@ -635,6 +648,18 @@ class StaticStringy
return Stringy::create($str, $encoding)->isLowerCase();
}
/**
* Returns true if the string is serialized, false otherwise.
*
* @param string $str String to check
* @param string $encoding The character encoding
* @return bool Whether or not $str is serialized
*/
public static function isSerialized($str, $encoding = null)
{
return Stringy::create($str, $encoding)->isSerialized();
}
/**
* Returns true if the string contains only upper case chars, false otherwise.
*
@@ -647,6 +672,18 @@ class StaticStringy
return Stringy::create($str, $encoding)->isUpperCase();
}
/**
* Returns true if the string contains only hexadecimal chars, false otherwise.
*
* @param string $str String to check
* @param string $encoding The character encoding
* @return bool Whether or not $str contains only hexadecimal characters
*/
public static function isHexadecimal($str, $encoding = null)
{
return Stringy::create($str, $encoding)->isHexadecimal();
}
/**
* Returns the number of occurrences of $substring in the given string.
* An alias for mb_substr_count()

View File

@@ -8,6 +8,20 @@ class Stringy
public $encoding;
/**
* Initializes a Stringy object and assigns both str and encoding properties
* the supplied values. If $encoding is not specified, it defaults to
* mb_internal_encoding().
*
* @param string $str String to modify
* @param string $encoding The character encoding
*/
public function __construct($str, $encoding = null)
{
$this->str = $str;
$this->encoding = $encoding ?: mb_internal_encoding();
}
/**
* Creates a Stringy object and assigns both str and encoding properties
* the supplied values. If $encoding is not specified, it defaults to
@@ -19,13 +33,7 @@ class Stringy
*/
public static function create($str, $encoding = null)
{
$encoding = $encoding ?: mb_internal_encoding();
$stringyObj = new self();
$stringyObj->str = $str;
$stringyObj->encoding = $encoding;
return $stringyObj;
return new self($str, $encoding);
}
/**
@@ -249,16 +257,21 @@ class Stringy
/**
* Trims the string and replaces consecutive whitespace characters with a
* single space. This includes tabs and newline characters.
* single space. This includes tabs and newline characters, as well as
* multibyte whitespace such as the thin space and ideographic space.
*
* @return Stringy Object with a trimmed $str and condensed whitespace
*/
public function collapseWhitespace()
{
$stringy = self::create($this->str, $this->encoding);
$stringy->str = preg_replace('/\s+/u', ' ', $stringy->trim());
$regexEncoding = mb_regex_encoding();
mb_regex_encoding($this->encoding);
return $stringy;
$stringy = self::create($this->str, $this->encoding);
$stringy->str = mb_ereg_replace('[[:space:]]+', ' ', $stringy);
mb_regex_encoding($regexEncoding);
return $stringy->trim();
}
/**
@@ -336,9 +349,9 @@ class Stringy
* 'right', 'both') is 'right'. Throws an InvalidArgumentException if
* $padType isn't one of those 3 values.
*
* @param int $length Desired string length after padding
* @param string $padStr String used to pad, defaults to space
* @param string $padType One of 'left', 'right', 'both'
* @param int $length Desired string length after padding
* @param string $padStr String used to pad, defaults to space
* @param string $padType One of 'left', 'right', 'both'
* @return Stringy Object with a padded $str
* @throws InvalidArgumentException If $padType isn't one of 'right',
* 'left' or 'both'
@@ -969,6 +982,16 @@ class Stringy
return $this->matchesPattern('^([[:alnum:]])*$');
}
/**
* Returns true if the string contains only hexadecimal chars, false otherwise.
*
* @return bool Whether or not $str contains only hexadecimal chars
*/
public function isHexadecimal()
{
return $this->matchesPattern('^([[:xdigit:]])*$');
}
/**
* Returns true if the string contains only whitespace chars, false otherwise.
*
@@ -979,6 +1002,21 @@ class Stringy
return $this->matchesPattern('^([[:space:]])*$');
}
/**
* Returns true if the string is JSON, false otherwise.
*
* @return bool Whether or not $str is JSON
*/
public function isJson()
{
if (!$this->endsWith('}') && !$this->endsWith(']')) {
return false;
}
return !is_null(json_decode($this->str));
}
/**
* Returns true if the string contains only lower case chars, false otherwise.
*
@@ -999,6 +1037,16 @@ class Stringy
return $this->matchesPattern('^([[:upper:]])*$');
}
/**
* Returns true if the string is serialized, false otherwise.
*
* @return bool Whether or not $str is serialized
*/
public function isSerialized()
{
return $this->str === 'b:0;' || @unserialize($this->str) !== false;
}
/**
* Returns the number of occurrences of $substring in the given string.
* An alias for mb_substr_count()

View File

@@ -1,6 +1,6 @@
<?php
class CommonTest extends PHPUnit_Framework_TestCase
abstract class CommonTest extends PHPUnit_Framework_TestCase
{
public function stringsForUpperCaseFirst()
{
@@ -173,6 +173,8 @@ class CommonTest extends PHPUnit_Framework_TestCase
array('test string', 'test string'),
array('Ο συγγραφέας', ' Ο συγγραφέας '),
array('123', ' 123 '),
array('1 2 3', '  1  2  3  ', 'UTF-8'), // ideographic spaces
array('', ' ', 'UTF-8'), // thin space and space
array('', ' '),
array('', ''),
);
@@ -764,6 +766,28 @@ class CommonTest extends PHPUnit_Framework_TestCase
return $testData;
}
public function stringsForIsJson()
{
$testData = array(
array(false, ''),
array(false, '123'),
array(true, '{"foo": "bar"}'),
array(false, '{"foo":"bar",}'),
array(false, '{"foo"}'),
array(true, '["foo"]'),
array(false, '{"foo": "bar"]'),
array(false, '123', 'UTF-8'),
array(true, '{"fòô": "bàř"}', 'UTF-8'),
array(false, '{"fòô":"bàř",}', 'UTF-8'),
array(false, '{"fòô"}', 'UTF-8'),
array(false, '["fòô": "bàř"]', 'UTF-8'),
array(true, '["fòô"]', 'UTF-8'),
array(false, '{"fòô": "bàř"]', 'UTF-8'),
);
return $testData;
}
public function stringsForIsLowerCase()
{
$testData = array(
@@ -780,6 +804,21 @@ class CommonTest extends PHPUnit_Framework_TestCase
return $testData;
}
public function stringsForIsSerialized()
{
$testData = array(
array(false, ''),
array(true, 'a:1:{s:3:"foo";s:3:"bar";}'),
array(false, 'a:1:{s:3:"foo";s:3:"bar"}'),
array(true, serialize(array('foo' => 'bar'))),
array(true, 'a:1:{s:5:"fòô";s:5:"bàř";}', 'UTF-8'),
array(false, 'a:1:{s:5:"fòô";s:5:"bàř"}', 'UTF-8'),
array(true, serialize(array('fòô' => 'bár')), 'UTF-8'),
);
return $testData;
}
public function stringsForIsUpperCase()
{
$testData = array(
@@ -796,6 +835,27 @@ class CommonTest extends PHPUnit_Framework_TestCase
return $testData;
}
public function stringsForIsHexadecimal()
{
$testData = array(
array(true, ''),
array(true, 'abcdef'),
array(true, 'ABCDEF'),
array(true, '0123456789'),
array(true, '0123456789AbCdEf'),
array(false, '0123456789x'),
array(false, 'ABCDEFx'),
array(true, 'abcdef', 'UTF-8'),
array(true, 'ABCDEF', 'UTF-8'),
array(true, '0123456789', 'UTF-8'),
array(true, '0123456789AbCdEf', 'UTF-8'),
array(false, '0123456789x', 'UTF-8'),
array(false, 'ABCDEFx', 'UTF-8'),
);
return $testData;
}
public function stringsForCount()
{
$testData = array(
@@ -831,10 +891,4 @@ class CommonTest extends PHPUnit_Framework_TestCase
return $testData;
}
// A test is required so as not to throw an error
// This is a lot cleaner than using PHPUnit's mocks to spy
public function test() {
$this->assertTrue(true);
}
}

View File

@@ -111,9 +111,9 @@ class StaticStringyTestCase extends CommonTest
/**
* @dataProvider stringsForCollapseWhitespace
*/
public function testCollapseWhitespace($expected, $str)
public function testCollapseWhitespace($expected, $str, $encoding = null)
{
$result = S::collapseWhitespace($str);
$result = S::collapseWhitespace($str, $encoding);
$this->assertInternalType('string', $result);
$this->assertEquals($expected, $result);
}
@@ -292,10 +292,20 @@ class StaticStringyTestCase extends CommonTest
*/
public function testShuffle($str, $encoding = null)
{
// We'll just make sure that the chars are present before/after shuffle
$result = S::shuffle($str, $encoding);
$encoding = $encoding ?: mb_internal_encoding();
$this->assertInternalType('string', $result);
$this->assertEquals(count_chars($str), count_chars($result));
$this->assertEquals(mb_strlen($str, $encoding),
mb_strlen($result, $encoding));
// We'll make sure that the chars are present after shuffle
for ($i = 0; $i < mb_strlen($str, $encoding); $i++) {
$char = mb_substr($str, $i, 1, $encoding);
$countBefore = mb_substr_count($str, $char, $encoding);
$countAfter = mb_substr_count($result, $char, $encoding);
$this->assertEquals($countBefore, $countAfter);
}
}
/**
@@ -461,6 +471,16 @@ class StaticStringyTestCase extends CommonTest
$this->assertInternalType('boolean', $result);
$this->assertEquals($expected, $result);
}
/**
* @dataProvider stringsForIsJson
*/
public function testIsJson($expected, $str, $encoding = null)
{
$result = S::isJson($str, $encoding);
$this->assertInternalType('boolean', $result);
$this->assertEquals($expected, $result);
}
/**
* @dataProvider stringsForIsLowerCase
@@ -472,6 +492,16 @@ class StaticStringyTestCase extends CommonTest
$this->assertEquals($expected, $result);
}
/**
* @dataProvider stringsForIsSerialized
*/
public function testIsSerialized($expected, $str, $encoding = null)
{
$result = S::isSerialized($str, $encoding);
$this->assertInternalType('boolean', $result);
$this->assertEquals($expected, $result);
}
/**
* @dataProvider stringsForIsUpperCase
*/
@@ -482,6 +512,16 @@ class StaticStringyTestCase extends CommonTest
$this->assertEquals($expected, $result);
}
/**
* @dataProvider stringsForIsHexadecimal
*/
public function testIsHexadecimal($expected, $str, $encoding = null)
{
$result = S::isHexadecimal($str, $encoding);
$this->assertInternalType('boolean', $result);
$this->assertEquals($expected, $result);
}
/**
* @dataProvider stringsForCount
*/

View File

@@ -7,6 +7,14 @@ use Stringy\Stringy as S;
class StringyTestCase extends CommonTest
{
public function testConstruct()
{
$stringy = new S('foo bar', 'UTF-8');
$this->assertInstanceOf('Stringy\Stringy', $stringy);
$this->assertEquals('foo bar', $stringy->str);
$this->assertEquals('UTF-8', $stringy->encoding);
}
public function testCreate()
{
$stringy = S::create('foo bar', 'UTF-8');
@@ -145,9 +153,9 @@ class StringyTestCase extends CommonTest
/**
* @dataProvider stringsForCollapseWhitespace
*/
public function testCollapseWhitespace($expected, $str)
public function testCollapseWhitespace($expected, $str, $encoding = null)
{
$stringy = S::create($str);
$stringy = S::create($str, $encoding);
$result = $stringy->collapseWhitespace();
$this->assertInstanceOf('Stringy\Stringy', $result);
$this->assertEquals($expected, $result);
@@ -347,12 +355,22 @@ class StringyTestCase extends CommonTest
*/
public function testShuffle($str, $encoding = null)
{
// We'll just make sure that the chars are present before/after shuffle
$stringy = S::create($str, $encoding);
$encoding = $encoding ?: mb_internal_encoding();
$result = $stringy->shuffle();
$this->assertInstanceOf('Stringy\Stringy', $result);
$this->assertEquals(count_chars($str), count_chars($result));
$this->assertEquals($str, $stringy);
$this->assertEquals(mb_strlen($str, $encoding),
mb_strlen($result, $encoding));
// We'll make sure that the chars are present after shuffle
for ($i = 0; $i < mb_strlen($str, $encoding); $i++) {
$char = mb_substr($str, $i, 1, $encoding);
$countBefore = mb_substr_count($str, $char, $encoding);
$countAfter = mb_substr_count($result, $char, $encoding);
$this->assertEquals($countBefore, $countAfter);
}
}
/**
@@ -538,6 +556,18 @@ class StringyTestCase extends CommonTest
$this->assertEquals($str, $stringy);
}
/**
* @dataProvider stringsForIsJson
*/
public function testIsJson($expected, $str, $encoding = null)
{
$stringy = S::create($str, $encoding);
$result = $stringy->isJson();
$this->assertInternalType('boolean', $result);
$this->assertEquals($expected, $result);
$this->assertEquals($str, $stringy);
}
/**
* @dataProvider stringsForIsLowerCase
*/
@@ -550,6 +580,18 @@ class StringyTestCase extends CommonTest
$this->assertEquals($str, $stringy);
}
/**
* @dataProvider stringsForIsSerialized
*/
public function testIsSerialized($expected, $str, $encoding = null)
{
$stringy = S::create($str, $encoding);
$result = $stringy->isSerialized();
$this->assertInternalType('boolean', $result);
$this->assertEquals($expected, $result);
$this->assertEquals($str, $stringy);
}
/**
* @dataProvider stringsForIsUpperCase
*/
@@ -562,6 +604,18 @@ class StringyTestCase extends CommonTest
$this->assertEquals($str, $stringy);
}
/**
* @dataProvider stringsForIsHexadecimal
*/
public function testIsHexadecimal($expected, $str, $encoding = null)
{
$stringy = S::create($str, $encoding);
$result = $stringy->isHexadecimal();
$this->assertInternalType('boolean', $result);
$this->assertEquals($expected, $result);
$this->assertEquals($str, $stringy);
}
/**
* @dataProvider stringsForCount
*/