Skip to content

Commit a863f92

Browse files
committed
add contains method
1 parent ad714b7 commit a863f92

10 files changed

+89
-9
lines changed

Diff for: NOTICE

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
modethirteen.php
1+
TypeEx
22
Copyright 2020 James Andrew Vaughn <[email protected]> (https://modethirteen.com)
33

44
This project includes software developed at MindTouch, Inc. <[email protected]> (https://mindtouch.com)

Diff for: _bootstrap.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php declare(strict_types=1);
22
/**
3-
* modethirteen.php
3+
* TypeEx
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.

Diff for: src/StringEx.php

+19-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php declare(strict_types=1);
22
/**
3-
* modethirteen.php
3+
* TypeEx
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.
@@ -93,6 +93,24 @@ public function __construct(string $string) {
9393
$this->string = $string;
9494
}
9595

96+
/**
97+
* @note when comparing a string array, returns true if one or more values match
98+
* @param string|string[] $needle
99+
* @return bool
100+
*/
101+
public function contains($needle) : bool {
102+
if(is_array($needle)) {
103+
foreach($needle as $n) {
104+
$pos = strpos($this->string, self::stringify($n));
105+
if($pos !== false) {
106+
return true;
107+
}
108+
}
109+
return false;
110+
}
111+
return strpos($this->string, self::stringify($needle)) !== false;
112+
}
113+
96114
/**
97115
* @param string $needle
98116
* @return bool

Diff for: tests/StringEx/contains_Test.php

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php declare(strict_types=1);
2+
/**
3+
* TypeEx
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\TypeEx\Tests\StringEx;
18+
19+
use modethirteen\TypeEx\StringEx;
20+
use PHPUnit\Framework\TestCase;
21+
22+
class contains_Test extends TestCase {
23+
24+
/**
25+
* @return array
26+
*/
27+
public static function string_value_expected_Provider() : array {
28+
return [
29+
['foo', 'foo', true],
30+
['foo', 'f', true],
31+
['foo', 'o', true],
32+
['foo', 'oo', true],
33+
['foo bar', 'bar', true],
34+
['foo bar', ' ', true],
35+
['foo bar', 'foo', true],
36+
['foo', 'bar', false],
37+
['foo', 'boo', false],
38+
['foo', ['foo', 'f', 'o', 'oo'], true],
39+
['foo bar', ['foo', 'bar'], true],
40+
['foo bar', ['foo', ' '], true],
41+
['foo bar', ['foo'], true],
42+
['foo', ['foo', 'bar'], true],
43+
['foo', ['boo'], false]
44+
];
45+
}
46+
47+
/**
48+
* @dataProvider string_value_expected_Provider
49+
* @test
50+
* @param string $string
51+
* @param mixed $value
52+
* @param bool $expected
53+
*/
54+
public function String_contains(string $string, $value, bool $expected) : void {
55+
56+
// act
57+
$result = (new StringEx($string))->contains($value);
58+
59+
// assert
60+
static::assertEquals($expected, $result);
61+
}
62+
}

Diff for: tests/StringEx/endsWithInvariantCase_Test.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php declare(strict_types=1);
22
/**
3-
* modethirteen.php
3+
* TypeEx
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.

Diff for: tests/StringEx/endsWith_Test.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php declare(strict_types=1);
22
/**
3-
* modethirteen.php
3+
* TypeEx
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.

Diff for: tests/StringEx/isNullOrEmpty_Test.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php declare(strict_types=1);
22
/**
3-
* modethirteen.php
3+
* TypeEx
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.

Diff for: tests/StringEx/startsWithInvariantCase_Test.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php declare(strict_types=1);
22
/**
3-
* modethirteen.php
3+
* TypeEx
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.

Diff for: tests/StringEx/startsWith_Test.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php declare(strict_types=1);
22
/**
3-
* modethirteen.php
3+
* TypeEx
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.

Diff for: tests/StringEx/stringify_Test.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php declare(strict_types=1);
22
/**
3-
* modethirteen.php
3+
* TypeEx
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.

0 commit comments

Comments
 (0)