Skip to content

Commit

Permalink
functions
Browse files Browse the repository at this point in the history
  • Loading branch information
maxonfjvipon committed May 9, 2023
1 parent 08943b2 commit c1b94bb
Show file tree
Hide file tree
Showing 28 changed files with 376 additions and 511 deletions.
8 changes: 7 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,13 @@
"psr-4": {
"Maxonfjvipon\\ElegantElephant\\": "src",
"Maxonfjvipon\\ElegantElephant\\Support\\": "support"
}
},
"files": [
"src/Any/functions.php",
"src/Arr/functions.php",
"src/Logic/functions.php",
"src/Txt/functions.php"
]
},
"autoload-dev": {
"psr-4": {
Expand Down
522 changes: 243 additions & 279 deletions composer.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
</testsuites>
<logging>
<log type="coverage-clover" target="build/logs/clover.xml"/>
<log type="coverage-html" target="build/logs/html"/>
</logging>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
Expand Down
6 changes: 3 additions & 3 deletions src/Any/AnyFork.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
use Maxonfjvipon\ElegantElephant\Logic;
use Maxonfjvipon\ElegantElephant\Logic\EnsureLogic;

use function Maxonfjvipon\ElegantElephant\Any\any_fork;

/**
* Conditional Any.
*/
Expand All @@ -53,8 +55,6 @@ final public function __construct(

public function value(): mixed
{
return $this->ensuredBool($this->condition)
? $this->ensuredAnyValue($this->first)
: $this->ensuredAnyValue($this->second);
return any_fork($this->condition, $this->first, $this->second);
}
}
4 changes: 3 additions & 1 deletion src/Any/AtKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
use Maxonfjvipon\ElegantElephant\Num;
use Maxonfjvipon\ElegantElephant\Txt;

use function Maxonfjvipon\ElegantElephant\Any\at_key;

/**
* At key.
*/
Expand All @@ -54,6 +56,6 @@ final public function __construct(

public function value(): mixed
{
return $this->ensuredArray($this->arr)[$this->ensuredAnyValue($this->key)];
return at_key($this->key, $this->arr);
}
}
15 changes: 3 additions & 12 deletions src/Any/AtValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,14 @@

namespace Maxonfjvipon\ElegantElephant\Any;

use Exception;
use Maxonfjvipon\ElegantElephant\Any;
use Maxonfjvipon\ElegantElephant\Arr;
use Maxonfjvipon\ElegantElephant\Arr\EnsureArr;
use Maxonfjvipon\ElegantElephant\Num;
use Maxonfjvipon\ElegantElephant\Txt;

use function Maxonfjvipon\ElegantElephant\Any\at_value;

/**
* Key from array by element.
*/
Expand All @@ -54,16 +55,6 @@ final public function __construct(

final public function value(): string|int
{
$key = array_search(
needle: $this->ensuredAnyValue($this->value),
haystack: $this->ensuredArray($this->arr),
strict: true
);

if ($key === false) {
throw new Exception("Key by given value not found!");
}

return $key;
return at_value($this->value, $this->arr);
}
}
33 changes: 3 additions & 30 deletions src/Any/EnsureAny.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,8 @@
namespace Maxonfjvipon\ElegantElephant\Any;

use Exception;
use Maxonfjvipon\ElegantElephant\Any;
use Maxonfjvipon\ElegantElephant\Arr;
use Maxonfjvipon\ElegantElephant\Logic;
use Maxonfjvipon\ElegantElephant\Num;
use Maxonfjvipon\ElegantElephant\Txt;

use function Maxonfjvipon\ElegantElephant\Any\ensured_any;

/**
* Ensured Any.
Expand All @@ -45,30 +42,6 @@ trait EnsureAny
*/
private function ensuredAnyValue(mixed $any): mixed
{
$getAny = function (mixed $any): Any {
if ($any instanceof Any) {
return $any;
}

if ($any instanceof Arr) {
return AnyOf::arr($any);
}

if ($any instanceof Txt) {
return AnyOf::text($any);
}

if ($any instanceof Num) {
return AnyOf::num($any);
}

if ($any instanceof Logic) {
return AnyOf::logic($any);
}

return AnyOf::just($any);
};

return $getAny($any)->value();
return ensured_any($any);
}
}
15 changes: 3 additions & 12 deletions src/Any/FirstOf.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@

namespace Maxonfjvipon\ElegantElephant\Any;

use Exception;
use Maxonfjvipon\ElegantElephant\Any;
use Maxonfjvipon\ElegantElephant\Arr;
use Maxonfjvipon\ElegantElephant\Txt;

use function Maxonfjvipon\ElegantElephant\Any\first_of;

/**
* First item.
*/
Expand Down Expand Up @@ -68,16 +69,6 @@ final public function __construct(private string|Any|Arr|array|Txt $container)

public function value(): mixed
{
$value = $this->ensuredAnyValue($this->container);

if (!is_string($value) && !is_array($value)) {
throw new Exception("The element to get the first element from must be an array or string");
}

if (empty($value)) {
throw new Exception("Can't get the first element of an empty value");
}

return $value[0];
return first_of($this->container);
}
}
24 changes: 3 additions & 21 deletions src/Any/LastOf.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@

namespace Maxonfjvipon\ElegantElephant\Any;

use Couchbase\IndexFailureException;
use Exception;
use Maxonfjvipon\ElegantElephant\Any;
use Maxonfjvipon\ElegantElephant\Arr;
use Maxonfjvipon\ElegantElephant\Txt;

use function Maxonfjvipon\ElegantElephant\Any\last_of;

/**
* Last of.
*/
Expand Down Expand Up @@ -67,24 +67,6 @@ final public function __construct(private string|array|Txt|Arr|Any $container)

public function value(): mixed
{
$value = $this->ensuredAnyValue($this->container);

if (is_string($value)) {
if (empty($value)) {
throw new Exception(message: "Can't get the last element of an empty string");
}

$res = substr($value, -1);
} elseif (is_array($value)) {
if (empty($value)) {
throw new Exception(message: "Can't get the last element of an empty array");
}

$res = $value[count($value) - 1];
} else {
throw new Exception(message: "The element to get the last element from must be an array or string");
}

return $res;
return last_of($this->container);
}
}
33 changes: 1 addition & 32 deletions src/Arr/ArrCombined.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@
*/
final class ArrCombined implements IterableArr
{
use EnsureArr;
use EnsureAny;
use HasArrIterator;

/**
Expand All @@ -57,35 +55,6 @@ final public function __construct(

final public function asArray(): array
{
/**
* @var callable $callback
* @param string|int|float|Txt|Num|Any $item
* @return mixed
* @throws Exception
*/
$callback = fn (string|int|float|Txt|Num|Any $item) => $this->ensuredAnyValue($item);

/** @var array<string|int> $keys */
$keys = array_map(
$callback,
$this->ensuredArray($this->keys)
);

/** @var array<mixed> $values */
$values = $this->ensuredArray($this->values);

if ($this->ensure) {
/** @var array<mixed> $values */
$values = array_map(
fn (mixed $item) => $this->ensuredAnyValue($item),
$this->ensuredArray($this->values)
);
}

if (count($keys) !== count($values)) {
throw new Exception("Keys and values arrays must have the same length");
}

return array_combine($keys, $values);
return array_combined($this->keys, $this->values, $this->ensure);
}
}
1 change: 1 addition & 0 deletions src/Arr/ArrCond.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ final class ArrCond extends ArrWrap
/**
* Ctor.
*
* @param bool|Logic $condition
* @param Arr|array<mixed> $first
* @param array<mixed>|Arr $second
*/
Expand Down
9 changes: 4 additions & 5 deletions src/Arr/ArrExploded.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,13 @@
use Maxonfjvipon\ElegantElephant\Txt;
use Maxonfjvipon\ElegantElephant\Txt\EnsureTxt;

use function Maxonfjvipon\ElegantElephant\Arr\array_exploded;

/**
* Array exploded.
*/
final class ArrExploded implements IterableArr
{
use EnsureTxt;
use HasArrIterator;

/**
Expand All @@ -49,16 +50,14 @@ final public static function byComma(string|Txt $text): ArrExploded
* Ctor.
*
* @param non-empty-string|Txt $separator
* @param string|Txt $text
*/
final public function __construct(private string|Txt $separator, private string|Txt $text)
{
}

final public function asArray(): array
{
/** @var non-empty-string $separator */
$separator = $this->ensuredString($this->separator);

return explode($separator, $this->ensuredString($this->text));
return array_exploded($this->separator, $this->text);
}
}
7 changes: 1 addition & 6 deletions src/Arr/ArrFiltered.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
*/
final class ArrFiltered implements IterableArr
{
use EnsureArr;
use HasArrIterator;

/**
Expand All @@ -53,10 +52,6 @@ final public function __construct(private array|Arr $arr, callable $callback)

final public function asArray(): array
{
return array_filter(
$this->ensuredArray($this->arr),
$this->callback,
ARRAY_FILTER_USE_BOTH
);
return array_filtered($this->arr, $this->callback);
}
}
22 changes: 1 addition & 21 deletions src/Arr/ArrFlatten.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
*/
final class ArrFlatten implements IterableArr
{
use EnsureArr;
use HasArrIterator;

/**
Expand All @@ -48,25 +47,6 @@ final public function __construct(private array|Arr $arr, private int $deep = 1)

final public function asArray(): array
{
return $this->flat($this->ensuredArray($this->arr), []);
}

/**
* @param array<mixed> $arr
* @param array<mixed> $new
* @return array<mixed>
* @throws Exception
*/
private function flat(array $arr, array $new, int $currentDeep = 0): array
{
foreach ($arr as $item) {
if ($this->deep !== $currentDeep && (is_array($item) || $item instanceof Arr)) {
$new = $this->flat($this->ensuredArray($item), $new, $currentDeep + 1);
} else {
$new[] = $item;
}
}

return $new;
return array_flatten($this->arr, $this->deep);
}
}
2 changes: 1 addition & 1 deletion src/Arr/ArrIf.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ final class ArrIf extends ArrWrap
final public function __construct(bool|Logic $condition, array|Arr $arr)
{
parent::__construct(
new ArrCond(
new ArrFork(
$condition,
$arr,
new ArrEmpty()
Expand Down
Loading

0 comments on commit c1b94bb

Please sign in to comment.