<commit_before><?php
/**
 * This file is part of stubbles.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @package  net\stubbles\input
 */
namespace net\stubbles\input\broker;
use net\stubbles\input\Request;
use net\stubbles\lang\BaseObject;
use net\stubbles\lang\reflect\annotation\Annotation;
/**
 * Broker class to transfer values from the request into an object via annotations.
 *
 * @Singleton
 */
class RequestBroker extends BaseObject
{
    /**
     * the matcher to be used for methods and properties
     *
     * @type  RequestBrokerMethods
     */
    private $brokerMethods;
    /**
     * factory to create filters with
     *
     * @type  ParamBrokerMap
     */
    private $paramBrokerMap;

    /**
     * constructor
     *
     * @param  RequestBrokerMethods  $brokerMethods
     * @param  ParamBrokerMap        $paramBrokerMap
     * @Inject
     */
    public function __construct(RequestBrokerMethods $brokerMethods, ParamBrokerMap $paramBrokerMap)
    {
        $this->brokerMethods  = $brokerMethods;
        $this->paramBrokerMap = $paramBrokerMap;
    }

    /**
     * fills given object with values from request
     *
     * @param   Request  $request
     * @param   object   $object   the object instance to fill with values
     * @param   string   $group    group of values to filter
     */
    public function procure(Request $request, $object, $group = null)
    {
        foreach ($this->brokerMethods->get($object) as $refMethod) {
            $requestAnnotation = $refMethod->getAnnotation('Request');
            if ($this->isNotInGroup($group, $requestAnnotation)) {
                continue;
            }

            $value = $this->paramBrokerMap->getBroker($requestAnnotation->getAnnotationName())
                                          ->procure($request, $requestAnnotation);
            if (null !== $value) {
                $refMethod->invoke($object, $value);
            }
        }
    }

    /**
     * checks whether the annotation belongs to the given group
     *
     * @param   string      $group
     * @param   Annotation  $requestAnnotation
     * @return  bool
     */
    private function isNotInGroup($group, Annotation $requestAnnotation)
    {
        if (empty($group)) {
            return false;
        }

        return $requestAnnotation->getGroup() !== $group;
    }

    /**
     * returns all methods of given instance which are applicable for brokerage
     *
     * @param   object  $object
     * @param   string  $group   restrict list to given group
     * @return  ReflectionMethod[]
     */
    public function getMethods($object, $group = null)
    {
        return $this->brokerMethods->get($object, $group);
    }

    /**
     * returns a list of all request annotations on given object
     *
     * @param   object  $object
     * @param   string  $group   restrict list to given group
     * @return  net\stubbles\lang\reflect\annotation\Annotation[]
     */
    public function getAnnotations($object, $group = null)
    {
        return $this->brokerMethods->getAnnotations($object, $group);
    }
}
?><commit_msg>optimize<commit_after><?php
/**
 * This file is part of stubbles.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @package  net\stubbles\input
 */
namespace net\stubbles\input\broker;
use net\stubbles\input\Request;
use net\stubbles\lang\BaseObject;
/**
 * Broker class to transfer values from the request into an object via annotations.
 *
 * @Singleton
 */
class RequestBroker extends BaseObject
{
    /**
     * the matcher to be used for methods and properties
     *
     * @type  RequestBrokerMethods
     */
    private $brokerMethods;
    /**
     * factory to create filters with
     *
     * @type  ParamBrokerMap
     */
    private $paramBrokerMap;

    /**
     * constructor
     *
     * @param  RequestBrokerMethods  $brokerMethods
     * @param  ParamBrokerMap        $paramBrokerMap
     * @Inject
     */
    public function __construct(RequestBrokerMethods $brokerMethods, ParamBrokerMap $paramBrokerMap)
    {
        $this->brokerMethods  = $brokerMethods;
        $this->paramBrokerMap = $paramBrokerMap;
    }

    /**
     * fills given object with values from request
     *
     * @param   Request  $request
     * @param   object   $object   the object instance to fill with values
     * @param   string   $group    restrict procurement to given group
     */
    public function procure(Request $request, $object, $group = null)
    {
        foreach ($this->brokerMethods->get($object, $group) as $refMethod) {
            $requestAnnotation = $refMethod->getAnnotation('Request');
            $value = $this->paramBrokerMap->getBroker($requestAnnotation->getAnnotationName())
                                          ->procure($request, $requestAnnotation);
            if (null !== $value) {
                $refMethod->invoke($object, $value);
            }
        }
    }

    /**
     * returns all methods of given instance which are applicable for brokerage
     *
     * @param   object  $object
     * @param   string  $group   restrict list to given group
     * @return  ReflectionMethod[]
     */
    public function getMethods($object, $group = null)
    {
        return $this->brokerMethods->get($object, $group);
    }

    /**
     * returns a list of all request annotations on given object
     *
     * @param   object  $object
     * @param   string  $group   restrict list to given group
     * @return  net\stubbles\lang\reflect\annotation\Annotation[]
     */
    public function getAnnotations($object, $group = null)
    {
        return $this->brokerMethods->getAnnotations($object, $group);
    }
}
?>
{'fraction_non_alphanumeric': 0.10239316239316239, 'fraction_numerical': 0.0, 'mean_word_length': 2.316893424036281, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6740432', 'n_tokens_mistral': 1610, 'n_tokens_neox': 1603, 'n_words': 446}