<commit_before><?php
/**
 * Phossa Project
 *
 * PHP version 5.4
 *
 * @category  Library
 * @package   Phossa2\Shared
 * @copyright Copyright (c) 2016 phossa.com
 * @license   http://mit-license.org/ MIT License
 * @link      http://www.phossa.com/
 */
/*# declare(strict_types=1); */

namespace Phossa2\Shared\Reference;

use Phossa2\Shared\Message\Message;
use Phossa2\Shared\Exception\InvalidArgumentException;

/**
 * DelegatorTrait
 *
 * Implementation of DelegatorInterface
 *
 * @package Phossa2\Shared
 * @author  <NAME> <<EMAIL>>
 * @see     DelegatorInterface
 * @version 2.0.8
 * @since   2.0.8 added
 */
trait DelegatorTrait
{
    /**
     * lookup pool of registries
     *
     * @var    array
     * @access protected
     */
    protected $lookup_pool = [];

    /**
     * cached lookup key
     *
     * @var    string
     * @access private
     */
    private $cache_key;

    /**
     * cached lookup registry
     *
     * @var    object
     * @access private
     */
    private $cache_reg;

    /**
     * {@inheritDoc}
     */
    public function addRegistry($registry)
    {
        /* @var $registry DelegatorAwareInterface */

        // check registry type
        if (!$this->isValidRegistry($registry)) {
            throw new InvalidArgumentException(
                Message::get(Message::MSG_ARGUMENT_INVALID, get_class($registry)),
                Message::MSG_ARGUMENT_INVALID
            );
        }

        // remove this registry if exists already
        $this->removeFromLookup($registry);

        // append to the pool end
        $this->lookup_pool[] = $registry;

        return $this;
    }

    /**
     * {@inheritDoc}
     */
    public function hasInLookup(/*# string */ $key)/*# : bool */
    {
        foreach ($this->lookup_pool as $registry) {
            if ($this->hasInRegistry($registry, $key)) {
                $this->cache_key = $key;
                $this->cache_reg = $registry;
                return true;
            }
        }
        return false;
    }

    /**
     * {@inheritDoc}
     */
    public function getFromLookup(/*# string */ $key)
    {
        // lookup already ? try lookup first
        if ($key === $this->cache_key || $this->hasInLookup($key)) {
            return $this->getFromRegistry($this->cache_reg, $key);
        }
        // not found
        return null;
    }

    /**
     * Remove one object from the pool
     *
     * @param  object $registry
     * @return $this
     * @access protected
     */
    protected function removeFromLookup($registry)
    {
        foreach ($this->lookup_pool as $idx => $reg) {
            if ($registry === $reg) {
                unset($this->lookup_pool[$idx]);
            }
        }
        return $this;
    }

    /**
     * Is registry type allowed in lookup pool ?
     *
     * @param  object $registry
     * @return bool
     * @access protected
     */
    abstract protected function isValidRegistry($registry)/*# : bool */;

    /**
     * Try has in registry
     *
     * @param  object $registry
     * @param  name $key
     * @return bool
     * @access protected
     */
    abstract protected function hasInRegistry(
        $registry,
        /*# string */ $key
    )/*# : bool */;

    /**
     * Try get from registry
     *
     * @param  object $registry
     * @param  name $key
     * @return mixed
     * @access protected
     */
    abstract protected function getFromRegistry(
        $registry,
        /*# string */ $key
    );
}
<commit_msg>fix setDelegator bug<commit_after><?php
/**
 * Phossa Project
 *
 * PHP version 5.4
 *
 * @category  Library
 * @package   Phossa2\Shared
 * @copyright Copyright (c) 2016 phossa.com
 * @license   http://mit-license.org/ MIT License
 * @link      http://www.phossa.com/
 */
/*# declare(strict_types=1); */

namespace Phossa2\Shared\Reference;

use Phossa2\Shared\Message\Message;
use Phossa2\Shared\Exception\InvalidArgumentException;

/**
 * DelegatorTrait
 *
 * Implementation of DelegatorInterface
 *
 * @package Phossa2\Shared
 * @author  <NAME> <<EMAIL>>
 * @see     DelegatorInterface
 * @version 2.0.8
 * @since   2.0.8 added
 */
trait DelegatorTrait
{
    /**
     * lookup pool of registries
     *
     * @var    array
     * @access protected
     */
    protected $lookup_pool = [];

    /**
     * cached lookup key
     *
     * @var    string
     * @access private
     */
    private $cache_key;

    /**
     * cached lookup registry
     *
     * @var    object
     * @access private
     */
    private $cache_reg;

    /**
     * {@inheritDoc}
     */
    public function addRegistry($registry)
    {
        /* @var $registry DelegatorAwareInterface */

        // check registry type
        if (!$this->isValidRegistry($registry)) {
            throw new InvalidArgumentException(
                Message::get(Message::MSG_ARGUMENT_INVALID, get_class($registry)),
                Message::MSG_ARGUMENT_INVALID
            );
        }
        // register delegator
        $registry->setDelegator($this);

        // remove this registry if exists already
        $this->removeFromLookup($registry);

        // append to the pool end
        $this->lookup_pool[] = $registry;

        return $this;
    }

    /**
     * {@inheritDoc}
     */
    public function hasInLookup(/*# string */ $key)/*# : bool */
    {
        foreach ($this->lookup_pool as $registry) {
            if ($this->hasInRegistry($registry, $key)) {
                $this->cache_key = $key;
                $this->cache_reg = $registry;
                return true;
            }
        }
        return false;
    }

    /**
     * {@inheritDoc}
     */
    public function getFromLookup(/*# string */ $key)
    {
        // lookup already ? try lookup first
        if ($key === $this->cache_key || $this->hasInLookup($key)) {
            return $this->getFromRegistry($this->cache_reg, $key);
        }
        // not found
        return null;
    }

    /**
     * Remove one object from the pool
     *
     * @param  object $registry
     * @return $this
     * @access protected
     */
    protected function removeFromLookup($registry)
    {
        foreach ($this->lookup_pool as $idx => $reg) {
            if ($registry === $reg) {
                unset($this->lookup_pool[$idx]);
            }
        }
        return $this;
    }

    /**
     * Is registry type allowed in lookup pool ?
     *
     * @param  object $registry
     * @return bool
     * @access protected
     */
    abstract protected function isValidRegistry($registry)/*# : bool */;

    /**
     * Try has in registry
     *
     * @param  object $registry
     * @param  name $key
     * @return bool
     * @access protected
     */
    abstract protected function hasInRegistry(
        $registry,
        /*# string */ $key
    )/*# : bool */;

    /**
     * Try get from registry
     *
     * @param  object $registry
     * @param  name $key
     * @return mixed
     * @access protected
     */
    abstract protected function getFromRegistry(
        $registry,
        /*# string */ $key
    );
}

{'fraction_non_alphanumeric': 0.12979890310786105, 'fraction_numerical': 0.005062579102798481, 'mean_word_length': 1.786833855799373, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 38, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '4815990', 'n_tokens_mistral': 2106, 'n_tokens_neox': 2041, 'n_words': 487}