<commit_before><?php
namespace I18nMessages\I18n;

use Aura\Intl\Package;
use Cake\ORM\TableRegistry;
use Cake\Utility\Inflector;

/**
 * DbMessages loader.
 *
 * Returns translation messages stored in database.
 */
class DbMessagesLoader {

/**
 * The domain name
 *
 * @var string
 */
	protected $_domain;

/**
 * The locale to load messages for.
 *
 * @var string
 */
	protected $_locale;

/**
 * The model name to use for loading messages or model instance.
 *
 * @var mixed
 */
	protected $_model;

/**
 * Formatting used for messsages.
 * @var string
 */
	protected $_formatter;

/**
 * Constructor
 *
 * @param string $domain Domain name.
 * @param string $locale Locale string.
 * @param mixed $model Model name or instance. Defaults to 'I18nMessages.I18nMessages'.
 * @param string $formatter Formatter name. Defaults to 'default' (ICU formatter).
 */
	public function __construct(
		$domain,
		$locale,
		$model = null,
		$formatter = 'default'
	) {
		if (!$model) {
			$model = 'I18nMessages.I18nMessages';
		}
		$this->_domain = $domain;
		$this->_locale = $locale;
		$this->_model = $model;
		$this->_formatter = $formatter;
	}

/**
 * Fetches the translation messages from db and returns package with those
 * messages.
 *
 * @return \Aura\Intl\Package
 * @throws \RuntimeException If model could not be loaded.
 */
	public function __invoke() {
		$model = $this->_model;
		if (is_string($model)) {
			$model = TableRegistry::get($this->_model);
			if (!$model) {
				throw new \RuntimeException(sprintf(
					'Unable to model "%s".', $this->_model
				));
			}
			$this->_model = $model;
		}

		$results = $model->find('all', [
				'conditions' => [
					'domain' => $this->_domain,
					'locale' => $this->_locale
				]
			])
			->hydrate(false)
			->toArray();

		return new Package($this->_formatter, null, $this->_messages($results));
	}

/**
 * Convert db results array to message array.
 *
 * @param array $results Results array from db
 * @return array
 */
	protected function _messages(array $results) {
		if (empty($results)) {
			return [];
		}

		$messages = [];
		$pluralForms = 0;
		for ($i = 5; $i > 0; $i--) {
			if (isset($results[0]['value_' . $i])) {
				$pluralForms = $i;
				break;
			}
		}

		foreach ($results as $item) {
			$singular = $item['singular'];
			$context = $item['context'];
			$translation = $item['value_0'];
			if ($context) {
				$messages[$singular]['_context'][$context] = $item['value_0'];
			} else {
				$messages[$singular] = $item['value_0'];
			}

			if (!empty($item['plural'])) {
				$key = $item['plural'];
				for ($i = 0; $i <= $pluralForms; $i++) {
					$plurals[] = $item['value_' . $i];
				}

				if ($context) {
					$messages[$key]['_context'][$context] = $plurals;
				} else {
					$messages[$key] = $plurals;<commit_msg>Iterate resultset instead of array.<commit_after><?php
namespace I18nMessages\I18n;

use Aura\Intl\Package;
use Cake\ORM\ResultSet;
use Cake\ORM\TableRegistry;
use Cake\Utility\Inflector;

/**
 * DbMessages loader.
 *
 * Returns translation messages stored in database.
 */
class DbMessagesLoader {

/**
 * The domain name
 *
 * @var string
 */
	protected $_domain;

/**
 * The locale to load messages for.
 *
 * @var string
 */
	protected $_locale;

/**
 * The model name to use for loading messages or model instance.
 *
 * @var mixed
 */
	protected $_model;

/**
 * Formatting used for messsages.
 * @var string
 */
	protected $_formatter;

/**
 * Constructor
 *
 * @param string $domain Domain name.
 * @param string $locale Locale string.
 * @param mixed $model Model name or instance. Defaults to 'I18nMessages.I18nMessages'.
 * @param string $formatter Formatter name. Defaults to 'default' (ICU formatter).
 */
	public function __construct(
		$domain,
		$locale,
		$model = null,
		$formatter = 'default'
	) {
		if (!$model) {
			$model = 'I18nMessages.I18nMessages';
		}
		$this->_domain = $domain;
		$this->_locale = $locale;
		$this->_model = $model;
		$this->_formatter = $formatter;
	}

/**
 * Fetches the translation messages from db and returns package with those
 * messages.
 *
 * @return \Aura\Intl\Package
 * @throws \RuntimeException If model could not be loaded.
 */
	public function __invoke() {
		$model = $this->_model;
		if (is_string($model)) {
			$model = TableRegistry::get($this->_model);
			if (!$model) {
				throw new \RuntimeException(sprintf(
					'Unable to model "%s".', $this->_model
				));
			}
			$this->_model = $model;
		}

		$results = $model->find('all', [
				'conditions' => [
					'domain' => $this->_domain,
					'locale' => $this->_locale
				]
			])
			->hydrate(false)
			->all();

		return new Package($this->_formatter, null, $this->_messages($results));
	}

/**
 * Convert db results to message array.
 *
 * @param \Cake\ORM\ResultSet $results ResultSet
 * @return array
 */
	protected function _messages(ResultSet $results) {
		if (!$results->count()) {
			return [];
		}

		$messages = [];
		$pluralForms = 0;
		$item = $results->first();
		for ($i = 5; $i > 0; $i--) {
			if (isset($item['value_' . $i])) {
				$pluralForms = $i;
				break;
			}
		}

		foreach ($results as $item) {
			$singular = $item['singular'];
			$context = $item['context'];
			$translation = $item['value_0'];
			if ($context) {
				$messages[$singular]['_context'][$context] = $item['value_0'];
			} else {
				$messages[$singular] = $item['value_0'];
			}

			if (!empty($item['plural'])) {
				$key = $item['plural'];
				for ($i = 0; $i <= $pluralForms; $i++) {
					$plurals[] = $item['value_' . $i];
				}

				if ($context) {
					$messages[$key]['_context'][$context] = $plurals;
				} else {
					$messages[$key] = $plurals;
{'fraction_non_alphanumeric': 0.17626280466266336, 'fraction_numerical': 0.00688802543270929, 'mean_word_length': 3.226119402985075, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 43, 'https://': 0, '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': '900540', 'n_tokens_mistral': 2198, 'n_tokens_neox': 1903, 'n_words': 489}