<commit_before><?php
/**
 * Created by PhpStorm.
 * User: arthur
 * Date: 19.12.16
 * Time: 20:14
 */

namespace rjapi\transformers;

use League\Fractal\TransformerAbstract;
use rjapi\blocks\DefaultInterface;
use rjapi\exception\ModelException;
use rjapi\extension\BaseFormRequest;
use rjapi\extension\BaseModel;

class DefaultTransformer extends TransformerAbstract
{
    const INCLUDE_PREFIX = 'include';

    private $middleWare = null;

    public function __construct(BaseFormRequest $middleWare)
    {
        $this->middleWare = $middleWare;
        $this->setAvailableIncludes($middleWare->relations());
    }

    public function transform(BaseModel $object)
    {
        $props = get_object_vars($this->middleWare);
        $arr = [];
        try {
            foreach ($props as $prop => $value) {
                $arr[$prop] = $object->$prop;
            }
        } catch (ModelException $e) {
            $e->getTraceAsString();
        }

        return $arr;
    }

    public function __call($name, $arguments)
    {
        // getting entity relation name, ex.: includeAuthor - author
        $entityName = str_replace(self::INCLUDE_PREFIX, '', $name);
        $entityMiddleWare = $entityName . DefaultInterface::MIDDLEWARE_POSTFIX;
        $entityNameLow = strtolower($entityName);
        // getting object, ex.: Book
        $obj = $arguments[0];
        $entity = $obj->$entityNameLow;
        return $this->item($entity, new DefaultTransformer($this->middleWare), $entityNameLow);
    }
}<commit_msg>	*	#20. middleware<commit_after><?php
namespace rjapi\transformers;

use League\Fractal\TransformerAbstract;
use rjapi\blocks\DefaultInterface;
use rjapi\blocks\DirsInterface;
use rjapi\blocks\PhpEntitiesInterface;
use rjapi\exception\ModelException;
use rjapi\extension\BaseFormRequest;
use rjapi\extension\BaseModel;

class DefaultTransformer extends TransformerAbstract
{
    const INCLUDE_PREFIX = 'include';

    private $middleWare = null;

    public function __construct(BaseFormRequest $middleWare)
    {
        $this->middleWare = $middleWare;
        $this->setAvailableIncludes($middleWare->relations());
    }

    public function transform(BaseModel $object)
    {
        $props = get_object_vars($this->middleWare);
        $arr = [];
        try {
            foreach ($props as $prop => $value) {
                $arr[$prop] = $object->$prop;
            }
        } catch (ModelException $e) {
            $e->getTraceAsString();
        }

        return $arr;
    }

    public function __call($name, $arguments)
    {
        // getting entity relation name, ex.: includeAuthor - author
        $entityName = str_replace(self::INCLUDE_PREFIX, '', $name);

        $middlewareEntity = DirsInterface::MODULES_DIR . PhpEntitiesInterface::BACKSLASH . Config::getModuleName() .
            PhpEntitiesInterface::BACKSLASH . DirsInterface::HTTP_DIR .
            PhpEntitiesInterface::BACKSLASH .
            DirsInterface::MIDDLEWARE_DIR . PhpEntitiesInterface::BACKSLASH .
            $entityName .
            DefaultInterface::MIDDLEWARE_POSTFIX;
        $middleWare = new $middlewareEntity();
        $entityNameLow = strtolower($entityName);
        // getting object, ex.: Book
        $obj = $arguments[0];
        $entity = $obj->$entityNameLow;
        return $this->item($entity, new DefaultTransformer($middleWare), $entityNameLow);
    }
}
{'fraction_non_alphanumeric': 0.11995284409077513, 'fraction_numerical': 0.00412614205717654, 'mean_word_length': 2.8524404086265607, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 22, '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': '3298931', 'n_tokens_mistral': 1021, 'n_tokens_neox': 995, 'n_words': 192}