Skip to content

Commit ef153f2

Browse files
committed
initial commit
1 parent a2e0014 commit ef153f2

13 files changed

+544
-0
lines changed

.github/workflows/build.yml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
name: build
2+
3+
on:
4+
push:
5+
branches: [ main ]
6+
pull_request:
7+
branches: [ main ]
8+
9+
jobs:
10+
test:
11+
runs-on: ubuntu-latest
12+
strategy:
13+
matrix:
14+
php: [7.4]
15+
16+
steps:
17+
- uses: actions/checkout@v2
18+
19+
- name: Validate dependencies
20+
run: composer validate
21+
22+
- name: Get dependency cache directory
23+
id: composer-cache
24+
run: echo "::set-output name=dir::$(composer config cache-files-dir)"
25+
26+
- name: Cache dependencies
27+
uses: actions/cache@v2
28+
with:
29+
path: ${{ steps.composer-cache.outputs.dir }}
30+
key: ${{ runner.os }}-php-${{ hashFiles('**/composer.json') }}
31+
restore-keys: |
32+
${{ runner.os }}-php-
33+
- name: Install dependencies
34+
run: composer install --prefer-dist --no-progress --no-suggest
35+
36+
- name: Run static analysis
37+
run: vendor/bin/phpstan analyse
38+
39+
- name: Run test suite
40+
run: vendor/bin/phpunit --configuration phpunit.xml.dist --coverage-clover=coverage.xml
41+
42+
- uses: codecov/codecov-action@v1

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.phpunit.result.cache
2+
.idea/
3+
phpunit.xml
4+
composer.lock
5+
composer.phar
6+
vendor/
7+
*.orig

NOTICE

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
modethirteen.php
2+
Copyright 2020 James Andrew Vaughn <[email protected]> (https://modethirteen.com)
3+
4+
This project includes software developed at MindTouch, Inc. <[email protected]> (https://mindtouch.com)
5+
Copyright (C) 2006-2018 MindTouch, Inc.

_bootstrap.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php declare(strict_types=1);
2+
/**
3+
* modethirteen.php
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
require_once(dirname(__FILE__) . '/vendor/autoload.php');

composer.json

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"name": "modethirteen/modethirteen.php",
3+
"description": "A collection of useful utilities and extensions for PHP",
4+
"type": "library",
5+
"license": "Apache-2.0",
6+
"authors" : [
7+
{
8+
"name": "James Andrew Vaughn",
9+
"email": "[email protected]",
10+
"role": "maintainer"
11+
}
12+
],
13+
"support": {
14+
"issues": "https://github.com/modethirteen/modethirteen.php/issues"
15+
},
16+
"require": {
17+
"php": ">=7.4.12"
18+
},
19+
"require-dev": {
20+
"phpstan/phpstan": "~0.12.57",
21+
"phpunit/phpunit": "~9.4.3"
22+
},
23+
"autoload": {
24+
"psr-4": {
25+
"modethirteen\\": ["src/"]
26+
}
27+
},
28+
"autoload-dev": {
29+
"psr-4": {
30+
"modethirteen\\Tests\\": ["tests/"]
31+
}
32+
}
33+
}

phpunit.xml.dist

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<phpunit bootstrap="_bootstrap.php">
2+
<testsuites>
3+
<testsuite name="Tests">
4+
<directory>tests</directory>
5+
</testsuite>
6+
</testsuites>
7+
<coverage processUncoveredFiles="true">
8+
<include>
9+
<directory suffix=".php">src</directory>
10+
</include>
11+
</coverage>
12+
</phpunit>

src/StringEx.php

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
<?php declare(strict_types=1);
2+
/**
3+
* modethirteen.php
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
namespace modethirteen;
18+
19+
use Closure;
20+
21+
/**
22+
* Class StringEx
23+
*
24+
* @package modethirteen
25+
*/
26+
class StringEx {
27+
28+
/**
29+
* @param string $haystack
30+
* @param string $needle
31+
* @return bool
32+
*/
33+
public static function endsWith(string $haystack, string $needle) : bool {
34+
$length = strlen($needle);
35+
$start = $length * -1;
36+
return (substr($haystack, $start) === $needle);
37+
}
38+
39+
/**
40+
* @param string $haystack
41+
* @param string $needle
42+
* @return bool
43+
*/
44+
public static function endsWithInvariantCase(string $haystack, string $needle) : bool {
45+
return self::endsWith(strtolower($haystack), strtolower($needle));
46+
}
47+
48+
/**
49+
* @param string|null $string
50+
* @return bool
51+
*/
52+
public static function isNullOrEmpty(?string $string) : bool {
53+
return $string === null || $string === '';
54+
}
55+
56+
/**
57+
* @param string $haystack
58+
* @param string $needle
59+
* @return bool
60+
*/
61+
public static function startsWith(string $haystack, string $needle) : bool {
62+
$length = strlen($needle);
63+
return (substr($haystack, 0, $length) === $needle);
64+
}
65+
66+
/**
67+
* @param string $haystack
68+
* @param string $needle
69+
* @return bool
70+
*/
71+
public static function startsWithInvariantCase(string $haystack, string $needle) : bool {
72+
return self::startsWith(strtolower($haystack), strtolower($needle));
73+
}
74+
75+
/**
76+
* Stringify any value
77+
*
78+
* @param mixed $value
79+
* @return string
80+
*/
81+
public static function stringify($value) : string {
82+
if($value === null) {
83+
return '';
84+
}
85+
if(is_string($value)) {
86+
return $value;
87+
}
88+
if(is_bool($value)) {
89+
return $value ? 'true' : 'false';
90+
}
91+
if(is_array($value)) {
92+
return implode(',', array_map(function($v) : string {
93+
return self::stringify($v);
94+
}, $value));
95+
}
96+
if($value instanceof Closure) {
97+
return self::stringify($value());
98+
}
99+
return strval($value);
100+
}
101+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php declare(strict_types=1);
2+
/**
3+
* modethirteen.php
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
namespace modethirteen\Tests\StringEx;
18+
19+
use modethirteen\StringEx;
20+
use PHPUnit\Framework\TestCase;
21+
22+
class endsWithInvariantCase_Test extends TestCase {
23+
24+
/**
25+
* @test
26+
*/
27+
public function String_starts_with() {
28+
29+
// act
30+
$result = StringEx::endsWithInvariantCase('FOObAR', 'bar');
31+
32+
// assert
33+
static::assertEquals(true, $result);
34+
}
35+
36+
/**
37+
* @test
38+
*/
39+
public function String_does_not_start_with() {
40+
41+
// act
42+
$result = StringEx::endsWithInvariantCase('foobar', 'gak');
43+
44+
// assert
45+
static::assertEquals(false, $result);
46+
}
47+
}

tests/StringEx/endsWith_Test.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php declare(strict_types=1);
2+
/**
3+
* modethirteen.php
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
namespace modethirteen\Tests\StringUtil;
18+
19+
use modethirteen\StringEx;
20+
use PHPUnit\Framework\TestCase;
21+
22+
class endsWith_Test extends TestCase {
23+
24+
/**
25+
* @test
26+
*/
27+
public function String_ends_with() : void {
28+
29+
// act
30+
$result = StringEx::endsWith('foobar', 'ar');
31+
32+
// assert
33+
static::assertEquals(true, $result);
34+
}
35+
36+
/**
37+
* @test
38+
*/
39+
public function String_does_not_end_with() : void {
40+
41+
// act
42+
$result = StringEx::endsWith('foobar', 'gak');
43+
44+
// assert
45+
static::assertEquals(false, $result);
46+
}
47+
}

tests/StringEx/isNullOrEmpty_Test.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php declare(strict_types=1);
2+
/**
3+
* modethirteen.php
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
namespace modethirteen\Tests\StringEx;
18+
19+
use modethirteen\StringEx;
20+
use PHPUnit\Framework\TestCase;
21+
22+
class isNullOrEmpty_Test extends TestCase {
23+
24+
/**
25+
* @test
26+
*/
27+
public function Is_null() : void {
28+
29+
// arrange
30+
$string = null;
31+
32+
// act
33+
$result = StringEx::isNullOrEmpty($string);
34+
35+
// assert
36+
static::assertTrue($result);
37+
}
38+
39+
/**
40+
* @test
41+
*/
42+
public function Is_empty() : void {
43+
44+
// arrange
45+
$string = '';
46+
47+
// act
48+
$result = StringEx::isNullOrEmpty($string);
49+
50+
// assert
51+
static::assertTrue($result);
52+
}
53+
54+
/**
55+
* @test
56+
*/
57+
public function Is_not_null_or_empty() : void {
58+
59+
// arrange
60+
$string = 'foo';
61+
62+
// act
63+
$result = StringEx::isNullOrEmpty($string);
64+
65+
// assert
66+
static::assertFalse($result);
67+
}
68+
}

0 commit comments

Comments
 (0)