mirror of
https://github.com/danielstjules/Stringy.git
synced 2025-08-06 13:26:40 +02:00
Start work on library
This commit is contained in:
19
LICENSE.txt
Normal file
19
LICENSE.txt
Normal file
@@ -0,0 +1,19 @@
|
||||
Copyright (C) 2013 Daniel St. Jules
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
106
README.md
106
README.md
@@ -1,4 +1,108 @@
|
||||
Stringy
|
||||
=======
|
||||
|
||||
A PHP library with a variety of string manipulation methods.
|
||||
A PHP library with a variety of multibyte string manipulation functions.
|
||||
|
||||
Usage
|
||||
-----
|
||||
|
||||
```php
|
||||
use Stringy\Stringy as S;
|
||||
```
|
||||
|
||||
**upperCaseFirst**
|
||||
|
||||
S::upperCaseFirst($string [, $encoding])
|
||||
|
||||
Converts the first character of the supplied string to upper case, with
|
||||
support for multibyte strings.
|
||||
|
||||
```php
|
||||
S::upperCaseFirst('σ test', 'UTF-8'); // 'Σ test'
|
||||
```
|
||||
|
||||
**lowerCaseFirst**
|
||||
|
||||
S::lowerCaseFirst($string [, $encoding])
|
||||
|
||||
Converts the first character of the supplied string to lower case, with
|
||||
support for multibyte strings.
|
||||
|
||||
```php
|
||||
S::lowerCaseFirst('Σ test', 'UTF-8'); // 'σ test'
|
||||
```
|
||||
|
||||
**camelize**
|
||||
|
||||
S::camelize($string [, $encoding])
|
||||
|
||||
Returns a camelCase version of a supplied string, with multibyte support.
|
||||
Trims surrounding spaces, capitalizes letters following digits, spaces,
|
||||
dashes and underscores, and removes spaces, dashes, underscores.
|
||||
|
||||
```php
|
||||
S::camelize('Camel-Case'); // 'camelCase'
|
||||
```
|
||||
|
||||
**upperCamelize**
|
||||
|
||||
S::upperCamelize($string [, $encoding])
|
||||
|
||||
Returns an UpperCamelCase version of a supplied string, with multibyte
|
||||
support. Trims surrounding spaces, capitalizes letters following digits,
|
||||
spaces, dashes and underscores, and removes spaces, dashes, underscores.
|
||||
|
||||
```php
|
||||
S::upperCamelize('Upper Camel-Case'); // 'UpperCamelCase'
|
||||
```
|
||||
|
||||
TODO
|
||||
----
|
||||
|
||||
**dasherize**
|
||||
|
||||
**underscored**
|
||||
|
||||
**swapCase**
|
||||
|
||||
**dashes**
|
||||
|
||||
**underscores**
|
||||
|
||||
**title**
|
||||
|
||||
**sentence**
|
||||
|
||||
**center**
|
||||
|
||||
**endsWith**
|
||||
|
||||
**beginsWith**
|
||||
|
||||
**toSpaces**
|
||||
|
||||
**toTabs**
|
||||
|
||||
**slugify**
|
||||
|
||||
**contains**
|
||||
|
||||
**clean**
|
||||
|
||||
**between**
|
||||
|
||||
**insert**
|
||||
|
||||
**nextChar**
|
||||
|
||||
**truncateByChars**
|
||||
|
||||
**truncateByWords**
|
||||
|
||||
**longestCommonPrefix**
|
||||
|
||||
**longestCommonSubstring**
|
||||
|
||||
**isJson**
|
||||
|
||||
**toAnchor**
|
||||
|
20
composer.json
Normal file
20
composer.json
Normal file
@@ -0,0 +1,20 @@
|
||||
{
|
||||
"name": "danielstjules/Stringy",
|
||||
"description": "A PHP library with a variety of multibyte string manipulation functions.",
|
||||
"keywords": ["multibyte", "string", "manipulation", "utf-8", "helpers", "utils"],
|
||||
"license": "MIT",
|
||||
"authors": [
|
||||
{
|
||||
"name": "Daniel St. Jules",
|
||||
"email": "danielst.jules@gmail.com",
|
||||
"homepage": "http://www.danielstjules.com"
|
||||
}
|
||||
],
|
||||
"minimum-stability": "stable",
|
||||
"require": {
|
||||
"php": ">=5.3.0"
|
||||
},
|
||||
"autoload": {
|
||||
"psr-0": { "Stringy": "src/" }
|
||||
}
|
||||
}
|
11
phpunit.xml.dist
Normal file
11
phpunit.xml.dist
Normal file
@@ -0,0 +1,11 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<phpunit processIsolation="false"
|
||||
stopOnFailure="false"
|
||||
syntaxCheck="false">
|
||||
<testsuites>
|
||||
<testsuite name="Stringy">
|
||||
<directory>tests/Stringy</directory>
|
||||
</testsuite>
|
||||
</testsuites>
|
||||
</phpunit>
|
111
src/Stringy/Stringy.php
Normal file
111
src/Stringy/Stringy.php
Normal file
@@ -0,0 +1,111 @@
|
||||
<?php
|
||||
|
||||
namespace Stringy;
|
||||
|
||||
class Stringy {
|
||||
|
||||
/**
|
||||
* Checks that the first argument ($ars[0]) supplied to the private method
|
||||
* is a string, and throws an exception if not. And if not provided, sets
|
||||
* the second argument ($ars[1]) to mb_internal_encoding(). It then calls
|
||||
* the static method.
|
||||
*
|
||||
* @param string $method Private static method being called
|
||||
* @param array $args Array of arguments supplied to the method call
|
||||
* @return string String returned by the private method called
|
||||
* @throws BadMethodCallException If $method doesn't exist
|
||||
* @throws InvalidArgumentException If $args[0] is not of type string
|
||||
*/
|
||||
public static function __callStatic($method, $args) {
|
||||
if (!method_exists(__CLASS__, $method))
|
||||
throw new \BadMethodCallException("Method doesn't exist");
|
||||
|
||||
if (!is_string($args[0])) {
|
||||
// Scalar type hinting isn't allowed, so have to throw exceptions
|
||||
$message = sprintf('Argument of type string expected, instead ' .
|
||||
'received an argument of type %s', gettype($args[0]));
|
||||
|
||||
throw new \InvalidArgumentException($message, $args[0]);
|
||||
}
|
||||
|
||||
// Set the character encoding ($args[1]/$encoding) if not provided
|
||||
if (sizeof($args) == 1 || !$args[1])
|
||||
$args[1] = mb_internal_encoding();
|
||||
|
||||
return forward_static_call(array(__CLASS__, $method), $args[0], $args[1]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the first character of the supplied string to upper case, with
|
||||
* support for multibyte strings.
|
||||
*
|
||||
* @param string $string String to modify
|
||||
* @param string $encoding The character encodng
|
||||
* @return string String with the first character being upper case
|
||||
*/
|
||||
private static function upperCaseFirst($string, $encoding) {
|
||||
$first = mb_substr($string, 0, 1, $encoding);
|
||||
$rest = mb_substr($string, 1, mb_strlen($string, $encoding) - 1, $encoding);
|
||||
|
||||
return mb_strtoupper($first, $encoding) . $rest;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the first character of the supplied string to lower case, with
|
||||
* support for multibyte strings.
|
||||
*
|
||||
* @param string $string String to modify
|
||||
* @param string $encoding The character encodng
|
||||
* @return string String with the first character being lower case
|
||||
*/
|
||||
private static function lowerCaseFirst($string, $encoding) {
|
||||
$first = mb_substr($string, 0, 1, $encoding);
|
||||
$rest = mb_substr($string, 1, mb_strlen($string, $encoding) - 1, $encoding);
|
||||
|
||||
return mb_strtolower($first, $encoding) . $rest;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a camelCase version of a supplied string, with multibyte support.
|
||||
* Trims surrounding spaces, capitalizes letters following digits, spaces,
|
||||
* dashes and underscores, and removes spaces, dashes, underscores.
|
||||
*
|
||||
* @param string $string String to convert to camelCase
|
||||
* @return string String in camelCase
|
||||
*/
|
||||
private static function camelize($string, $encoding) {
|
||||
$camelCase = preg_replace_callback(
|
||||
'/[-_\s]+(.)?/u',
|
||||
function ($matches) use (&$encoding) {
|
||||
return $matches[1] ? mb_strtoupper($matches[1], $encoding) : "";
|
||||
},
|
||||
self::lowerCaseFirst(trim($string), $encoding)
|
||||
);
|
||||
|
||||
$camelCase = preg_replace_callback(
|
||||
'/[\d]+(.)?/u',
|
||||
function ($matches) use (&$encoding) {
|
||||
return mb_strtoupper($matches[0], $encoding);
|
||||
},
|
||||
$camelCase
|
||||
);
|
||||
|
||||
return $camelCase;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an UpperCamelCase version of a supplied string, with multibyte
|
||||
* support. Trims surrounding spaces, capitalizes letters following digits,
|
||||
* spaces, dashes and underscores, and removes spaces, dashes, underscores.
|
||||
*
|
||||
* @param string $string String to convert to UpperCamelCase
|
||||
* @return string String in UpperCamelCase
|
||||
*/
|
||||
private static function upperCamelize($string, $encoding) {
|
||||
$camelCase = self::camelize($string, $encoding);
|
||||
|
||||
return self::upperCaseFirst($camelCase, $encoding);
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
116
tests/Stringy/StringyTest.php
Normal file
116
tests/Stringy/StringyTest.php
Normal file
@@ -0,0 +1,116 @@
|
||||
<?php
|
||||
|
||||
$base = realpath(dirname(__FILE__) . '/../..');
|
||||
require("$base/src/Stringy/Stringy.php");
|
||||
|
||||
use Stringy\Stringy as S;
|
||||
|
||||
class StringyTestCase extends PHPUnit_Framework_TestCase {
|
||||
|
||||
/**
|
||||
* @expectedException BadMethodCallException
|
||||
*/
|
||||
public function testExceptionOnUndefinedMethod() {
|
||||
S::doesntExist('test', 'UTF-8');
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException InvalidArgumentException
|
||||
*/
|
||||
public function testExceptionWithInvalidArgument() {
|
||||
S::camelize(1, 'UTF-8');
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForUpperCaseFirst
|
||||
*/
|
||||
public function testUpperCaseFirst($string, $expected, $encoding = null) {
|
||||
$result = S::upperCaseFirst($string, $encoding);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
public function stringsForUpperCaseFirst() {
|
||||
$testData = array(
|
||||
array('Test', 'Test'),
|
||||
array('test', 'Test'),
|
||||
array('σ test', 'Σ test', 'UTF-8')
|
||||
);
|
||||
|
||||
return $testData;
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForLowerCaseFirst
|
||||
*/
|
||||
public function testLowerCaseFirst($string, $expected, $encoding = null) {
|
||||
$result = S::lowerCaseFirst($string, $encoding);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
public function stringsForLowerCaseFirst() {
|
||||
$testData = array(
|
||||
array('Test', 'test'),
|
||||
array('test', 'test'),
|
||||
array('Σ test', 'σ test', 'UTF-8')
|
||||
);
|
||||
|
||||
return $testData;
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForCamelize
|
||||
*/
|
||||
public function testCamelize($string, $expected, $encoding = null) {
|
||||
$result = S::camelize($string, $encoding);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
public function stringsForCamelize() {
|
||||
$testData = array(
|
||||
array('CamelCase', 'camelCase'),
|
||||
array('Camel-Case', 'camelCase'),
|
||||
array('camel case', 'camelCase'),
|
||||
array('camel -case', 'camelCase'),
|
||||
array('camel - case', 'camelCase'),
|
||||
array('camel_case', 'camelCase'),
|
||||
array('camel c test', 'camelCTest'),
|
||||
array('string_with1number', 'stringWith1Number'),
|
||||
array('string-with-2-2 numbers', 'stringWith22Numbers'),
|
||||
array('camel σase', 'camelΣase', 'UTF-8'),
|
||||
array('Σamel case', 'σamelCase', 'UTF-8'),
|
||||
array('σamel Case', 'σamelCase', 'UTF-8')
|
||||
);
|
||||
|
||||
return $testData;
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForUpperCamelize
|
||||
*/
|
||||
public function testUpperCamelize($string, $expected, $encoding = null) {
|
||||
$result = S::upperCamelize($string, $encoding);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
public function stringsForUpperCamelize() {
|
||||
$testData = array(
|
||||
array('camelCase', 'CamelCase'),
|
||||
array('Camel-Case', 'CamelCase'),
|
||||
array('camel case', 'CamelCase'),
|
||||
array('camel -case', 'CamelCase'),
|
||||
array('camel - case', 'CamelCase'),
|
||||
array('camel_case', 'CamelCase'),
|
||||
array('camel c test', 'CamelCTest'),
|
||||
array('string_with1number', 'StringWith1Number'),
|
||||
array('string-with-2-2 numbers', 'StringWith22Numbers'),
|
||||
array('camel σase', 'CamelΣase', 'UTF-8'),
|
||||
array('σamel case', 'ΣamelCase', 'UTF-8'),
|
||||
array('Σamel Case', 'ΣamelCase', 'UTF-8')
|
||||
);
|
||||
|
||||
return $testData;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
Reference in New Issue
Block a user