Skip to content

Commit

Permalink
Add ArrayTools + remove value function + update PhpSpec
Browse files Browse the repository at this point in the history
  • Loading branch information
Awkan committed Jul 12, 2018
1 parent 7a7bd70 commit ce9b55d
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 3 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Added
- New `ArrayTools` class helper
- `ArrayTools::removeValue` method to remove a value from given array

## [2.1.0] - 2017-04-23
### Added
Expand Down
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ This library provide some tools to help you with your developments.

Here is the list of tools it provides:
- [StringTools](#stringtools-class)
- [ArrayTools](#arraytools-class)
- [EqualableInterface](#equalableinterface)
- [DateTimeComparator](#datetimecomparator-class)

Expand Down Expand Up @@ -91,6 +92,17 @@ StringTools::mb_ucfirst($str, $encoding) : string
* `$str` string input
* `$encoding` (optional, default "UTF-8") encoding of your input string

### ArrayTools class

#### ::removeValue

```php
ArrayTools::removeValue($array, $value) : void
```

* `$array` input array passed by reference
* `$value` The value to remove from the $array

### EqualableInterface

Helps you equals on objects on a similar way as [java](http://stackoverflow.com/questions/1643067/whats-the-difference-between-equals-and).
Expand Down Expand Up @@ -125,4 +137,4 @@ DateTimeComparator::lowest($dateTime1, $dateTime2) : ?\DateTimeInterface
```

* `$dateTime1` The first \DateTimeInterface or null
* `$dateTime2` The second \DateTimeInterface or null
* `$dateTime2` The second \DateTimeInterface or null
11 changes: 9 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,14 @@
"autoload": {
"psr-4": { "Nekland\\Tools\\": "src/" }
},
"require": {
"php": "^5.6 || ^7.0"
},
"require-dev": {
"phpspec/phpspec": "^2.5"
}
"phpspec/phpspec": "^3.2",
"bossa/phpspec2-expect": "^2.3"
},
"_comment": [
"PHPSpec is limited to ^3.2 to keep compatibility with PHP5.6"
]
}
27 changes: 27 additions & 0 deletions spec/Nekland/Tools/ArrayToolsSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace spec\Nekland\Tools;

use Nekland\Tools\ArrayTools;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;

class ArrayToolsSpec extends ObjectBehavior
{
function it_is_initializable()
{
$this->shouldHaveType('Nekland\Tools\ArrayTools');
}

function it_should_remove_value()
{
$array = [
'foo',
'bar',
];

ArrayTools::removeValue($array, $array[1]);

\expect($array)->toBe([$array[0]]);
}
}
20 changes: 20 additions & 0 deletions src/ArrayTools.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Nekland\Tools;

class ArrayTools
{
/**
* Remove a value from the list
*
* @param array $array The array in which we have to remove value
* @param mixed $value The value to remove from the list
*/
public static function removeValue(array &$array, $value)
{
$index = \array_search($value, $array);
if (false !== $index) {
unset($array[$index]);
}
}
}

0 comments on commit ce9b55d

Please sign in to comment.