Source of file KeylistHydrator.php

Size: 1,300 Bytes - Last Modified: 2023-11-16T22:56:02+01:00

/home/websites/teicee/packagist/site/phpdoc/conf/../vendor/teicee/dorm-bundle/src/Hydration/KeylistHydrator.php

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
<?php
namespace TIC\DormBundle\Hydration;

use Doctrine\ORM\Internal\Hydration\AbstractHydrator;

/**
 * Récupération des résultats groupés par les valeurs de la 1ère colonne (en index).
 * Note: colonne 1 en clé et colonne 2 en valeurs uniquement
 *
 * Exemple:
 *   > SELECT a, b, c FROM t
 *   < array(
 *       a1 => array( b1 ),
 *       a2 => array( b2, b22 ),
 *       ...
 *     )
 */
class KeylistHydrator extends AbstractHydrator
{

	/**
	 * Hydrates all rows from the current statement instance at once.
	 *
	 * @return mixed[]
	 */
	protected function hydrateAllData(): array
	{
		$result = array();
		
		while ($data = $this->statement()->fetchNumeric()) {
			if (! \is_array($data) || \count($data) < 2) continue;
			
			$key = $data[0];
			if     ($key === null) $key = '[_NULL_]';
			elseif ($key === '')   $key = '[_EMPTY_]';
			
			$val = (\count($data) > 1) ? $data[1] : $data[0];
			
			if (! isset($result[ $key ])) $result[ $key ] = array();
			$result[ $key ][] = $val;
		}
		return $result;
	}

	/**
	 * BC layer for a wide-spread use-case of old DBAL APIs
	 *
	 * @deprecated This API is deprecated and will be removed after 2022
	 */
#	protected function hydrateAllData(): array
#	{
#		return $this->_stmt->fetchAll(\PDO::FETCH_COLUMN|\PDO::FETCH_GROUP, null);
#	}

}