CommerceGuys\Zone\Matcher\ZoneMatcher::matchAll PHP Method

matchAll() public method

public matchAll ( CommerceGuys\Addressing\AddressInterface $address, $scope = null )
$address CommerceGuys\Addressing\AddressInterface
    public function matchAll(AddressInterface $address, $scope = null)
    {
        // Find all matching zones.
        $results = [];
        foreach ($this->repository->getAll($scope) as $zone) {
            if ($zone->match($address)) {
                $results[] = ['priority' => (int) $zone->getPriority(), 'zone' => $zone];
            }
        }
        // Sort the matched zones by priority.
        usort($results, function ($a, $b) {
            if ($a['priority'] == $b['priority']) {
                return 0;
            }
            return $a['priority'] > $b['priority'] ? -1 : 1;
        });
        // Create the final zone array from the results.
        $zones = [];
        foreach ($results as $result) {
            $zones[] = $result['zone'];
        }
        return $zones;
    }

Usage Example

 /**
  * @covers ::matchAll
  *
  * @uses \CommerceGuys\Zone\Matcher\ZoneMatcher::__construct
  */
 public function testMatchAll()
 {
     $address = $this->getMockBuilder('CommerceGuys\\Addressing\\Model\\Address')->disableOriginalConstructor()->getMock();
     $zones = $this->matcher->matchAll($address);
     $this->assertCount(3, $zones);
     // de3 must come first because it has the highest priority.
     $this->assertEquals('de3', $zones[0]->getId());
     // The other two zones have the same priority, so their order is
     // undefined and different between PHP and HHVM.
     $otherIds = [];
     $otherIds[] = $zones[1]->getId();
     $otherIds[] = $zones[2]->getId();
     $this->assertContains('de2', $otherIds);
     $this->assertContains('de', $otherIds);
 }