<?php
/**
 * File containing the Categorie SIT SearchField class
 *
 * @copyright Copyright (C) 1999-2014 eZ Systems AS. All rights reserved.
 * @license http://ez.no/licenses/gnu_gpl GNU General Public License v2.0
 * @version 
 */

namespace Ayaline\Bundle\EzSitBundle\eZ\Publish\FieldType\AyalineCategoriesSit;

use eZ\Publish\SPI\Persistence\Content\Field;
use eZ\Publish\SPI\FieldType\Indexable;
use eZ\Publish\SPI\Persistence\Content\Search;

/**
 * Indexable definition for Categorie SIT field type
 */
class SearchField implements Indexable
{
	/**
	 * Get index data for field for search backend
	 *
	 * @param Field $field
	 *
	 * @return \eZ\Publish\SPI\Persistence\Content\Search\Field[]
	 */
	public function getIndexData( Field $field )
	{
		$simpleXml = new SimpleXMLElement($field->value->data, 0, true);

		$categories = array();

		foreach ($simpleXml->xpath("//categorie[@nbFils='0']") as $categorie) {
			$categories[$categorie->attributes()->id] = array('intitule' => $categorie->children()[0], 'idc' => $categorie->attributes()->id);
		}

		return array(
			new Search\Field(
				'value',
				$categories,
				new Search\FieldType\MultipleStringField()
			),
		);
	}

	/**
	 * Get index field types for search backend
	 *
	 * @return \eZ\Publish\SPI\Persistence\Content\Search\FieldType[]
	 */
	public function getIndexDefinition()
	{
		return array(
			'value' => new Search\FieldType\MultipleStringField(),
		);
	}
}
