Skip to content

Commit

Permalink
1.35
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgecc-business-account committed Sep 20, 2024
1 parent 409720e commit 3ebf1d8
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 3 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -663,10 +663,14 @@ $this->makeRequestArrayByExample(['a'=1,'b'=>2]); // ['a'='post','b'=>'post'];


## versions
* 2.5. 2024-09-20
* [new] keepCol()
* [new] coolRename()
* [new] shuffle()
* 2.4 2024-08-15
* [update] sort() now allow to group by multiples columns
* [fixed] group() fixed the value returned. If multiples columns are used then, the new grouping column could be split.
* [new] splitColumn() split a column in two or more columns.
* [new] splitColumn() split a column in two or more columns.~~
* 2.3 2024-08-10
* [new] sum(),min(),max(),avg(),count(),aggr()
* 2.2 2024-08-06
Expand Down
70 changes: 68 additions & 2 deletions src/ArrayOne.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
*/
class ArrayOne implements ArrayAccess
{
public const VERSION = "2.3";
public const VERSION = "2.5";
/** @var mixed */
protected $array;
protected ?object $serviceObject;
Expand Down Expand Up @@ -277,8 +277,9 @@ public function modCol($colName = null, ?callable $operation = null): ArrayOne
return $this;
}


/**
* It removes a column<br/>
* It removes a column or columns<br/>
* **Example:**
* ```
* $this->removeCol('col1');
Expand All @@ -303,6 +304,32 @@ public function removeCol($colName): ArrayOne
}
return $this;
}
/**
* It keeps a column or columns and removes the rest of columns<br/>
* **Example:**
* ```
* $this->keepCol('col1');
* $this->keepCol(['col1','col2']);
* ```
* @param mixed $colName The name of the column or columns (array)
* @return $this
* @noinspection TypeUnsafeArraySearchInspection
*/
public function keepCol($colName): ArrayOne
{
if ($this->array === null) {
return $this;
}
$cols=is_array($colName)?$colName:[$colName];
foreach ($this->array as $index => $row) {
foreach ($row as $col=>$value) {
if(!in_array($col,$cols)){
unset($this->array[$index][$col]);
}
}
}
return $this;
}

/**
* It transforms a row of values into a single value. If a row does not contains a column then it returns null
Expand Down Expand Up @@ -646,6 +673,37 @@ public function map(?callable $condition): ArrayOne
return $this;
}

/**
* It renames a column (or multiples columns) with a different name.<br>
* **Example:**
* ```
* $this->colRename('c1','n1'); // [['c1'=1,'c2'=>2]] => [['n1'=>1,'c2'=2]]
* $this->colRename(['c1','c2'],['n1','n2']); // [['c1'=1,'c2'=>2]] => [['n1'=>1,'n2'=2]]
* ```
* @param string|array $colName the name of the column or columns
* @param string|array $newColName the name of the column or columns
* @return $this
*/
public function colRename($colName,$newColName): ArrayOne
{
if ($this->array === null) {
return $this;
}
$colName=is_array($colName)?$colName:[$colName];
$newColName=is_array($newColName)?$newColName:[$newColName];
if(count($colName)!==count($newColName)){
throw new RuntimeException('colRename must have the same number of columns');
}
foreach($this->array as &$val){
foreach($colName as $k=>$v) {
$val[$newColName[$k]] = $val[$v] ?? null;
unset($val[$v]);
}
}

return $this;
}

/**
* It flats the results. If the result is an array with a single row, then it returns the row without the array<br/>
* **Example:**
Expand Down Expand Up @@ -1314,6 +1372,14 @@ public function sort($column = null, $direction = 'asc'): ArrayOne
}
return $this;
}
public function shuffle(): ArrayOne
{
if ($this->array === null) {
return $this; // nothing to sort.
}
shuffle($this->array);
return $this;
}

/**
* This function removes duplicates of a table.<br/>
Expand Down
36 changes: 36 additions & 0 deletions test/ArrayOneTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,42 @@ public function testAgg(): void
$this->assertEquals(1800,ArrayOne::set($products)->aggr('sum','price'));

}
public function testColRename():void {
$arr= [
['name' => 'cocacola1', 'price' => 500, 'quantity' => 200, 'type' => 'type1'],
['name' => 'cocacola2', 'price' => 600, 'quantity' => 300, 'type' => 'type2'],
['name' => 'cocacola3', 'price' => 700, 'quantity' => 400, 'type' => 'type1']
];
$arr2= [
['name2' => 'cocacola1', 'price2' => 500, 'quantity' => 200, 'type' => 'type1'],
['name2' => 'cocacola2', 'price2' => 600, 'quantity' => 300, 'type' => 'type2'],
['name2' => 'cocacola3', 'price2' => 700, 'quantity' => 400, 'type' => 'type1']
];
$this->assertEquals($arr2,ArrayOne::set($arr)->colRename(['name','price'],['name2','price2'])->all());

}
public function testkeepCol():void {
$arr= [
['name' => 'cocacola1', 'price' => 500, 'quantity' => 200, 'type' => 'type1'],
['name' => 'cocacola2', 'price' => 600, 'quantity' => 300, 'type' => 'type2'],
['name' => 'cocacola3', 'price' => 700, 'quantity' => 400, 'type' => 'type1']
];
$arr2= [
['quantity' => 200, 'type' => 'type1'],
[ 'quantity' => 300, 'type' => 'type2'],
[ 'quantity' => 400, 'type' => 'type1']
];
$this->assertEquals($arr2,ArrayOne::set($arr)->keepCol(['quantity','type'])->all());

}
public function testShuffle():void {
$arr= [
['name' => 'cocacola1', 'price' => 500, 'quantity' => 200, 'type' => 'type1'],
['name' => 'cocacola2', 'price' => 600, 'quantity' => 300, 'type' => 'type2'],
['name' => 'cocacola3', 'price' => 700, 'quantity' => 400, 'type' => 'type1']
];
$this->assertIsArray(ArrayOne::set($arr)->shuffle()->all());
}

public function testSort(): void
{
Expand Down

0 comments on commit 3ebf1d8

Please sign in to comment.