<commit_before><?php
/**
 * Fi.php
 *
 * @author <NAME> <<EMAIL>>
 * @license MIT
 * @link https://github.com/yuanqing/fi
 */

namespace yuanqing\Fi;

class Fi implements \Iterator
{
  const ASC = 1;
  const DESC = 2;

  private $mapCallbacks;
  private $sortCallbacks;
  private $fileParser;
  private $fileIterator;

  /**
   * @param string $dataDir The directory containing the data files
   * @param string $filePathFormat The file path format of the data files
   * @throws \InvalidArgumentException
   */
  public function __construct($dataDir, $filePathFormat)
  {
    # check $dataDir
    if (!is_dir($dataDir) || !is_readable($dataDir)) {
      throw new \InvalidArgumentException(sprintf('Invalid data directory: \'%s\'', $dataDir));
    }

    # normalise $args
    $dataDir = rtrim($dataDir, '/');
    $filePathFormat = $dataDir . '/' . ltrim($filePathFormat, '/');

    $this->mapCallbacks = array();
    $this->sortCallbacks = array();

    # dependencies
    $yamlParser = new YAMLParser;
    $filePathParser = new FilePathParser($filePathFormat);

    # get all file paths that match the given file path format
    $iterator = new FileFinder($dataDir, $filePathParser);

    # sort file paths in ascending order
    $this->fileParser = new FileParser($yamlParser, $filePathParser);
    $this->fileIterator = new FileIterator($iterator, $this->fileParser);
    $this->sort(function($document1, $document2) {
      return strnatcmp($document1->getFilePath(), $document2->getFilePath());
    });
  }

  /**
   * Returns all files in {$fileIterator} as an array of Document objects
   *
   * @return array
   */
  public function get()
  {
    return iterator_to_array($this, false);
  }

  /**
   * Adds a callback for filtering the {$fileIterator}
   *
   * @param callable $callback Takes a single argument of type Document. The callback must return * false to exclude the Document from the iterator
   * @throws \InvalidArgumentException
   */
  public function filter($callback)
  {
    if (!is_callable($callback)) {
      throw new \InvalidArgumentException('Filter callback must be callable');
    }
    $this->fileIterator->filter($callback);

    return $this;<commit_msg>Sort by file name in constructor<commit_after><?php
/**
 * Fi.php
 *
 * @author <NAME> <<EMAIL>>
 * @license MIT
 * @link https://github.com/yuanqing/fi
 */

namespace yuanqing\Fi;

class Fi implements \Iterator
{
  const ASC = 1;
  const DESC = 2;

  private $mapCallbacks;
  private $sortCallbacks;
  private $fileParser;
  private $fileIterator;

  /**
   * @param string $dataDir The directory containing the data files
   * @param string $filePathFormat The file path format of the data files
   * @throws \InvalidArgumentException
   */
  public function __construct($dataDir, $filePathFormat)
  {
    # check $dataDir
    if (!is_dir($dataDir) || !is_readable($dataDir)) {
      throw new \InvalidArgumentException(sprintf('Invalid data directory: \'%s\'', $dataDir));
    }

    # normalise $args
    $dataDir = rtrim($dataDir, '/');
    $filePathFormat = $dataDir . '/' . ltrim($filePathFormat, '/');

    $this->mapCallbacks = array();
    $this->sortCallbacks = array();

    # dependencies
    $yamlParser = new YAMLParser;
    $filePathParser = new FilePathParser($filePathFormat);

    # get all file paths that match the given file path format
    $iterator = new FileFinder($dataDir, $filePathParser);

    # sort file paths in ascending order
    $this->fileParser = new FileParser($yamlParser, $filePathParser);
    $this->fileIterator = new FileIterator($iterator, $this->fileParser);
    $this->sort(function($document1, $document2) {
      return strnatcasecmp($document1->getFilePath(), $document2->getFilePath());
    });
  }

  /**
   * Returns all files in {$fileIterator} as an array of Document objects
   *
   * @return array
   */
  public function get()
  {
    return iterator_to_array($this, false);
  }

  /**
   * Adds a callback for filtering the {$fileIterator}
   *
   * @param callable $callback Takes a single argument of type Document. The callback must return * false to exclude the Document from the iterator
   * @throws \InvalidArgumentException
   */
  public function filter($callback)
  {
    if (!is_callable($callback)) {
      throw new \InvalidArgumentException('Filter callback must be callable');
    }
    $this->fileIterator->filter($callback);

    return $this;
{'fraction_non_alphanumeric': 0.1264445955132563, 'fraction_numerical': 0.002719238613188307, 'mean_word_length': 3.56935817805383, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 29, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1109949', 'n_tokens_mistral': 1317, 'n_tokens_neox': 1279, 'n_words': 372}