示例#1
0
 private function matchValue(array $value)
 {
     $isCalled = false;
     foreach ($this->patterns as $key => $pattern) {
         list($object, $method) = $this->flow[$key];
         if (empty($pattern)) {
             $value = $object->{$method}($value);
             $isCalled = true;
             break;
         } else {
             try {
                 $this->mapper->match($pattern, [$value]);
             } catch (\Exception $e) {
                 $isCalled = false;
                 continue;
             }
             $mappedValue = $this->mapper->current();
             if (!is_null($mappedValue)) {
                 $isCalled = true;
                 $value = $object->{$method}($value);
                 break;
             }
         }
     }
     if (!$isCalled) {
         throw new NoMatch();
     }
     return $value;
 }
示例#2
0
 public function testMatch()
 {
     $pattern = ['id' => 0, 'value' => ['title' => 'foo', 'tags' => []]];
     $data = [['id' => 11, 'value' => ['title' => 'foo', 'tags' => [1, 2, 3]]], ['id' => 12, 'value' => ['title' => 'bar', 'tags' => [1]]], ['id' => 13, 'value' => ['title' => 'bar', 'tags' => [1, 2, 5]]], ['id' => 14, 'value' => ['title' => 'foo', 'tags' => [1, 3]]]];
     $matching = new Pattern();
     $matching->match($pattern, $data);
     $this->assertEquals(2, iterator_count($matching));
     $matching->rewind();
     $id11 = $matching->current();
     $matching->next();
     $id14 = $matching->current();
     $this->assertEquals(['id' => 11, 'value' => ['title' => 'foo', 'tags' => [1, 2, 3]]], $id11);
     $this->assertEquals(['id' => 14, 'value' => ['title' => 'foo', 'tags' => [1, 3]]], $id14);
 }