<commit_before><?php

namespace Muffin\Webservice\Model;

use ArrayObject;
use BadMethodCallException;
use Cake\Core\App;
use Cake\Datasource\EntityInterface;
use Cake\Datasource\Exception\InvalidPrimaryKeyException;
use Cake\Datasource\RepositoryInterface;
use Cake\Datasource\RulesAwareTrait;
use Cake\Datasource\RulesChecker;
use Cake\Event\EventDispatcherInterface;
use Cake\Event\EventDispatcherTrait;
use Cake\Event\EventListenerInterface;
use Cake\Event\EventManager;
use Cake\Network\Exception\NotImplementedException;
use Cake\Utility\Inflector;
use Cake\Validation\ValidatorAwareTrait;
use Muffin\Webservice\Exception\MissingResourceClassException;
use Muffin\Webservice\Exception\UnexpectedDriverException;
use Muffin\Webservice\Marshaller;
use Muffin\Webservice\Query;
use Muffin\Webservice\Schema;
use Muffin\Webservice\StreamQuery;

/**
 * The table equivalent of a webservice endpoint
 *
 * @package Muffin\Webservice\Model
 */
class Endpoint implements RepositoryInterface, EventListenerInterface, EventDispatcherInterface
{

    use EventDispatcherTrait;
    use RulesAwareTrait;
    use ValidatorAwareTrait;

    /**
     * Name of default validation set.
     *
     * @var string
     */
    const DEFAULT_VALIDATOR = 'default';

    /**
     * The alias this object is assigned to validators as.
     *
     * @var string
     */
    const VALIDATOR_PROVIDER_NAME = 'endpoint';

    protected $_connection;

    /**
     * The schema object containing a description of this endpoint fields
     *
     * @var \Muffin\Webservice\Schema
     */
    protected $_schema;

    /**
     * The name of the class that represent a single resource for this endpoint
     *
     * @var string
     */
    protected $_resourceClass;

    /**
     * Registry key used to create this endpoint object
     *
     * @var string
     */
    protected $_registryAlias;

    /**
     * The name of the endpoint to contact
     *
     * @var string
     */
    protected $_endpoint;

    /**
     * The name of the field that represents the primary key in the endpoint
     *
     * @var string|array
     */
    protected $_primaryKey;

    /**
     * The name of the field that represents a human readable representation of a row
     *
     * @var string
     */
    protected $_displayField;

    /**
     * The webservice instance to call
     *
     * @var \Muffin\Webservice\Webservice\WebserviceInterface
     */
    protected $_webservice;

    /**
     * The alias to use for the endpoint
     *
     * @var string
     */
    protected $_alias;

    /**
     * The inflect method to use for endpoint routes
     *
     * @var string
     */
    protected $_inflectionMethod = 'underscore';

    /**
     * Initializes a new instance
     *
     * The $config array understands the following keys:
     *
     * - alias: Alias to be assigned to this endpoint (default to endpoint name)
     * - connection: The connection instance to use
     * - endpoint: Name of the endpoint to represent
     * - resourceClass: The fully namespaced class name of the resource class that will
     *   represent rows in this endpoint.
     * - schema: A \Muffin\Webservice\Schema object or an array that can be
     *   passed to it.
     *
     * @param array $config List of options for this endpoint
     */
    public function __construct(array $config = [])
    {
        if (!empty($config['alias'])) {
            $this->alias($config['alias']);
        }
        if (!empty($config['connection'])) {
            $this->connection($config['connection']);
        }
        if (!empty($config['displayField'])) {
            $this->displayField($config['displayField']);
        }
        if (!empty($config['endpoint'])) {
            $this->endpoint($config['endpoint']);
        }
        $eventManager = null;
        if (!empty($config['eventManager'])) {
            $eventManager = $config['eventManager'];
        }
        if (!empty($config['primaryKey'])) {
            $this->primaryKey($config['primaryKey']);
        }
        if (!empty($config['schema'])) {
            $this->schema($config['schema']);
        }
        if (!empty($config['registryAlias'])) {
            $this->registryAlias($config['registryAlias']);
        }
        if (!empty($config['resourceClass'])) {
            $this->resourceClass($config['resourceClass']);
        }
        if (!empty($config['inflect'])) {
            $this->inflectionMethod($config['inflect']);
        }

        $this->_eventManager = $eventManager ?: new EventManager();

        $this->initialize($config);
        $this->_eventManager->on($this);
        $this->dispatchEvent('Model.initialize');
    }

    /**
     * Get the default connection name.
     *
     * This method is used to get the fallback connection name if an
     * instance is created through the EndpointRegistry without a connection.
     *
     * @return string
     *
     * @see \Muffin\Webservice\Model\EndpointRegistry::get()
     */
    public static function defaultConnectionName()
    {
        $namespaceParts = explode('\\', get_called_class());
        $plugin = array_slice(array_reverse($namespaceParts), 3, 2);

        return Inflector::underscore(current($plugin));
    }

    /**
     * Initialize a endpoint instance. Called after the constructor.
     *
     * You can use this method to define validation and do any other initialization logic you need.
     *<commit_msg>Use getter, setter instead to directly accessing property.<commit_after><?php

namespace Muffin\Webservice\Model;

use ArrayObject;
use BadMethodCallException;
use Cake\Core\App;
use Cake\Datasource\EntityInterface;
use Cake\Datasource\Exception\InvalidPrimaryKeyException;
use Cake\Datasource\RepositoryInterface;
use Cake\Datasource\RulesAwareTrait;
use Cake\Datasource\RulesChecker;
use Cake\Event\EventDispatcherInterface;
use Cake\Event\EventDispatcherTrait;
use Cake\Event\EventListenerInterface;
use Cake\Event\EventManager;
use Cake\Network\Exception\NotImplementedException;
use Cake\Utility\Inflector;
use Cake\Validation\ValidatorAwareTrait;
use Muffin\Webservice\Exception\MissingResourceClassException;
use Muffin\Webservice\Exception\UnexpectedDriverException;
use Muffin\Webservice\Marshaller;
use Muffin\Webservice\Query;
use Muffin\Webservice\Schema;
use Muffin\Webservice\StreamQuery;

/**
 * The table equivalent of a webservice endpoint
 *
 * @package Muffin\Webservice\Model
 */
class Endpoint implements RepositoryInterface, EventListenerInterface, EventDispatcherInterface
{

    use EventDispatcherTrait;
    use RulesAwareTrait;
    use ValidatorAwareTrait;

    /**
     * Name of default validation set.
     *
     * @var string
     */
    const DEFAULT_VALIDATOR = 'default';

    /**
     * The alias this object is assigned to validators as.
     *
     * @var string
     */
    const VALIDATOR_PROVIDER_NAME = 'endpoint';

    protected $_connection;

    /**
     * The schema object containing a description of this endpoint fields
     *
     * @var \Muffin\Webservice\Schema
     */
    protected $_schema;

    /**
     * The name of the class that represent a single resource for this endpoint
     *
     * @var string
     */
    protected $_resourceClass;

    /**
     * Registry key used to create this endpoint object
     *
     * @var string
     */
    protected $_registryAlias;

    /**
     * The name of the endpoint to contact
     *
     * @var string
     */
    protected $_endpoint;

    /**
     * The name of the field that represents the primary key in the endpoint
     *
     * @var string|array
     */
    protected $_primaryKey;

    /**
     * The name of the field that represents a human readable representation of a row
     *
     * @var string
     */
    protected $_displayField;

    /**
     * The webservice instance to call
     *
     * @var \Muffin\Webservice\Webservice\WebserviceInterface
     */
    protected $_webservice;

    /**
     * The alias to use for the endpoint
     *
     * @var string
     */
    protected $_alias;

    /**
     * The inflect method to use for endpoint routes
     *
     * @var string
     */
    protected $_inflectionMethod = 'underscore';

    /**
     * Initializes a new instance
     *
     * The $config array understands the following keys:
     *
     * - alias: Alias to be assigned to this endpoint (default to endpoint name)
     * - connection: The connection instance to use
     * - endpoint: Name of the endpoint to represent
     * - resourceClass: The fully namespaced class name of the resource class that will
     *   represent rows in this endpoint.
     * - schema: A \Muffin\Webservice\Schema object or an array that can be
     *   passed to it.
     *
     * @param array $config List of options for this endpoint
     */
    public function __construct(array $config = [])
    {
        if (!empty($config['alias'])) {
            $this->alias($config['alias']);
        }
        if (!empty($config['connection'])) {
            $this->connection($config['connection']);
        }
        if (!empty($config['displayField'])) {
            $this->displayField($config['displayField']);
        }
        if (!empty($config['endpoint'])) {
            $this->endpoint($config['endpoint']);
        }
        if (!empty($config['primaryKey'])) {
            $this->primaryKey($config['primaryKey']);
        }
        if (!empty($config['schema'])) {
            $this->schema($config['schema']);
        }
        if (!empty($config['registryAlias'])) {
            $this->registryAlias($config['registryAlias']);
        }
        if (!empty($config['resourceClass'])) {
            $this->resourceClass($config['resourceClass']);
        }
        if (!empty($config['inflect'])) {
            $this->inflectionMethod($config['inflect']);
        }

        if (!empty($config['eventManager'])) {
            $this->setEventManager($config['eventManager']);
        }

        $this->initialize($config);
        $this->getEventManager()->on($this);
        $this->dispatchEvent('Model.initialize');
    }

    /**
     * Get the default connection name.
     *
     * This method is used to get the fallback connection name if an
     * instance is created through the EndpointRegistry without a connection.
     *
     * @return string
     *
     * @see \Muffin\Webservice\Model\EndpointRegistry::get()
     */
    public static function defaultConnectionName()
    {
        $namespaceParts = explode('\\', get_called_class());
        $plugin = array_slice(array_reverse($namespaceParts), 3, 2);

        return Inflector::underscore(current($plugin));
    }

    /**
     * Initialize a endpoint instance. Called after the constructor.
     *
     * You can use this method to define validation and do any other initialization logic you need.
     *
{'fraction_non_alphanumeric': 0.11177718343631836, 'fraction_numerical': 0.00036890159549940053, 'mean_word_length': 2.5907284768211922, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 31, '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': '7112909', 'n_tokens_mistral': 2948, 'n_tokens_neox': 2870, 'n_words': 800}