<commit_before><?php
/**
 * Generic entity
 *
 * @author    <NAME> <<EMAIL>>
 * @copyright 2015 (c) Oasiswork
 * @license   https://opensource.org/licenses/MIT MIT
 */
namespace Crunchmail\Entities;

use Crunchmail\Client;
use Crunchmail\Resources\GenericResource;

/**
 * Generic entity class
 */
class GenericEntity
{
    /**
     * Caller resource
     *
     * @var object
     */
    protected $_resource;

    /**
     * Entity body
     *
     * @var stdClass
     */
    protected $_body;

    /**
     * Links remapping
     *
     * @var array
     */
    private static $links = [
        'recipients'   => 'mails'
    ];

    /**
     * Some links could lead to confusion, because they would not return
     * a proper resource: let's blacklist them
     *
     * Untested resources are also here
     *
     * @var array
     */
    private static $blacklistLinks = [
        'preview.html',
        'preview.txt',
        'archive_url',
        'spam_details',
        'opt_outs'
    ];

    /**
     * Create a new entity
     *
     * @param GenericResource $resource caller resource
     * @param stdClass $data entity data
     *
     * @return Crunchmail\Entity\GenericEntity
     */
    public function __construct(GenericResource $resource, $data)
    {
        $this->_resource = $resource;
        $this->_body     = $data;
    }

    /**
     * Return Entity body
     *
     * @return stdClass
     */
    public function getBody()
    {
        $copy = clone $this->_body;
        unset($copy->_links);
        return $copy;
    }

    /**
     * Catch get, post, put… methods
     *
     * @param string $name method name
     * @param array $args arguments
     *
     * @return Crunchmail\Entity\GenericEntity
     */
    public function __call($method, $args)
    {
        return $this->_resource->callRequest($method, $args, $this->url);
    }

    /**
     * Access entity or resources with object properties
     *
     * Note that this technic could lead to conflict if a resource and a body
     * field have the same name
     *
     * Ex:
     * echo $message->title
     * $arr = $message->recipients->current();
     *
     * @param string $name resource name
     *
     * @return mixed resource
     */
    public function __get($name)
    {
        // forbidden resource
        if (in_array($name, self::$blacklistLinks))
        {
            throw new \RuntimeException('Direct access to ' . $name . ' is
                prohibited');
        }

        // a subresource was found, create and return it
        if ($url = $this->getLink($name))
        {
            // save it, no need to create a new one each time
            $this->$name = $this->_resource->client->createResource($name, $url);
            return $this->$name;
        }

        // shortcut to body fields, when no resource was found
        if (is_object($this->_body) && property_exists($this->_body, $name))
        {
            return $this->_body->$name;
        }

        throw new \RuntimeException('Entity has no resource "' . $name . '"');
    }

    /**
     * Get the content of the links attribute, mapping the name first
     *
     * @param string $name name of the field
     *
     * @return string
     */
    public function getLink($name)
    {
        // access to collections
        $map = isset(self::$links[$name]) ? self::$links[$name] : $name;

        return isset($this->_body->_links->$map) ?
            $this->_body->_links->$map->href : false;
    }
}
<commit_msg>junk: Add debug help for travis<commit_after><?php
/**
 * Generic entity
 *
 * @author    <NAME> <<EMAIL>>
 * @copyright 2015 (c) Oasiswork
 * @license   https://opensource.org/licenses/MIT MIT
 */
namespace Crunchmail\Entities;

use Crunchmail\Client;
use Crunchmail\Resources\GenericResource;

/**
 * Generic entity class
 */
class GenericEntity
{
    /**
     * Caller resource
     *
     * @var object
     */
    protected $_resource;

    /**
     * Entity body
     *
     * @var stdClass
     */
    protected $_body;

    /**
     * Links remapping
     *
     * @var array
     */
    private static $links = [
        'recipients'   => 'mails'
    ];

    /**
     * Some links could lead to confusion, because they would not return
     * a proper resource: let's blacklist them
     *
     * Untested resources are also here
     *
     * @var array
     */
    private static $blacklistLinks = [
        'preview.html',
        'preview.txt',
        'archive_url',
        'spam_details',
        'opt_outs'
    ];

    /**
     * Create a new entity
     *
     * @param GenericResource $resource caller resource
     * @param stdClass $data entity data
     *
     * @return Crunchmail\Entity\GenericEntity
     */
    public function __construct(GenericResource $resource, $data)
    {
        $this->_resource = $resource;
        $this->_body     = $data;
    }

    /**
     * Return Entity body
     *
     * @return stdClass
     */
    public function getBody()
    {
        $copy = clone $this->_body;
        unset($copy->_links);
        return $copy;
    }

    /**
     * Catch get, post, put… methods
     *
     * @param string $name method name
     * @param array $args arguments
     *
     * @return Crunchmail\Entity\GenericEntity
     */
    public function __call($method, $args)
    {
        return $this->_resource->callRequest($method, $args, $this->url);
    }

    /**
     * Access entity or resources with object properties
     *
     * Note that this technic could lead to conflict if a resource and a body
     * field have the same name
     *
     * Ex:
     * echo $message->title
     * $arr = $message->recipients->current();
     *
     * @param string $name resource name
     *
     * @return mixed resource
     */
    public function __get($name)
    {
        // forbidden resource
        if (in_array($name, self::$blacklistLinks))
        {
            throw new \RuntimeException('Direct access to ' . $name . ' is
                prohibited');
        }

        // a subresource was found, create and return it
        if ($url = $this->getLink($name))
        {
            // save it, no need to create a new one each time
            $this->$name = $this->_resource->client->createResource($name, $url);
            return $this->$name;
        }

        // shortcut to body fields, when no resource was found
        if (is_object($this->_body) && property_exists($this->_body, $name))
        {
            return $this->_body->$name;
        }

        var_dump($this);
        throw new \RuntimeException('Entity has no resource "' . $name . '"');
    }

    /**
     * Get the content of the links attribute, mapping the name first
     *
     * @param string $name name of the field
     *
     * @return string
     */
    public function getLink($name)
    {
        // access to collections
        $map = isset(self::$links[$name]) ? self::$links[$name] : $name;

        return isset($this->_body->_links->$map) ?
            $this->_body->_links->$map->href : false;
    }
}

{'fraction_non_alphanumeric': 0.12537102473498232, 'fraction_numerical': 0.0011307420494699647, 'mean_word_length': 1.8636179684338325, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 65, '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': '7034242', 'n_tokens_mistral': 2053, 'n_tokens_neox': 2020, 'n_words': 562}