Skip to content

Commit

Permalink
Merge pull request #12 from transprime-research/feature/#6-add-other-…
Browse files Browse the repository at this point in the history
…methods

Feature/#6 allow proxied calls to unimplemented array_* methods
  • Loading branch information
omitobi committed May 4, 2020
2 parents 61f5044 + 4cd3758 commit b91b3c9
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 6 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ php:
before_script:
- composer self-update
- composer install --prefer-source --no-interaction --dev
- vendor/bin/phpcs

script: vendor/bin/phpunit
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,18 @@ arrayed(['a' => 'www', 'b' => 'dot', 'c' => 'www'])
> The pipe method makes use of [Piper](https://github.com/transprime-research/piper) - A PHP functional pipe'ing
> See `\Transprime\Arrayed\Tests\ArrayedTest`
#### Proxied calls

`array_*` methods that are not yet implemented are automatically proxied to call an array method with the assumption that they accept initial array first. Example is this:

```php
// ->combine() method is not yet implemented

arrayed(['a', 'b'])
->combine(['name', 'data'])
->result(); //['a' => 'name', 'b' => 'data']
```

## Coming Soon

- Implement other `array_*` methods
Expand Down
2 changes: 1 addition & 1 deletion src/Arrayed.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,6 @@ public function __toString(): string
*/
public function jsonSerialize()
{
return $this->getWorkableItem(true);
return $this->getWorkableItem(true);
}
}
29 changes: 28 additions & 1 deletion src/Traits/ArrayPrefix.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Transprime\Arrayed\Traits;

use Transprime\Arrayed\Exceptions\ArrayedException;
use Transprime\Arrayed\Interfaces\ArrayedInterface;

trait ArrayPrefix
Expand All @@ -20,4 +21,30 @@ public function column($column, $index_key = null): ArrayedInterface
{
return $this->setResult(array_column($this->getWorkableItem(), $column, $index_key));
}
}

/**
* Forward the calls to `array_*` that is not yet implemented
* <br>
* Assumption is for those array method that accepts the initial array as the first value
* @param $name
* @param $arguments
* @return mixed
* @throws ArrayedException
*/
public function __call($name, $arguments)
{
// See: https://stackoverflow.com/a/57833019/5704410
$methodName = strtolower(preg_replace("/([a-z])([A-Z])/", "$1_$2", $name));
$methodName = 'array_' . $methodName;
if (function_exists($methodName)) {
$result = $methodName($this->getWorkableItem(), ...$arguments);
return is_array($result)
? $this->setResult($result)
: $result;
}
throw new ArrayedException(sprintf('Method %s cannot be resolved', $name));
}
}
16 changes: 14 additions & 2 deletions tests/ArrayPrefixTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function testChunk()
{
$this->assertEquals(
[[1,2], [3,4]],
arrayed(1,2,3,4)->chunk(2)->result()
arrayed(1, 2, 3, 4)->chunk(2)->result()
);
}

Expand All @@ -34,4 +34,16 @@ public function testColumn()
arrayed($array)->column('b')->result()
);
}
}

public function testUnImplementedArrayPrefixFunction()
{
// array_combine
$keys = ['a', 'b'];
$values = ['name', 'data'];

$this->assertEquals(
array_combine($keys, $values),
arrayed($keys)->combine($values)->result()
);
}
}
4 changes: 2 additions & 2 deletions tests/ArrayedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -309,8 +309,8 @@ public function testExtraCallableOnResultMethod()
'name,age',
arrayed(['a' => 'name', 'b' => 'age'])
->values()(function ($val) {
return implode(',', $val);
})
return implode(',', $val);
})
);

$this->assertEquals(
Expand Down

0 comments on commit b91b3c9

Please sign in to comment.