-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add AssocValue flip and swap functions #60
base: master
Are you sure you want to change the base?
Changes from all commits
fbc3439
5552211
d56dc08
c69909a
b58a769
2f55b50
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
namespace GW\Value; | ||
|
||
use BadMethodCallException; | ||
use GW\Value\Associable\Flip; | ||
use IteratorAggregate; | ||
use ArrayAccess; | ||
|
||
|
@@ -143,6 +144,18 @@ public function only(...$keys): AssocValue; | |
*/ | ||
public function withoutElement($value): AssocValue; | ||
|
||
/** | ||
* @phpstan-return AssocValue<int|string, TKey> | ||
*/ | ||
public function flip(): AssocValue; | ||
|
||
/** | ||
* @param TKey $keyA | ||
* @param TKey $keyB | ||
* @phpstan-return AssocArray<TKey, TValue> | ||
*/ | ||
public function swap($keyA, $keyB): AssocArray; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It can have type-hint |
||
|
||
/** | ||
* @deprecated use join() or replace() instead | ||
* @phpstan-param AssocValue<TKey, TValue> $other | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace GW\Value\Associable; | ||
|
||
use GW\Value\Associable; | ||
use function array_flip; | ||
|
||
/** | ||
* @template TKey | ||
* @template TValue of int|string | ||
* @implements Associable<int|string,TValue> | ||
*/ | ||
final class Flip implements Associable | ||
{ | ||
/** @var Associable<TValue,TKey> */ | ||
private Associable $associable; | ||
|
||
/** | ||
* @param Associable<TValue,TKey> $associable | ||
*/ | ||
public function __construct(Associable $associable) | ||
{ | ||
$this->associable = $associable; | ||
} | ||
|
||
/** @return array<int|string,TValue> */ | ||
public function toAssocArray(): array | ||
{ | ||
return array_flip($this->associable->toAssocArray()); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace GW\Value\Associable; | ||
|
||
use GW\Value\Associable; | ||
use InvalidArgumentException; | ||
|
||
/** | ||
* @template TKey of int|string | ||
* @template TValue | ||
* @implements Associable<TKey,TValue> | ||
*/ | ||
final class Swap implements Associable | ||
{ | ||
/** @var Associable<TKey,TValue> */ | ||
private Associable $associable; | ||
/** @var TKey */ | ||
private $keyA; | ||
/** @var TKey */ | ||
private $keyB; | ||
|
||
/** | ||
* @param Associable<TKey,TValue> $associable | ||
* @param TKey $keyA | ||
* @param TKey $keyB | ||
*/ | ||
public function __construct(Associable $associable, $keyA, $keyB) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
{ | ||
$this->associable = $associable; | ||
$this->keyA = $keyA; | ||
$this->keyB = $keyB; | ||
} | ||
|
||
/** @return array<TKey,TValue> */ | ||
public function toAssocArray(): array | ||
{ | ||
$items = $this->associable->toAssocArray(); | ||
$valueA = $items[$this->keyA] ?? throw new InvalidArgumentException("Undefined key {$this->keyA}"); | ||
$valueB = $items[$this->keyB] ?? throw new InvalidArgumentException("Undefined key {$this->keyB}"); | ||
$items[$this->keyA] = $valueB; | ||
$items[$this->keyB] = $valueA; | ||
|
||
return $items; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -148,7 +148,7 @@ public function positionLast($needle): ?int; | |
|
||
/** | ||
* @param string|StringValue $pattern | ||
* @return ArrayValue<array<int|string, string>> | ||
* @return ArrayValue<array<string>> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It can be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think I has similar problem on my branch and resolved it like this 😏 |
||
*/ | ||
public function matchAllPatterns($pattern): ArrayValue; | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It can't be
AssocValue<TValue, TKey>
?I know it doesn't make sense if TValue isn't int|string, but
AssocValue<int, string>
flipped should be ratherAssocValue<string, int>
thanAssocValue<int|string, int>