Skip to content

Commit

Permalink
Added isString() method
Browse files Browse the repository at this point in the history
  • Loading branch information
aimeos committed Jul 31, 2024
1 parent e4c2e81 commit 74849f0
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 12 deletions.
44 changes: 44 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ will return:
<a href="#isnumeric">isNumeric</a>
<a href="#isobject">isObject</a>
<a href="#isscalar">isScalar</a>
<a href="#isstring">isString</a>
<a href="#join">join</a>
<a href="#jsonserialize">jsonSerialize</a>
<a href="#keys">keys</a>
Expand Down Expand Up @@ -413,6 +414,7 @@ will return:
* [isNumeric()](#isnumeric) : Tests if all entries are numeric values
* [isObject()](#isobject) : Tests if all entries are objects
* [isScalar()](#isscalar) : Tests if all entries are scalar values.
* [isString()](#isstring) : Tests if all entries are string values.
* [implements()](#implements) : Tests if all entries are objects implementing the interface
* [none()](#none) : Tests if none of the elements are part of the map
* [some()](#some) : Tests if at least one element is included
Expand Down Expand Up @@ -3103,6 +3105,48 @@ Map::from( [[1]] )->isScalar();
```


### isString()

Determines if all entries are string values.

```php
public function isString() : bool
```

* @return **bool** TRUE if all map entries are string values, FALSE if not

**Examples:**

```php
Map::from( ['abc'] )->isString();
// true

Map::from( [] )->isString();
// true

Map::from( [1] )->isString();
// false

Map::from( [1.1] )->isString();
// false

Map::from( [true, false] )->isString();
// false

Map::from( [new stdClass] )->isString();
// false

Map::from( [resource] )->isString();
// false

Map::from( [null] )->isString();
// false

Map::from( [[1]] )->isString();
// false
```


### join()

Concatenates the string representation of all elements.
Expand Down
50 changes: 38 additions & 12 deletions src/Map.php
Original file line number Diff line number Diff line change
Expand Up @@ -2535,16 +2535,14 @@ public function isEmpty() : bool
*/
public function isNumeric() : bool
{
$result = true;

foreach( $this->list() as $val )
{
if( !is_numeric( $val ) ) {
$result = false;
return false;
}
}

return $result;
return true;
}


Expand All @@ -2563,16 +2561,14 @@ public function isNumeric() : bool
*/
public function isObject() : bool
{
$result = true;

foreach( $this->list() as $val )
{
if( !is_object( $val ) ) {
$result = false;
return false;
}
}

return $result;
return true;
}


Expand All @@ -2597,16 +2593,46 @@ public function isObject() : bool
*/
public function isScalar() : bool
{
$result = true;

foreach( $this->list() as $val )
{
if( !is_scalar( $val ) ) {
$result = false;
return false;
}
}

return $result;
return true;
}


/**
* Determines if all entries are string values.
*
* Examples:
* Map::from( ['abc'] )->isString();
* Map::from( [] )->isString();
* Map::from( [1] )->isString();
* Map::from( [1.1] )->isString();
* Map::from( [true, false] )->isString();
* Map::from( [new stdClass] )->isString();
* Map::from( [#resource] )->isString();
* Map::from( [null] )->isString();
* Map::from( [[1]] )->isString();
*
* Results:
* The first two examples return TRUE while the others return FALSE
*
* @return bool TRUE if all map entries are string values, FALSE if not
*/
public function isString() : bool
{
foreach( $this->list() as $val )
{
if( !is_string( $val ) ) {
return false;
}
}

return true;
}


Expand Down
14 changes: 14 additions & 0 deletions tests/MapTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1846,6 +1846,20 @@ public function testIsScalar()
}


public function testIsString()
{
$this->assertTrue( Map::from( [] )->isString() );
$this->assertTrue( Map::from( ['abc'] )->isString() );

$this->assertFalse( Map::from( [1] )->isString() );
$this->assertFalse( Map::from( [1.1] )->isString() );
$this->assertFalse( Map::from( [true, false] )->isString() );
$this->assertFalse( Map::from( [new \stdClass] )->isString() );
$this->assertFalse( Map::from( [null] )->isString() );
$this->assertFalse( Map::from( [[1]] )->isString() );
}


public function testJoin()
{
$m = new Map( ['a', 'b', null, false] );
Expand Down

0 comments on commit 74849f0

Please sign in to comment.