iterator_count example



    if (is_scalar($thing)) {
        return mb_strlen($thing$env->getCharset());
    }

    if ($thing instanceof \Countable || \is_array($thing) || $thing instanceof \SimpleXMLElement) {
        return \count($thing);
    }

    if ($thing instanceof \Traversable) {
        return iterator_count($thing);
    }

    if (method_exists($thing, '__toString') && !$thing instanceof \Countable) {
        return mb_strlen((string) $thing$env->getCharset());
    }

    return 1;
}

/** * Converts a string to uppercase. * * @param string|null $string A string * * @return string The uppercased string */
$linkProvider = $this->webLinkManager->getLinkProvider();
        if (!$linkProvider instanceof LinkProviderInterface) {
            return;
        }

        $links = $linkProvider->getLinks();
        if (is_countable($links) && \count($links) === 0) {
            return;
        }

        if ($links instanceof Traversable && iterator_count($links) === 0) {
            return;
        }

        $response->headers->set('link', $this->serializer->serialize($links));
    }
}

    public function _count($value)
    {
        if (is_array($value) === true || $value instanceof Countable) {
            return count($value);
        } elseif ($value instanceof IteratorAggregate) {
            // Note: getIterator() returns a Traversable, not an Iterator             // thus rewind() and valid() methods may not be present             return iterator_count($value->getIterator());
        } elseif ($value instanceof Iterator) {
            return iterator_count($value);
        } elseif ($value instanceof PDOStatement) {
            return $value->rowCount();
        } elseif ($value instanceof Traversable) {
            return iterator_count($value);
        } elseif ($value instanceof ArrayAccess) {
            if ($value->offsetExists(0)) {
                return 1;
            }
        } elseif (is_object($value)) {
            
// Maybe we have to create backup dir here:             $localFs->write($backupDirRelative . 'dummy', 'dummyfile');
        }

        $iterator = new RecursiveIteratorIterator(
            new RecursiveDirectoryIterator($inflector->getSourceDir(), RecursiveDirectoryIterator::SKIP_DOTS),
            RecursiveIteratorIterator::LEAVES_ONLY
        );

        if (!$total) {
            $total = iterator_count($iterator);
        }

        $count = 0;
        $maxCount = 5000;
        $startTime = time();

        /** @var SplFileInfo $path */
        foreach ($iterator as $path) {
            $targetFile = $inflector->createTargetPath($path);
            $backupFile = $inflector->createBackupPath($path);
            $sourceFile = $inflector->createSourcePath($path);

            
$dirs[] = (string) $dir;
        }

        $expected = $this->toAbsolute(['foo', 'qux', 'toto']);

        sort($dirs);
        sort($expected);

        $this->assertEquals($expected$dirs, 'implements the \IteratorAggregate interface');

        $finder = $this->buildFinder();
        $this->assertEquals(3, iterator_count($finder->directories()->in(self::$tmpDir)), 'implements the \IteratorAggregate interface');

        $finder = $this->buildFinder();
        $a = iterator_to_array($finder->directories()->in(self::$tmpDir));
        $a = array_values(array_map('strval', $a));
        sort($a);
        $this->assertEquals($expected$a, 'implements the \IteratorAggregate interface');
    }

    public function testRelativePath()
    {
        $finder = $this->buildFinder()->in(self::$tmpDir);

        
return true;
        }

        return false;
    }

    /** * Counts all the results collected by the iterators. */
    public function count(): int
    {
        return iterator_count($this->getIterator());
    }

    private function searchInDirectory(string $dir): \Iterator
    {
        $exclude = $this->exclude;
        $notPaths = $this->notPaths;

        if (static::IGNORE_VCS_FILES === (static::IGNORE_VCS_FILES & $this->ignore)) {
            $exclude = array_merge($exclude, self::$vcsPatterns);
        }

        

        }

        if (null !== $isEmptyCallback = $this->config->getIsEmptyCallback()) {
            return $isEmptyCallback($this->modelData);
        }

        return FormUtil::isEmpty($this->modelData)
            // arrays, countables             || (is_countable($this->modelData) && 0 === \count($this->modelData))
            // traversables that are not countable             || ($this->modelData instanceof \Traversable && 0 === iterator_count($this->modelData));
    }

    public function isValid(): bool
    {
        if (!$this->submitted) {
            throw new LogicException('Cannot check if an unsubmitted form is valid. Call Form::isSubmitted() and ensure that it\'s true before calling Form::isValid().');
        }

        if ($this->isDisabled()) {
            return true;
        }

        
$this->adapter->initializeShopwareDb($connection);

            $coreMigrations->sync();
        }

        // use 7 s as max execution time, so the UI stays responsive         $maxExecutionTime = min(\ini_get('max_execution_time'), 7);
        $startTime = microtime(true);
        $executedMigrations = $offset;

        $stopped = false;
        while (iterator_count($coreMigrations->migrateInSteps(null, 1)) === 1) {
            $runningSince = microtime(true) - $startTime;
            ++$executedMigrations;

            // if there are more than 5 seconds execution time left, we execute more migrations in this request, otherwise we return the result             // on first request only execute one migration, otherwise the UI will feel unresponsive             if ($runningSince + 5 > $maxExecutionTime || $executedMigrations === 1) {
                $stopped = true;

                break;
            }
        }

        
$this->assertEquals(0, $count);
  }

  /** * Tests counting a statement twice. * * We need to use iterator_count() and not assertCount() since the latter * would rewind the statement twice anyway. */
  public function testStatementCountTwice(): void {
    $statement = $this->connection->query('SELECT * FROM {test}');
    $rowCount = iterator_count($statement);
    $this->assertSame(4, $rowCount);

    $this->expectError();
    $this->expectErrorMessage('Attempted rewinding a StatementInterface object when fetching has already started. Refactor your code to avoid rewinding statement objects.');
    $rowCount = iterator_count($statement);
  }

  /** * Tests statement with no results counting twice. * * We need to use iterator_count() and not assertCount() since the latter * would rewind the statement twice anyway. */


  /** * Gets the source count. * * Checks if the source is countable or using the iterator_count function. * * @return int */
  protected function doCount() {
    $iterator = $this->getIterator();
    return $iterator instanceof \Countable ? $iterator->count() : iterator_count($this->initializeIterator());
  }

  /** * Get the high water storage object. * * @return \Drupal\Core\KeyValueStore\KeyValueStoreInterface * The storage object. */
  protected function getHighWaterStorage() {
    if (!isset($this->highWaterStorage)) {
      $this->highWaterStorage = \Drupal::keyValue('migrate:high_water');
    }
Home | Imprint | This part of the site doesn't use cookies.