Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
- Add `fromInteger()` and `tryFromInteger()` on the new `Contracts\Factory4Interface` (IPv4 and Multi).
- Arbitrary-precision `fromIntegerString()` and `tryFromIntegerString()` on all classes.
- Fix: `Multi::fromProtocol()` now rejects a binary string of the wrong length instead of passing it through.
- Fix: `Multi::fromEmbedded()` now keeps the strategy it is given instead of falling back to the default.

### Output

Expand Down
1 change: 1 addition & 0 deletions docs/04-helpers.md
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ an IPv6 address.

```php
<?php
use Darsyn\IP\Strategy\Derived;
use Darsyn\IP\Strategy\Mapped;
use Darsyn\IP\Version\IPv6;
use Darsyn\IP\Version\Multi;
Expand Down
10 changes: 10 additions & 0 deletions src/Version/Multi.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,16 @@ public static function isValid(string $ip, ?EmbeddingStrategyInterface $strategy
return null !== static::tryFromProtocol($ip, $strategy);
}

/**
* @throws \Darsyn\IP\Exception\InvalidIpAddressException
* @throws \Darsyn\IP\Exception\WrongVersionException
* @return static
*/
public static function fromEmbedded(string $ip, ?EmbeddingStrategyInterface $strategy = null)
{
return static::tryFromProtocol($ip, $strategy) ?? static::fromBinary($ip, $strategy);
}

protected function __construct(string $ip, ?EmbeddingStrategyInterface $strategy = null)
{
// Fallback to default in case this instance was created from static in
Expand Down
9 changes: 9 additions & 0 deletions tests/Version/MultiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -928,6 +928,15 @@ public function testFromBinaryUsesExplicitStrategy(): void
$this->assertSame('2002:0c22:384e:0000:0000:0000:0000:0000', $ip->toExpandedAddress());
}

/** @test */
#[PHPUnit\Test]
public function testFromEmbeddedUsesExplicitStrategy(): void
{
$ip = IP::fromEmbedded('127.0.0.1', new Strategy\Derived());
$this->assertSame('2002:7f00:1::', $ip->toCompactedAddress());
$this->assertSame('127.0.0.1', $ip->toDotAddress());
}

/**
* @test
* @dataProvider \Darsyn\IP\Tests\DataProvider\Multi::getValidBinarySequences()
Expand Down