Skip to content

Commit ccd7c73

Browse files
committed
Added strCompare() and deprecate compare()
1 parent 4bf1f33 commit ccd7c73

File tree

3 files changed

+101
-73
lines changed

3 files changed

+101
-73
lines changed

README.md

Lines changed: 49 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,6 @@ will return:
140140
<a href="#col">col</a>
141141
<a href="#collapse">collapse</a>
142142
<a href="#combine">combine</a>
143-
<a href="#compare">compare</a>
144143
<a href="#concat">concat</a>
145144
<a href="#contains">contains</a>
146145
<a href="#copy">copy</a>
@@ -257,6 +256,7 @@ will return:
257256
<a href="#splice">splice</a>
258257
<a href="#strafter">strAfter</a>
259258
<a href="#strbefore">strBefore</a>
259+
<a href="#strcompare">strCompare</a>
260260
<a href="#strcontains">strContains</a>
261261
<a href="#strcontainsall">strContainsAll</a>
262262
<a href="#strends">strEnds</a>
@@ -436,7 +436,6 @@ will return:
436436

437437
* [function is_map()](#is_map-function) : Tests if the variable is a map object
438438
* [any()](#any) : Tests if at least one element satisfies the callback function
439-
* [compare()](#compare) : Compares the value against all map elements
440439
* [contains()](#contains) : Tests if an item exists in the map
441440
* [each()](#each) : Applies a callback to each element
442441
* [empty()](#empty) : Tests if map is empty
@@ -459,6 +458,7 @@ will return:
459458
* [implements()](#implements) : Tests if all entries are objects implementing the interface
460459
* [none()](#none) : Tests if none of the elements are part of the map
461460
* [some()](#some) : Tests if at least one element is included
461+
* [strCompare()](#strcompare) : Compares the value against all map elements
462462
* [strContains()](#strcontains) : Tests if at least one of the passed strings is part of at least one entry
463463
* [strContainsAll()](#strcontainsall) : Tests if all of the entries contains one of the passed strings
464464
* [strEnds()](#strends) : Tests if at least one of the entries ends with one of the passed strings
@@ -1440,53 +1440,6 @@ Map::from( ['name', 'age'] )->combine( ['Tom', 29] );
14401440
* [zip()](#zip) - Merges the values of all arrays at the corresponding index
14411441

14421442

1443-
### compare()
1444-
1445-
Compares the value against all map elements.
1446-
1447-
```php
1448-
public function compare( string $value, bool $case = true ) : bool
1449-
```
1450-
1451-
* @param **string** `$value` Value to compare map elements to
1452-
* @param **bool** `$case` TRUE if comparison is case sensitive, FALSE to ignore upper/lower case
1453-
* @return **bool** TRUE If at least one element matches, FALSE if value is not in map
1454-
1455-
All scalar values (bool, float, int and string) are casted to string values before
1456-
comparing to the given value. Non-scalar values in the map are ignored.
1457-
1458-
**Examples:**
1459-
1460-
```php
1461-
Map::from( ['foo', 'bar'] )->compare( 'foo' );
1462-
// true
1463-
1464-
Map::from( ['foo', 'bar'] )->compare( 'Foo', false );
1465-
// true (case insensitive)
1466-
1467-
Map::from( [123, 12.3] )->compare( '12.3' );
1468-
// true
1469-
1470-
Map::from( [false, true] )->compare( '1' );
1471-
// true
1472-
1473-
Map::from( ['foo', 'bar'] )->compare( 'Foo' );
1474-
// false (case sensitive)
1475-
1476-
Map::from( ['foo', 'bar'] )->compare( 'baz' );
1477-
// false
1478-
1479-
Map::from( [new \stdClass(), 'bar'] )->compare( 'foo' );
1480-
// false
1481-
```
1482-
1483-
**See also:**
1484-
1485-
* [contains()](#contains) - Tests if an item exists in the map
1486-
* [in()](#in) - Tests if element is included
1487-
* [includes()](#includes) - Tests if element is included
1488-
1489-
14901443
### concat()
14911444

14921445
Pushs all of the given elements onto the map without creating a new map.
@@ -5659,6 +5612,53 @@ Map::from( [0, 0.0, false, []] )->strBefore( '' );
56595612
* [strAfter()](#strafter) - Returns the strings after the passed value
56605613

56615614

5615+
### strCompare()
5616+
5617+
Compares the value against all map elements.
5618+
5619+
```php
5620+
public function strCompare( string $value, bool $case = true ) : bool
5621+
```
5622+
5623+
* @param **string** `$value` Value to compare map elements to
5624+
* @param **bool** `$case` TRUE if comparison is case sensitive, FALSE to ignore upper/lower case
5625+
* @return **bool** TRUE If at least one element matches, FALSE if value is not in map
5626+
5627+
All scalar values (bool, float, int and string) are casted to string values before
5628+
comparing to the given value. Non-scalar values in the map are ignored.
5629+
5630+
**Examples:**
5631+
5632+
```php
5633+
Map::from( ['foo', 'bar'] )->strCompare( 'foo' );
5634+
// true
5635+
5636+
Map::from( ['foo', 'bar'] )->strCompare( 'Foo', false );
5637+
// true (case insensitive)
5638+
5639+
Map::from( [123, 12.3] )->strCompare( '12.3' );
5640+
// true
5641+
5642+
Map::from( [false, true] )->strCompare( '1' );
5643+
// true
5644+
5645+
Map::from( ['foo', 'bar'] )->strCompare( 'Foo' );
5646+
// false (case sensitive)
5647+
5648+
Map::from( ['foo', 'bar'] )->strCompare( 'baz' );
5649+
// false
5650+
5651+
Map::from( [new \stdClass(), 'bar'] )->strCompare( 'foo' );
5652+
// false
5653+
```
5654+
5655+
**See also:**
5656+
5657+
* [contains()](#contains) - Tests if an item exists in the map
5658+
* [in()](#in) - Tests if element is included
5659+
* [includes()](#includes) - Tests if element is included
5660+
5661+
56625662
### strContains()
56635663

56645664
Tests if at least one of the passed strings is part of at least one entry.

src/Map.php

Lines changed: 40 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1012,37 +1012,16 @@ public function combine( iterable $values ) : self
10121012
/**
10131013
* Compares the value against all map elements.
10141014
*
1015-
* Examples:
1016-
* Map::from( ['foo', 'bar'] )->compare( 'foo' );
1017-
* Map::from( ['foo', 'bar'] )->compare( 'Foo', false );
1018-
* Map::from( [123, 12.3] )->compare( '12.3' );
1019-
* Map::from( [false, true] )->compare( '1' );
1020-
* Map::from( ['foo', 'bar'] )->compare( 'Foo' );
1021-
* Map::from( ['foo', 'bar'] )->compare( 'baz' );
1022-
* Map::from( [new \stdClass(), 'bar'] )->compare( 'foo' );
1023-
*
1024-
* Results:
1025-
* The first four examples return TRUE, the last three examples will return FALSE.
1026-
*
1027-
* All scalar values (bool, float, int and string) are casted to string values before
1028-
* comparing to the given value. Non-scalar values in the map are ignored.
1015+
* This method is an alias for strCompare().
10291016
*
10301017
* @param string $value Value to compare map elements to
10311018
* @param bool $case TRUE if comparison is case sensitive, FALSE to ignore upper/lower case
10321019
* @return bool TRUE If at least one element matches, FALSE if value is not in map
1020+
* @deprecated Use strCompare() method instead
10331021
*/
10341022
public function compare( string $value, bool $case = true ) : bool
10351023
{
1036-
$fcn = $case ? 'strcmp' : 'strcasecmp';
1037-
1038-
foreach( $this->list() as $item )
1039-
{
1040-
if( is_scalar( $item ) && !$fcn( (string) $item, $value ) ) {
1041-
return true;
1042-
}
1043-
}
1044-
1045-
return false;
1024+
return $this->strCompare( $value, $case );
10461025
}
10471026

10481027

@@ -4626,6 +4605,43 @@ public function strBefore( string $value, bool $case = false, string $encoding =
46264605
}
46274606

46284607

4608+
/**
4609+
* Compares the value against all map elements.
4610+
*
4611+
* Examples:
4612+
* Map::from( ['foo', 'bar'] )->compare( 'foo' );
4613+
* Map::from( ['foo', 'bar'] )->compare( 'Foo', false );
4614+
* Map::from( [123, 12.3] )->compare( '12.3' );
4615+
* Map::from( [false, true] )->compare( '1' );
4616+
* Map::from( ['foo', 'bar'] )->compare( 'Foo' );
4617+
* Map::from( ['foo', 'bar'] )->compare( 'baz' );
4618+
* Map::from( [new \stdClass(), 'bar'] )->compare( 'foo' );
4619+
*
4620+
* Results:
4621+
* The first four examples return TRUE, the last three examples will return FALSE.
4622+
*
4623+
* All scalar values (bool, float, int and string) are casted to string values before
4624+
* comparing to the given value. Non-scalar values in the map are ignored.
4625+
*
4626+
* @param string $value Value to compare map elements to
4627+
* @param bool $case TRUE if comparison is case sensitive, FALSE to ignore upper/lower case
4628+
* @return bool TRUE If at least one element matches, FALSE if value is not in map
4629+
*/
4630+
public function strCompare( string $value, bool $case = true ) : bool
4631+
{
4632+
$fcn = $case ? 'strcmp' : 'strcasecmp';
4633+
4634+
foreach( $this->list() as $item )
4635+
{
4636+
if( is_scalar( $item ) && !$fcn( (string) $item, $value ) ) {
4637+
return true;
4638+
}
4639+
}
4640+
4641+
return false;
4642+
}
4643+
4644+
46294645
/**
46304646
* Tests if at least one of the passed strings is part of at least one entry.
46314647
*

tests/MapTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3133,6 +3133,18 @@ public function testStrBefore()
31333133
}
31343134

31353135

3136+
public function testStrCompare()
3137+
{
3138+
$this->assertEquals( true, Map::from( ['foo', 'bar'] )->strCompare( 'foo' ) );
3139+
$this->assertEquals( true, Map::from( ['foo', 'bar'] )->strCompare( 'Foo', false ) );
3140+
$this->assertEquals( true, Map::from( [123, 12.3] )->strCompare( '12.3' ) );
3141+
$this->assertEquals( true, Map::from( [false, true] )->strCompare( '1' ) );
3142+
$this->assertEquals( false, Map::from( ['foo', 'bar'] )->strCompare( 'Foo' ) );
3143+
$this->assertEquals( false, Map::from( ['foo', 'bar'] )->strCompare( 'baz' ) );
3144+
$this->assertEquals( false, Map::from( [new \stdClass(), 'bar'] )->strCompare( 'foo' ) );
3145+
}
3146+
3147+
31363148
public function testStrContains()
31373149
{
31383150
$this->assertTrue( Map::from( ['abc'] )->strContains( '' ) );

0 commit comments

Comments
 (0)