<commit_before><?php

namespace Nohex\Eix\Services\Data\Responders;

use Nohex\Eix\Core\Exception;
use Nohex\Eix\Core\Responders\Restricted;
use Nohex\Eix\Core\Responses\Http;
use Nohex\Eix\Core\Responses\Http\Html;
use Nohex\Eix\Core\Responses\Http\Redirection;
use Nohex\Eix\Services\Data\Entity;
use Nohex\Eix\Services\Data\Factory;
use Nohex\Eix\Services\Net\Http\BadRequestException;
use Nohex\Eix\Services\Net\Http\NotFoundException;

/**
 * A responder used to manage the contents of a collection of entities.
 */
abstract class CollectionManager extends CollectionBrowser implements Restricted
{
    private $collectionBrowser;

    /**
     * Get this collection manager's underlying collection browser.
     *
     * @return CollectionBrowser
     */
    protected function getCollectionBrowser()
    {
        if (empty($this->collectionBrowser)) {
            throw new Exception('No collection browser set for ' . get_called_class());
        }

        return $this->collectionBrowser;
    }

    /**
     * Establish this collection manager's underlying collection browser.
     *
     * @param \Nohex\Eix\Services\Data\Responders\CollectionBrowser $collectionBrowser
     */
    protected function setCollectionBrowser(CollectionBrowser $collectionBrowser)
    {
        $this->collectionBrowser = $collectionBrowser;
    }

    /**
     * The default factory defaults to the collection browser's.
     *
     * @return Factory
     */
    public function getDefaultFactory()
    {
        return $this->getCollectionBrowser()->getDefaultFactory();
    }

    /**
     * @see CollectionBrowser::getCollectionName;
     *
     * Lacking polymorphism and PHP 5.4's traits, this technique allows using
     * both an implementation of the collection browser, and the implementation
     * of the collection manager.
     */
    public function getCollectionName()
    {
        return $this->getCollectionBrowser()->getCollectionName();
    }

    /**
     * @see CollectionBrowser::getCollectionName;
     *
     * Lacking polymorphism and PHP 5.4's traits, this technique allows using
     * both an implementation of the collection browser, and the implementation
     * of the collection manager.
     */
    public function getItemName()
    {
        return $this->getCollectionBrowser()->getItemName();
    }

    /**
     * @see CollectionBrowser::getEntity;
     *
     * Lacking polymorphism and PHP 5.4's traits, this technique allows using
     * both an implementation of the collection browser, and the implementation
     * of the collection manager.
     */
    protected function getEntity($id)
    {
        return $this->getCollectionBrowser()->getEntity($id);
    }

    /**
     * @see CollectionBrowser::getEntities;
     *
     * Lacking polymorphism and PHP 5.4's traits, this technique allows using
     * both an implementation of the collection browser, and the implementation
     * of the collection manager.
     */
    protected function getEntities($view = null)
    {
        return $this->getCollectionBrowser()->getEntities($view);
    }

    /**
     * Destroys the selected entity.
     */
    protected function destroyEntity($id)
    {
        return $this->getFactory()->findEntity($id)->destroy();
    }

    /**
     * Updates the entity with the new data in the request and stores it.
     *
     * @return Entity the stored entity
     */
    protected function storeEntity()
    {
        $request = $this->getRequest();
        $id = $request->getParameter('id');
        // Try to find an existing instance of the entity.
        $entity = NULL;
        try {
            $entity = $this->getFactory()->findEntity($id);
        } catch (NotFoundException $exception) {
            // Not found? Create it.
            $className = $this->getFactory()->getEntitiesClassName();
            $entity = new $className(array(
                'id' => $id,
            ));
        }


        // Update the product from the request data.
        $entity->update($this->getEntityDataFromRequest());
        // Store the entity.<commit_msg>Add flag to collection manager to help interpret what to do with nulls in requests.<commit_after><?php

namespace Nohex\Eix\Services\Data\Responders;

use Nohex\Eix\Core\Exception;
use Nohex\Eix\Core\Responders\Restricted;
use Nohex\Eix\Core\Responses\Http;
use Nohex\Eix\Core\Responses\Http\Html;
use Nohex\Eix\Core\Responses\Http\Redirection;
use Nohex\Eix\Services\Data\Entity;
use Nohex\Eix\Services\Data\Factory;
use Nohex\Eix\Services\Net\Http\BadRequestException;
use Nohex\Eix\Services\Net\Http\NotFoundException;

/**
 * A responder used to manage the contents of a collection of entities.
 */
abstract class CollectionManager extends CollectionBrowser implements Restricted
{
    private $collectionBrowser;

    // Whether null values are ignored when storing. By default it is false,
    // which means that null values will set their associated field to null.
    protected $nullsIgnoredOnStore = false;

    /**
     * Get this collection manager's underlying collection browser.
     *
     * @return CollectionBrowser
     */
    protected function getCollectionBrowser()
    {
        if (empty($this->collectionBrowser)) {
            throw new Exception('No collection browser set for ' . get_called_class());
        }

        return $this->collectionBrowser;
    }

    /**
     * Establish this collection manager's underlying collection browser.
     *
     * @param \Nohex\Eix\Services\Data\Responders\CollectionBrowser $collectionBrowser
     */
    protected function setCollectionBrowser(CollectionBrowser $collectionBrowser)
    {
        $this->collectionBrowser = $collectionBrowser;
    }

    /**
     * The default factory defaults to the collection browser's.
     *
     * @return Factory
     */
    public function getDefaultFactory()
    {
        return $this->getCollectionBrowser()->getDefaultFactory();
    }

    /**
     * @see CollectionBrowser::getCollectionName;
     *
     * Lacking polymorphism and PHP 5.4's traits, this technique allows using
     * both an implementation of the collection browser, and the implementation
     * of the collection manager.
     */
    public function getCollectionName()
    {
        return $this->getCollectionBrowser()->getCollectionName();
    }

    /**
     * @see CollectionBrowser::getCollectionName;
     *
     * Lacking polymorphism and PHP 5.4's traits, this technique allows using
     * both an implementation of the collection browser, and the implementation
     * of the collection manager.
     */
    public function getItemName()
    {
        return $this->getCollectionBrowser()->getItemName();
    }

    /**
     * @see CollectionBrowser::getEntity;
     *
     * Lacking polymorphism and PHP 5.4's traits, this technique allows using
     * both an implementation of the collection browser, and the implementation
     * of the collection manager.
     */
    protected function getEntity($id)
    {
        return $this->getCollectionBrowser()->getEntity($id);
    }

    /**
     * @see CollectionBrowser::getEntities;
     *
     * Lacking polymorphism and PHP 5.4's traits, this technique allows using
     * both an implementation of the collection browser, and the implementation
     * of the collection manager.
     */
    protected function getEntities($view = null)
    {
        return $this->getCollectionBrowser()->getEntities($view);
    }

    /**
     * Destroys the selected entity.
     */
    protected function destroyEntity($id)
    {
        return $this->getFactory()->findEntity($id)->destroy();
    }

    /**
     * Updates the entity with the new data in the request and stores it.
     *
     * @return Entity the stored entity
     */
    protected function storeEntity()
    {
        $request = $this->getRequest();
        $id = $request->getParameter('id');
        // Try to find an existing instance of the entity.
        $entity = NULL;
        try {
            $entity = $this->getFactory()->findEntity($id);
        } catch (NotFoundException $exception) {
            // Not found? Create it.
            $className = $this->getFactory()->getEntitiesClassName();
            $entity = new $className(array(
                'id' => $id,
            ));
        }


        // Get the data from the request.
        $dataFromRequest = $this->getEntityDataFromRequest();
        if ($this->nullsIgnoredOnStore) {
            // Remove unspecified values, so that they are not modified.
            $dataFromRequest = array_filter(
                $dataFromRequest,
                function ($value) {
                    return !is_null($value);
                }
            );
        }
        // Update the product from the request data.
        $entity->update($dataFromRequest);
        // Store the entity.
{'fraction_non_alphanumeric': 0.10142308561102327, 'fraction_numerical': 0.0018070928393946238, 'mean_word_length': 2.5462555066079293, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 54, 'https://': 0, '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': '3403876', 'n_tokens_mistral': 2323, 'n_tokens_neox': 2246, 'n_words': 656}