Skip to content
This repository has been archived by the owner on Oct 28, 2020. It is now read-only.

Commit

Permalink
Provide new function names, according to changes to the RFC
Browse files Browse the repository at this point in the history
  • Loading branch information
ramsey committed Jul 4, 2019
1 parent ec7a79d commit 8764e72
Show file tree
Hide file tree
Showing 29 changed files with 966 additions and 267 deletions.
25 changes: 25 additions & 0 deletions CHANGELOG.md
Expand Up @@ -10,10 +10,34 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

### Added

Added the following functions to accommodate [recent changes][rfc-0.3] to the
[proposed RFC][rfc].

* `mb_str_ends_with()`
* `mb_str_ends_with_ci()`
* `mb_str_starts_with()`
* `mb_str_starts_with_ci()`
* `str_ends_with()`
* `str_ends_with_ci()`
* `str_starts_with()`
* `str_starts_with_ci()`

### Changed

### Deprecated

Since the following are no longer specified in the [RFC][], they have been
deprecated in this polyfill library.

* `mb_str_begins()`
* `mb_str_ends()`
* `mb_str_ibegins()`
* `mb_str_iends()`
* `str_begins()`
* `str_ends()`
* `str_ibegins()`
* `str_iends()`

### Removed

### Fixed
Expand All @@ -38,6 +62,7 @@ functions][rfc]."
* `str_iends()`


[rfc-0.3]: https://wiki.php.net/rfc/add_str_begin_and_end_functions?do=diff&rev2%5B0%5D=1506086901&rev2%5B1%5D=1562258308&difftype=sidebyside
[rfc]: https://wiki.php.net/rfc/add_str_begin_and_end_functions
[Unreleased]: https://github.com/ramsey/str-begins-ends/compare/1.0.0...HEAD
[1.0.0]: https://github.com/ramsey/str-begins-ends/commits/1.0.0
56 changes: 28 additions & 28 deletions README.md
Expand Up @@ -35,10 +35,10 @@ cause conflicts in any project using it, should PHP decide to adopt and
implement the [RFC][] in a future version.


## str_begins
## str_starts_with

``` php
str_begins(string $haystack , string $needle): bool
str_starts_with(string $haystack , string $needle): bool
```

Performs a *case-sensitive* check to determine whether `$haystack` begins with
Expand All @@ -50,16 +50,16 @@ Performs a *case-sensitive* check to determine whether `$haystack` begins with
``` php
$url = 'http://example.com';

if (str_begins($url, 'http://')) {
if (str_starts_with($url, 'http://')) {
$url = str_replace('http://', 'https://', $url);
}
```


## str_ends
## str_ends_with

``` php
str_ends(string $haystack , string $needle): bool
str_ends_with(string $haystack , string $needle): bool
```

Performs a *case-sensitive* check to determine whether `$haystack` ends with
Expand All @@ -71,16 +71,16 @@ Performs a *case-sensitive* check to determine whether `$haystack` ends with
``` php
$file = '/path/to/something.log';

if (str_ends($file, '.log')) {
if (str_ends_with($file, '.log')) {
$contents = file_get_contents($file);
}
```


## str_ibegins
## str_begins_with_ci

``` php
str_ibegins(string $haystack , string $needle): bool
str_begins_with_ci(string $haystack , string $needle): bool
```

Performs a *case-insensitive* check to determine whether `$haystack` begins with
Expand All @@ -92,16 +92,16 @@ Performs a *case-insensitive* check to determine whether `$haystack` begins with
``` php
$url = 'HTTPS://example.com';

if (str_ibegins($url, 'https://')) {
if (str_begins_with_ci($url, 'https://')) {
$url = substr($url, 8);
}
```


## str_iends
## str_ends_with_ci

``` php
str_iends(string $haystack , string $needle): bool
str_ends_with_ci(string $haystack , string $needle): bool
```

Performs a *case-insensitive* check to determine whether `$haystack` ends with
Expand All @@ -113,19 +113,19 @@ Performs a *case-insensitive* check to determine whether `$haystack` ends with
``` php
$file = '/path/to/something.TXT';

if (str_iends($file, '.txt')) {
if (str_ends_with_ci($file, '.txt')) {
$contents = file_get_contents($file);
}
```


## mb_str_begins
## mb_str_starts_with

``` php
mb_str_begins(string $haystack , string $needle [, string $encoding = mb_internal_encoding()]): bool
mb_str_starts_with(string $haystack , string $needle [, string $encoding = mb_internal_encoding()]): bool
```

Performs a *case-sensitive*, multi-byte safe `str_begins()` operation to check
Performs a *case-sensitive*, multi-byte safe `str_starts_with()` operation to check
whether `$haystack` begins with `$needle`.

> This function is only available if the [mbstring extension][] is installed.
Expand All @@ -136,19 +136,19 @@ whether `$haystack` begins with `$needle`.
``` php
$runePoem = 'ᚠᛇᚻ᛫ᛒᛦᚦ᛫ᚠᚱᚩᚠᚢᚱ᛫ᚠᛁᚱᚪ᛫ᚷᛖᚻᚹᛦᛚᚳᚢᛗ';

if (mb_str_begins($runePoem, 'ᚠᛇᚻ')) {
if (mb_str_starts_with($runePoem, 'ᚠᛇᚻ')) {
$poem = 'Wealth is a comfort to all men';
}
```


## mb_str_ends
## mb_str_ends_with

``` php
mb_str_ends(string $haystack , string $needle [, string $encoding = mb_internal_encoding()]): bool
mb_str_ends_with(string $haystack , string $needle [, string $encoding = mb_internal_encoding()]): bool
```

Performs a *case-sensitive*, multi-byte safe `str_ends()` operation to check
Performs a *case-sensitive*, multi-byte safe `str_ends_with()` operation to check
whether `$haystack` ends with `$needle`.

> This function is only available if the [mbstring extension][] is installed.
Expand All @@ -159,19 +159,19 @@ whether `$haystack` ends with `$needle`.
``` php
$sanskrit = 'काचं शक्नोम्यत्तुम् । नोपहिनस्ति माम् ॥';

if (mb_str_ends($sanskrit, 'माम् ॥')) {
if (mb_str_ends_with($sanskrit, 'माम् ॥')) {
$translation = 'I can eat glass';
}
```


## mb_str_ibegins
## mb_str_begins_with_ci

``` php
mb_str_ibegins(string $haystack , string $needle [, string $encoding = mb_internal_encoding()]): bool
mb_str_begins_with_ci(string $haystack , string $needle [, string $encoding = mb_internal_encoding()]): bool
```

Performs a *case-insensitive*, multi-byte safe `str_ibegins()` operation to check
Performs a *case-insensitive*, multi-byte safe `str_begins_with_ci()` operation to check
whether `$haystack` begins with `$needle`.

> This function is only available if the [mbstring extension][] is installed.
Expand All @@ -182,19 +182,19 @@ whether `$haystack` begins with `$needle`.
``` php
$poem = 'Τὴ γλῶσσα μοῦ ἔδωσαν ἑλληνικὴ';

if (mb_str_ibegins($poem, 'ΤῊ')) {
if (mb_str_begins_with_ci($poem, 'ΤῊ')) {
$poet = 'Οδυσσέας Ελύτης';
}
```


## mb_str_iends
## mb_str_ends_with_ci

``` php
mb_str_iends(string $haystack , string $needle [, string $encoding = mb_internal_encoding()]): bool
mb_str_ends_with_ci(string $haystack , string $needle [, string $encoding = mb_internal_encoding()]): bool
```

Performs a *case-insensitive*, multi-byte safe `str_iends()` operation to check
Performs a *case-insensitive*, multi-byte safe `str_ends_with_ci()` operation to check
whether `$haystack` ends with `$needle`.

> This function is only available if the [mbstring extension][] is installed.
Expand All @@ -205,7 +205,7 @@ whether `$haystack` ends with `$needle`.
``` php
$poem = 'Τὴ γλῶσσα μοῦ ἔδωσαν ἑλληνικὴ';

if (mb_str_iends($poem, 'ἙΛΛΗΝΙΚῊ')) {
if (mb_str_ends_with_ci($poem, 'ἙΛΛΗΝΙΚῊ')) {
$poet = 'Οδυσσέας Ελύτης';
}
```
Expand Down
27 changes: 17 additions & 10 deletions composer.json
@@ -1,17 +1,17 @@
{
"name": "ramsey/str-begins-ends",
"description": "Provides functions to test whether a string begins or ends with a certain substring.",
"description": "Provides functions to test whether a string starts or ends with a certain substring.",
"type": "library",
"keywords": [
"mb_str_begins",
"mb_str_ends",
"mb_str_ibegins",
"mb_str_iends",
"mb_str_ends_with",
"mb_str_ends_with_ci",
"mb_str_starts_with",
"mb_str_starts_with_ci",
"polyfill",
"str_begins",
"str_ends",
"str_ibegins",
"str_iends",
"str_ends_with",
"str_ends_with_ci",
"str_starts_with",
"str_starts_with_ci",
"strings"
],
"homepage": "https://github.com/ramsey/str-begins-ends",
Expand All @@ -34,12 +34,19 @@
"require-dev": {
"jakub-onderka/php-parallel-lint": "^1",
"mockery/mockery": "^0.9.11",
"php-mock/php-mock-mockery": "^1.1",
"phpunit/phpunit": "^4.8 || ^5",
"squizlabs/php_codesniffer": "^3"
},
"suggest": {
"ext-mbstring": "Needed to support the mb_str_* functions provided."
},
"autoload": {
"psr-4": {
"Ramsey\\String\\": "src/Ramsey/String/"
},
"files": [
"src/helpers.php",
"src/Ramsey/String/helpers.php",
"src/mb_str_begins.php",
"src/mb_str_ends.php",
"src/mb_str_ibegins.php",
Expand Down
50 changes: 50 additions & 0 deletions src/Ramsey/String/AbstractString.php
@@ -0,0 +1,50 @@
<?php
/**
* This file is part of the ramsey/str-begins-ends library
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @copyright Copyright (c) Ben Ramsey <ben@benramsey.com>
* @license http://opensource.org/licenses/MIT MIT
*/

namespace Ramsey\String;

abstract class AbstractString implements StringInterface
{
/**
* @var string
*/
private $value;

/**
* @param string $value
*/
public function __construct($value)
{
$this->value = $this->checkString($value, 1);
}

/**
* @return string
*/
public function __toString()
{
return (string) $this->value;
}

/**
* @param mixed $value
* @param int $position
* @return StringValue
*/
protected function checkString($value, $position)
{
if (is_scalar($value) || (is_object($value) && method_exists($value, '__toString'))) {
return strval($value);
}

throw new InvalidStringArgumentException($value, $position);
}
}

0 comments on commit 8764e72

Please sign in to comment.