-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add isSame, contains, isSameOrContains methods
- Loading branch information
Showing
12 changed files
with
165 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace MLocati\ComuniItaliani\Service; | ||
|
||
use MLocati\ComuniItaliani\Territory; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
trait TerritoryTrait | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
* | ||
* @see \MLocati\ComuniItaliani\Territory::isSame() | ||
*/ | ||
public function isSame(Territory $territory): bool | ||
{ | ||
return $this->getID() === $territory->getID(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace MLocati\ComuniItaliani\Service; | ||
|
||
use MLocati\ComuniItaliani\Territory; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
trait TerritoryWithChildrenTrait | ||
{ | ||
use TerritoryTrait; | ||
|
||
/** | ||
* {@inheritdoc} | ||
* | ||
* @see \MLocati\ComuniItaliani\TerritoryWithChildren::contains() | ||
*/ | ||
public function contains(Territory $territory): bool | ||
{ | ||
foreach ($this->getChildren() as $child) { | ||
if ($child instanceof TerritoryWithChildren) { | ||
if ($child->isSameOrContains($territory)) { | ||
return true; | ||
} | ||
} elseif ($child->isSame($territory)) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
* | ||
* @see \MLocati\ComuniItaliani\TerritoryWithChildren::isSameOrContains() | ||
*/ | ||
public function isSameOrContains(Territory $territory): bool | ||
{ | ||
return $this->isSame($territory) || $this->contains($territory); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace MLocati\ComuniItaliani\Test; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use MLocati\ComuniItaliani\Factory; | ||
use MLocati\ComuniItaliani\Territory; | ||
use MLocati\ComuniItaliani\TerritoryWithChildren; | ||
|
||
class HierarchyTest extends TestCase | ||
{ | ||
public function testHierarchy(): void | ||
{ | ||
$factory1 = new Factory(); | ||
$factory2 = new Factory(); | ||
|
||
$this->testHierarchyFor($factory1->getGeographicalSubdivisions()[0], $factory2->getGeographicalSubdivisions()[0]); | ||
} | ||
|
||
private function testHierarchyFor(Territory $territory, Territory $equalsNotSame): void | ||
{ | ||
$this->assertNotSame($territory, $equalsNotSame); | ||
$this->assertTrue($territory->isSame($territory)); | ||
$this->assertTrue($territory->isSame($equalsNotSame)); | ||
if ($territory instanceof TerritoryWithChildren) { | ||
$this->assertFalse($territory->contains($territory)); | ||
$this->assertFalse($territory->contains($equalsNotSame)); | ||
$this->assertTrue($territory->isSameOrContains($territory)); | ||
$this->assertTrue($territory->isSameOrContains($equalsNotSame)); | ||
$child = $territory->getChildren()[0]; | ||
$this->assertFalse($child->isSame($territory)); | ||
$this->assertFalse($territory->isSame($child)); | ||
if ($child instanceof TerritoryWithChildren) { | ||
$this->assertFalse($child->contains($territory)); | ||
$this->assertFalse($child->isSameOrContains($territory)); | ||
$this->testHierarchyFor($child, $equalsNotSame->getChildren()[0]); | ||
} | ||
} | ||
} | ||
} |