Skip to content

Commit

Permalink
Merge pull request #13 from niden/1.x
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeckerson authored Oct 4, 2022
2 parents 9751773 + 5f79feb commit 2a67d1e
Show file tree
Hide file tree
Showing 9 changed files with 407 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:

runs-on: ubuntu-20.04
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3

- name: Run PHP_CodeSniffer
run: docker run --rm -v $(pwd):/data cytopia/phpcs --standard=./config/phpcs.xml
Expand All @@ -38,7 +38,7 @@ jobs:
# php-versions: ['7.4', '8.0']
#
# steps:
# - uses: actions/checkout@v2
# - uses: actions/checkout@v3
#
# - name: Validate stubs
# if: always()
Expand All @@ -52,7 +52,7 @@ jobs:

runs-on: ubuntu-20.04
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3

- name: Setup PHP
uses: shivammathur/setup-php@v2
Expand Down Expand Up @@ -88,11 +88,11 @@ jobs:
fail-fast: false
matrix:
operating-system: [ubuntu-20.04]
php-versions: ['7.4', '8.0']
php-versions: ['7.4', '8.0', '8.1']

steps:
- name: Checkout Code
uses: actions/checkout@v2
uses: actions/checkout@v3
with:
fetch-depth: 1

Expand Down Expand Up @@ -133,7 +133,8 @@ jobs:
run: vendor/bin/codecept run --coverage-xml=coverage-unit-${{ matrix.php-versions }}.xml --ext DotReporter unit

- name: Upload Code Coverage on ${{ matrix.php-versions }} for unit
uses: codecov/codecov-action@v2
uses: codecov/codecov-action@v3
with:
token: ${{ secrets.CODECOV_TOKEN }}
directory: ./tests/_output/
files: coverage-unit-7.4.xml,coverage-unit-8.0.xml,coverage-unit-8.1.xml
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## Phalcon Traits

[![Phalcon CI](https://github.com/phalcon/traits/actions/workflows/build-and-test.yml/badge.svg?branch=1.x)](https://github.com/niden/traits/actions/workflows/build-and-test.yml)
[![Phalcon CI](https://github.com/phalcon/traits/actions/workflows/main.yml/badge.svg?branch=1.x)](https://github.com/niden/traits/actions/workflows/main.yml)
[![PDS Skeleton](https://img.shields.io/badge/pds-skeleton-blue.svg?style=flat-square)](https://github.com/php-pds/skeleton)
[![codecov](https://codecov.io/gh/phalcon/traits/branch/1.x/graph/badge.svg?token=I4bgs0E168)](https://codecov.io/gh/phalcon/traits)
[![Commitizen friendly](https://img.shields.io/badge/commitizen-friendly-brightgreen.svg)](http://commitizen.github.io/cz-cli/)
Expand Down
48 changes: 48 additions & 0 deletions docs/general.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,31 @@ $filtered = $this->toFilter(
#### Str
Namespace containing traits relevant to string manipulation and processing.

##### CamelizeTrait

```php
/**
* @param string $text
* @param string|null $delimiters
* @param bool $lowerFirst
*
* @return string
*/
public function toCamelize(
string $text,
string $delimiters = '\-_',
bool $lowerFirst = false
): string
```
Accepts a string and camelizes it based on the passed delimiter (or the
default one). It also allows the developer to lowercase the first character.

**Example**
```php
echo $this->toCamelize('came_li_ze');
// CameLiZe
```

##### DirFromFileTrait

```php
Expand Down Expand Up @@ -228,6 +253,29 @@ var_dump($this->toStartsWith('Hello', 'h', true);
// true
````

##### UncamelizeTrait

```php
/**
* @param string $text
* @param string $delimiters
*
* @return string
*/
public function toUncamelize(
string $text,
string $delimiter = '_'
): string
```
Accepts a string and uncamelizes it based on the passed delimiter (or the
default one)

**Example**
```php
echo $this->toUncamelize('CameLiZe');
// came_li_ze
```

##### UpperTrait
Converts the passed string to uppercase using the `mbstring` extension.
```php
Expand Down
85 changes: 85 additions & 0 deletions src/Helper/Str/CamelizeTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

/**
* This file is part of the Phalcon.
*
* (c) Phalcon Team <team@phalcon.io>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Phalcon\Traits\Helper\Str;

use function array_map;
use function implode;
use function lcfirst;
use function preg_split;
use function str_replace;
use function ucfirst;

use const PREG_SPLIT_DELIM_CAPTURE;
use const PREG_SPLIT_NO_EMPTY;

/**
* Converts strings to upperCamelCase or lowerCamelCase
*/
trait CamelizeTrait
{
/**
* @param string $text
* @param string $delimiters
* @param bool $lowerFirst
*
* @return string
*/
public function toCamelize(
string $text,
string $delimiters = '\-_',
bool $lowerFirst = false
): string {
$exploded = $this->processArray($text, $delimiters);

$output = array_map(
function ($element) {
return ucfirst(mb_strtolower($element));
},
$exploded
);

$result = implode('', $output);

if (true === $lowerFirst) {
$result = lcfirst($result);
}

return $result;
}

/**
* @param string $text
* @param string|null $delimiters
*
* @return array
*/
protected function processArray(string $text, string $delimiters = '\-_'): array
{
/**
* Escape the `-` if it exists so that it does not get interpreted
* as a range. First remove any escaping for the `-` if present and then
* add it again - just to be on the safe side
*/
$delimiters = str_replace(['\-', '-'], ['-', '\-'], $delimiters);

$result = preg_split(
'/[' . $delimiters . ']+/',
$text,
-1,
PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY
);

return (false === $result) ? [] : $result;
}
}
39 changes: 39 additions & 0 deletions src/Helper/Str/UncamelizeTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

/**
* This file is part of the Phalcon.
*
* (c) Phalcon Team <team@phalcon.io>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Phalcon\Traits\Helper\Str;

/**
* Converts strings to non camelized style
*/
trait UncamelizeTrait
{
/**
* @param string $text
* @param string $delimiters
*
* @return string
*/
public function toUncamelize(
string $text,
string $delimiter = '_'
): string {
$text = (string) preg_replace(
'/[A-Z]/',
$delimiter . '\\0',
lcfirst($text)
);

return mb_convert_case($text, MB_CASE_LOWER, 'UTF-8');
}
}
38 changes: 38 additions & 0 deletions tests/_data/fixtures/Helper/Str/CamelizeFixture.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

/**
* This file is part of the Phalcon Framework.
*
* (c) Phalcon Team <team@phalcon.io>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Phalcon\Tests\Fixtures\Helper\Str;

use Phalcon\Traits\Helper\Str\CamelizeTrait;

class CamelizeFixture
{
use CamelizeTrait;

/**
* Camelizes a string
*
* @param string $text
* @param string $delimiters
* @param bool $lowerFirst
*
* @return string
*/
public function camelize(
string $text,
string $delimiters,
bool $lowerFirst
): string {
return $this->toCamelize($text, $delimiters, $lowerFirst);
}
}
36 changes: 36 additions & 0 deletions tests/_data/fixtures/Helper/Str/UncamelizeFixture.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

/**
* This file is part of the Phalcon Framework.
*
* (c) Phalcon Team <team@phalcon.io>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Phalcon\Tests\Fixtures\Helper\Str;

use Phalcon\Traits\Helper\Str\UncamelizeTrait;

class UncamelizeFixture
{
use UncamelizeTrait;

/**
* Uncamelizes a string
*
* @param string $text
* @param string $delimiters
*
* @return string
*/
public function Uncamelize(
string $text,
string $delimiter = '_'
): string {
return $this->toUncamelize($text, $delimiter);
}
}
Loading

0 comments on commit 2a67d1e

Please sign in to comment.