private function getByPosition($operator, $pos) { $matches = $this->candidateList(); $found = new \SplObjectStorage(); if ($matches->count() == 0) { return; } switch ($operator) { case 'nth': case 'eq': if ($matches->count() >= $pos) { //$found[] = $matches[$pos -1]; foreach ($matches as $match) { // CSS is 1-based, so we pre-increment. if ($matches->key() + 1 == $pos) { $found->attach($match); break; } } } break; case 'first': if ($matches->count() > 0) { $matches->rewind(); // This is necessary to init. $found->attach($matches->current()); } break; case 'last': if ($matches->count() > 0) { // Spin through iterator. foreach ($matches as $item) { } $found->attach($item); } break; // case 'even': // for ($i = 1; $i <= count($matches); ++$i) { // if ($i % 2 == 0) { // $found[] = $matches[$i]; // } // } // break; // case 'odd': // for ($i = 1; $i <= count($matches); ++$i) { // if ($i % 2 == 0) { // $found[] = $matches[$i]; // } // } // break; // case 'even': // for ($i = 1; $i <= count($matches); ++$i) { // if ($i % 2 == 0) { // $found[] = $matches[$i]; // } // } // break; // case 'odd': // for ($i = 1; $i <= count($matches); ++$i) { // if ($i % 2 == 0) { // $found[] = $matches[$i]; // } // } // break; case 'lt': $i = 0; foreach ($matches as $item) { if (++$i < $pos) { $found->attach($item); } } break; case 'gt': $i = 0; foreach ($matches as $item) { if (++$i > $pos) { $found->attach($item); } } break; } $this->matches = $found; }