Actions

Source Code of This Project

/lib/model/search/Composite.php

<?php
/*
 * This file is part of the pwp package.
 * (c) 2009-2010 Victor Rad' <victor.v.rad[at]gmail.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
*/

/**
 * Search collection
 * Represent of Composite pattern
 *
 * @package    pwp
 * @subpackage Search
 * @author     Victor Rad'
 */
class Search_Composite extends Search
{
    protected $instances = array();

    /**
     * Register new instances of search collection
     *
     * @param array of search $instances
     */
    public function __construct()
    {
        $searchInstances = func_get_args();
        $this->register($searchInstances);
    }

    /**
     * Setter found ids for search collection
     */
    public function setFoundIds($foundIds)
    {
        foreach ($foundIds as $key => $ids){
            $this->instances[$key]->setFoundIds($ids);
        }
    }

    /**
     * Getter found ids for search collection
     */
    public function getFoundIds()
    {
        foreach ($this->instances as $instance){
            $foundIds[] = $instance->getFoundIds();
        }

        return $foundIds;
    }

    /**
     * Getter found data for search collection
     */
    public function getFoundData()
    {
        foreach ($this->instances as $instance){
            $foundData[] = $instance->getFoundData();
        }

        return $foundData;
    }

    /**
     * Register new instances of search collection
     *
     * @param array of search $instances
     */
    public function register($instances)
    {
        foreach ($instances as $instance) {
            if ($instance instanceof Search){
                array_push($this->instances, $instance);
            }
            else{
                throw new Exception(sprintf('Instance %s must implements of Search class', $instance));
            }
        }
    }

    /**
     * Find words in search collection
     *
     * @param string $query
     */
    public function find($query)
    {
        $foundIds = array();
        foreach ($this->instances as $instance){
            $foundIds[] = $instance->find($query);
        }

        return $foundIds;
    }

    /**
     * Render templates in search collection
     *
     * @return string
     */
    public function render()
    {
        $rendered = '';
        foreach ($this->instances as $instance){
            $rendered .= $instance->render();
        }

        return $rendered;
    }

    /**
     * Delete indexes and load
     * all data from scratch in search collection
     *
     * @return <type>
     */
    public function rebuildIndex()
    {
        foreach ($this->instances as $instance){
            $instance->rebuildIndex();
        }
    }

}