The getContents method reads the full content of the file. If you want to read only a certain number of bytes, you can use the native \SplObject::fread() instead.
Getting Started
Install the library via composer:
composer require susina/utils
and you're ready to go!
The library provides two classes: StringUtils and File. The first one is a utility class for string manipulation, while the second one is a utility class for file handling.
Let's explore them in detail.
StringUtils class
Susina\Utils\StringUtils
class provides utility static methods to manipulate strings,
not included into the standard PHP functions list.
- toKebabCase method: Converts a string to kebab-case.
- toSnakeCase method: Converts a string to snake_case.
- toStudlyCase method: Converts a string to StudlyCase.
- toCamelCase method: Converts a string to camelCase.
- toPascalCase method: alias of toStudlyCase method.
- ensureStart method: Ensures that a string starts with a specific character.
- ensureEnd method: Ensures that a string ends with a specific character.
- collapseSpaces method: Collapses multiple spaces into a single space.
Why static methods? Starting from PHP 8.5, the language has introduced the new pipe operator, allowing chaining functions and methods in a more functional programming style. The static methods of this library are designed to be used with this new operator, enabling a more concise and expressive way to manipulate strings.
If you prefer the object-oriented fluent api approach, you can use the Text
class, included into phootwork/lang package,
or the UnicodeString
class, from symfony/string library, instead.
toKebabCase method
toKebabCase method converts a string to kebab-case, which is a lowercase string with words separated by hyphens.
use Susina\Utils\StringUtils;
$kebabCase = StringUtils::toKebabCase('Hello World'); // hello-world
$kebabCase = StringUtils::toKebabCase('HelloWorld'); // hello-world
$kebabCase = StringUtils::toKebabCase('hello_world'); // hello-world
$kebabCase = StringUtils::toKebabCase('hello world'); // hello-world
$kebabCase = StringUtils::toKebabCase('helloWorld'); // hello-world
toSnakeCase method
toSnakeCase method converts a string to snake_case, which is a lowercase string with words separated by underscores.
use Susina\Utils\StringUtils;
$snakeCase = StringUtils::toSnakeCase('Hello World'); // hello_world
$snakeCase = StringUtils::toSnakeCase('HelloWorld'); // hello_world
$snakeCase = StringUtils::toSnakeCase('hello-world'); // hello_world
$snakeCase = StringUtils::toSnakeCase('hello world'); // hello_world
$snakeCase = StringUtils::toSnakeCase('helloWorld'); // hello_world
toStudlyCase method
toStudlyCase method converts a string to StudlyCase, which is a string with the first letter of each word capitalized and no spaces or separators.
use Susina\Utils\StringUtils;
$studlyCase = StringUtils::toStudlyCase('hello world'); // HelloWorld
$studlyCase = StringUtils::toStudlyCase('hello-world'); // HelloWorld
$studlyCase = StringUtils::toStudlyCase('hello_world'); // HelloWorld
$studlyCase = StringUtils::toStudlyCase('helloWorld'); // HelloWorld
toPascalCase method
Alias of toStudlyCase method.
toCamelCase method
toCamelCase method converts a string to camelCase, which is a string with the first letter of the first word in lowercase and the first letter of each subsequent word capitalized, with no spaces or separators.
use Susina\Utils\StringUtils;
$camelCase = StringUtils::toCamelCase('hello world'); // helloWorld
$camelCase = StringUtils::toCamelCase('hello-world'); // helloWorld
$camelCase = StringUtils::toCamelCase('hello_world'); // helloWorld
$camelCase = StringUtils::toCamelCase('HelloWorld'); // helloWorld
ensureStart method
ensureStart method ensures that a string starts with a specific character or string.
use Susina\Utils\StringUtils;
// Adds the leading slash
$var = StringUtils::ensureStart('my/personal/path', '/'); // /my/personal/path
// Does not add the leading slash, as it is already present
$var = StringUtils::ensureStart('/my/personal/path', '/'); // /my/personal/path
ensureEnd method
ensureEnd method ensures that a string ends with a specific character or string.
use Susina\Utils\StringUtils;
// Adds the trailing slash
$var = StringUtils::ensureEnd('my/personal/path', '/'); // my/personal/path/
// Does not add the trailing slash, as it is already present
$var = StringUtils::ensureEnd('my/personal/path/', '/'); // my/personal/path/
collapseSpaces method
collapseSpaces method collapses multiple spaces into a single space.
use Susina\Utils\StringUtils;
$var = StringUtils::collapseSpaces(' hello world '); // hello world
File class
File class is an extension of the standard PHP SplFileInfo , providing additional utility methods for file handling and supporting relative paths.
Let's get a look in detail.
Relative paths
Relative paths can be set into the constructor or using the makeRelative method.
The class constructor accepts two parameters: the file path and the relative path . If the relative path is not set, the file path is considered relative to the root directory, which is the same behavior of the base SplFileInfo::_construct().
use Susina\Utils\File;
// only one argument: same behavior as standard PHP SplFileInfo
$file = new File('var/www/html/index.php');
// index.php is relative to /home/james
$relativeFile = new File('index.php', '/home/james');
Now you can access the relative path and relative pathname of the file, via the specific methods getRelativePath and getRelativePathname , while the getPath and getPathname will return the absolute path. In example:
use Susina\Utils\File;
$file = new File('index.php', '/home/james');
$file->getRelativePath(); // /home/james/ (please, note the trailing slash)
$file->getRelativePathname(); // index.php
$file->getPath(); // /home/james
$file->getPathname() // /home/james/index.php
You can make a file relative to a directory by using makeRelativeTo method:
use Susina\Utils\File;
$file = new File('/home/james/index.php');
$file->getRelativePath(); // ''
$file->getRelativePathname(); // /home/james/index.php
$file->getPath(); // /home/james
$file->getPathname() // /home/james/index.php
// makeRelativeTo returns a File object
$relativeFile = $file->makeRelativeTo('/home/james');
$relativeFile->getRelativePath(); // /home/james/
$relativeFile->getRelativePathname(); // index.php
$relativeFile->getPath(); // /home/james
$relativeFile->getPathname() // /home/james/index.php
The File class also provides the isAbsolute method which try to determine if a path is absolute by using the following these rules:
- If the path contains a colon, it is considered absolute (Windows absolute paths have the form
C:\my_path). - If the realpath of the path is the same as the path, it is considered absolute.
- If the path starts with a slash, it is considered absolute (Unix absolute paths).
- If the path starts with a backslash, it is considered absolute (Windows UNC).
use Susina\Utils\File;
$file = new File('/my/path')->isAbsolute(); // true
$file = new File('C:\my\path')->isAbsolute(); // true
$file = new File('\my\path')->isAbsolute(); // true
$file = new File('my/path')->isAbsolute(); // false
Content manipulation methods
The File class also ships three methods to manipulate the file content:
- getContents to read the file content into a string.
- putContents to write a string into the file, overwriting it. If the file does not exist, this method creates it.
- appendContents to append a string at the end of the file. If the file does not exist, this method creates it.
When an error occurs, these methods throw a RuntimeException
instead of returning false
as the standard PHP functions do.
Example 1) read the full content of the file:
use Susina\Utils\File;
$file = new File('myfile.txt');
$content = $file->getContents();
// if any errors, a RuntimeException is thrown
Example 2) read only the first 100 bytes of the file, by using the standard fread method:
use Susina\Utils\File;
$file = new File('myfile.txt');
$content = $file->openFile('r')->fread(100);
// if any errors, no exception is thrown and $contents contains the boolean `false`