<commit_before><?php
/**
 * doctrine custom type to handle reading and writing $refs attributes
 */

namespace Graviton\DocumentBundle\Types;

use Symfony\Bundle\FrameworkBundle\Routing\Router;
use Symfony\Component\Routing\Route;
use Doctrine\ODM\MongoDB\Types\Type;

/**
 * based on http://doctrine-mongodb-odm.readthedocs.org/en/latest/reference/basic-mapping.html#custom-mapping-types
 *
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
 * @license  http://opensource.org/licenses/MIT MIT License
 * @link     http://swisscom.ch
 */
class ExtReference extends Type
{
    /**
     * @var Router
     */
    private $router;

    /**
     * @var array
     */
    private $mapping;

    /**
     * inject a router
     *
     * This uses setter injection due to the fact that doctrine doesn't do constructor injection
     *
     * @param Router $router router
     *
     * @return void
     */
    public function setRouter(Router $router)
    {
        $this->router = $router;
    }

    /**
     * inject collection name to routing service mapping
     *
     * @param array $mapping colleciton_name => service_id mapping
     *
     * @return void
     */
    public function setMapping(array $mapping)
    {
        $this->mapping = $mapping;
    }

    /**
     * get php value when field is used as identifier
     *
     * @param \MongoDBRef $value ref from mongodb
     *
     * @return string
     */
    public function convertToPHPValue($value)
    {
        if (
            !array_key_exists('$ref', $value)
            && !array_key_exists($value['$ref'], $this->mapping)
            && !array_key_exists('$id', $value)
        ) {
            return '';
        }
        return $this->router->generate($this->mapping[$value['$ref']], ['id' => $value['$id']]);
    }

    /**
     * return a closure as string that sets $return if field is a regular field
     *
     * @return string
     */
    public function closureToPHP()
    {
        // return full value for later processing since we do not have mappings during hydrator generation
        return '$return = json_encode($value);';
    }

    /**
     * return the mongodb representation from a php value
     *
     * @param string $value value of reference as URI
     *
     * @return array
     */
    public function convertToDatabaseValue($value)
    {
        if (empty($this->router)) {
            throw new \RuntimeException('no router injected into '.__CLASS__);<commit_msg>All hail the mighty CodeSniffer<commit_after><?php
/**
 * doctrine custom type to handle reading and writing $refs attributes
 */

namespace Graviton\DocumentBundle\Types;

use Symfony\Bundle\FrameworkBundle\Routing\Router;
use Symfony\Component\Routing\Route;
use Doctrine\ODM\MongoDB\Types\Type;

/**
 * based on http://doctrine-mongodb-odm.readthedocs.org/en/latest/reference/basic-mapping.html#custom-mapping-types
 *
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
 * @license  http://opensource.org/licenses/MIT MIT License
 * @link     http://swisscom.ch
 */
class ExtReference extends Type
{
    /**
     * @var Router
     */
    private $router;

    /**
     * @var array
     */
    private $mapping;

    /**
     * inject a router
     *
     * This uses setter injection due to the fact that doctrine doesn't do constructor injection
     *
     * @param Router $router router
     *
     * @return void
     */
    public function setRouter(Router $router)
    {
        $this->router = $router;
    }

    /**
     * inject collection name to routing service mapping
     *
     * @param array $mapping colleciton_name => service_id mapping
     *
     * @return void
     */
    public function setMapping(array $mapping)
    {
        $this->mapping = $mapping;
    }

    /**
     * get php value when field is used as identifier
     *
     * @param \MongoDBRef $value ref from mongodb
     *
     * @return string
     */
    public function convertToPHPValue($value)
    {
        if (!array_key_exists('$ref', $value)
            && !array_key_exists($value['$ref'], $this->mapping)
            && !array_key_exists('$id', $value)
        ) {
            return '';
        }
        return $this->router->generate($this->mapping[$value['$ref']], ['id' => $value['$id']]);
    }

    /**
     * return a closure as string that sets $return if field is a regular field
     *
     * @return string
     */
    public function closureToPHP()
    {
        // return full value for later processing since we do not have mappings during hydrator generation
        return '$return = json_encode($value);';
    }

    /**
     * return the mongodb representation from a php value
     *
     * @param string $value value of reference as URI
     *
     * @return array
     */
    public function convertToDatabaseValue($value)
    {
        if (empty($this->router)) {
            throw new \RuntimeException('no router injected into '.__CLASS__);
{'fraction_non_alphanumeric': 0.11860696517412936, 'fraction_numerical': 0.0, 'mean_word_length': 2.3328912466843503, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 23, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, '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': '6809894', 'n_tokens_mistral': 1464, 'n_tokens_neox': 1421, 'n_words': 391}