Create resource

@param  mixed            $source
@param  DocumentHydrator $hydrator
@return ResourceIdentifierObject
977976
protected function createResourceIdentifier($source, DocumentHydrator $hydrator): ResourceIdentifierObject
    {
        if (! isset($source->id)) {
            throw new InvalidDocumentException('Resource identifier contains no ID');
        }

        if (! isset($source->type)) {
            throw new InvalidDocumentException('Resource identifier contains no type');
        }

        $identifier = new ResourceIdentifierObject($source->id, $source->type);

        $hydrator->hydrateObject($identifier, $source);

        return $identifier;
    }
1359165
private function generateHydratorClass(ClassMetadata $class, $hydratorClassName, $fileName)
    {
        $code           = '';
        $inverseRefOnes = [];

        foreach ($class->propertyMetadata as $attributeName => $mapping) {
            if (isset($mapping->alsoLoadAttributes)) {
                foreach ($mapping->alsoLoadAttributes as $name) {
                    $code .= sprintf(<<<EOF

        /** @AlsoLoad("$name") */
        if (!array_key_exists('%1\$s', \$data) && array_key_exists('$name', \$data)) {
            \$data['%1\$s'] = \$data['$name'];
        }

EOF
                        ,
                        $mapping->attributeName
                    );
                }
            }

            if (!isset($mapping->association)) {
                $clone = $mapping->type === 'date' || $mapping->type === 'time' || $mapping->type === 'datetime' ? 'clone' : '';
                $code  .= sprintf(<<<EOF

        /** @Attribute(type="{$mapping->type}") */
        if (isset(\$data['%1\$s'])) {
            \$mapping = \$this->class->getPropertyMetadata('%2\$s');
            \$value = \$data['%1\$s'];
            if (\$mapping->notSaved) {
                \$return = Type::convertUnmappedToPHPValue(\$value);
            } else {
                \$type = \$mapping->getType();
                \$return = \$type->convertToPHPValue(\$value);
            }
            \$mapping->setValue(\$document, $clone \$return);
            \$hydratedData['%2\$s'] = \$return;
        }

EOF
                    ,
                    $mapping->attributeName,
                    $mapping->name
                );
            } elseif ($mapping->association === PropertyMetadata::ASSOCIATION_TYPE_REFERENCE_ONE && $mapping->isOwningSide) {
                $code .= sprintf(<<<EOF

        /** @ReferenceOne */
        if (isset(\$data['%1\$s'])) {
            \$mapping = \$this->class->getPropertyMetadata('%2\$s');
            \$identifier = \$this->unitOfWork->getAssociationPersister()->loadReference(\$mapping, \$data['%1\$s']);
            \$return = \$identifier->getDocument(\$this->dm);
            \$mapping->setValue(\$document, \$return);
            \$hydratedData['%2\$s'] = \$return;
        }

EOF
                    ,
                    $mapping->attributeName,
                    $mapping->name
                );
            } elseif ($mapping->association === PropertyMetadata::ASSOCIATION_TYPE_REFERENCE_ONE && $mapping->isInverseSide) {
                if (isset($mapping->repositoryMethod) && $mapping->repositoryMethod) {
                    $code .= sprintf(<<<EOF

        /** @ReferenceOne */
        \$mapping = \$this->class->getPropertyMetadata('%2\$s');
        \$className = \$mapping->targetDocument;
        \$return = \$this->dm->getRepository(\$className)->%3\$s(\$document);
        \$mapping->setValue(\$document, \$return);
        \$hydratedData['%2\$s'] = \$return;

EOF
                        ,
                        $mapping->attributeName,
                        $mapping->name,
                        $mapping->repositoryMethod
                    );
                } else {
                    //handled last, see below
                    $inverseRefOnes[] = $mapping;
                }
            } elseif ($mapping->association === PropertyMetadata::ASSOCIATION_TYPE_REFERENCE_MANY || $mapping->association === PropertyMetadata::ASSOCIATION_TYPE_EMBED_MANY) {
                $code .= sprintf(<<<EOF

        /** @Many */
        \$mapping = \$this->class->getPropertyMetadata('%2\$s');
        \$collectionData = isset(\$data['%1\$s']) ? \$data['%1\$s'] : null;
        \$return = \$this->dm->getConfiguration()->getPersistentCollectionFactory()->create(\$this->dm, \$mapping);
        \$return->setOwner(\$document, \$mapping);
        \$return->setInitialized(false);
        \$return->setReadOnly(\$readOnly);
        if (\$collectionData) {
            \$type = \$mapping->getType();
            \$collectionData = \$type->convertToPHPValue(\$collectionData);
            \$return->setRawData(\$collectionData);
        }
        \$mapping->setValue(\$document, \$return);
        \$hydratedData['%2\$s'] = \$return;

EOF
                    ,
                    $mapping->attributeName,
                    $mapping->name
                );
            } elseif ($mapping->association === PropertyMetadata::ASSOCIATION_TYPE_EMBED_ONE) {
                $code .= sprintf(<<<EOF

        /** @EmbedOne */
        if (isset(\$data['%1\$s'])) {            
            \$mapping = \$this->class->getPropertyMetadata('%2\$s');
            \$return = \$this->unitOfWork->getAssociationPersister()->loadEmbed(\$mapping, \$data['%1\$s'], \$document, null, \$readOnly);
            \$mapping->setValue(\$document, \$return);
            \$hydratedData['%2\$s'] = \$return;
        }

EOF
                    ,
                    $mapping->attributeName,
                    $mapping->name
                );
            }
        }

        //processed last to ensure the identifier value(s) are hydrated first
        foreach ($inverseRefOnes as $mapping) {
            $code .= sprintf(<<<EOF

        /** @ReferenceOne */
        \$mapping = \$this->class->getPropertyMetadata('%2\$s');
        \$className = \$mapping->targetDocument;
        \$query = \$this->unitOfWork->getDocumentPersister(\$className)->createReferenceInverseSideQuery(\$mapping, \$document);
        \$return = \$query->execute()->current();
        \$mapping->setValue(\$document, \$return);
        \$hydratedData['%2\$s'] = \$return;

EOF
                    ,
                    $mapping->attributeName,
                    $mapping->name
                );
        }

        $namespace = $this->hydratorNamespace;
        $code      = sprintf(<<<EOF
<?php

namespace $namespace;

use Cloudtek\DynamoDM\DocumentManager;
use Cloudtek\DynamoDM\Mapping\ClassMetadata;
use Cloudtek\DynamoDM\Hydrator\HydratorInterface;
use Cloudtek\DynamoDM\UnitOfWork;
use Cloudtek\DynamoDM\Type\Type;

/**
 * THIS CLASS WAS GENERATED BY DYNAMODM. DO NOT EDIT THIS FILE.
 */
class $hydratorClassName implements HydratorInterface
{
    private \$dm;
    private \$unitOfWork;
    private \$class;

    public function __construct(DocumentManager \$dm, UnitOfWork \$uow, ClassMetadata \$class)
    {
        \$this->dm = \$dm;
        \$this->unitOfWork = \$uow;
        \$this->class = \$class;
    }

    public function hydrate(\$document, \$data, \$readOnly = false)
    {
        \$hydratedData = array();
%s
        return \$hydratedData;
    }
}
EOF
            ,
            $code
        );

        if ($fileName === false) {
            if (!class_exists($namespace . '\\' . $hydratorClassName)) {
                eval(substr($code, 5));
            }
        } else {
            $parentDirectory = dirname($fileName);

            if (!is_dir($parentDirectory) && (false === @mkdir($parentDirectory, 0775, true))) {
                throw HydratorException::hydratorDirectoryNotWritable();
            }

            if (!is_writable($parentDirectory)) {
                throw HydratorException::hydratorDirectoryNotWritable();
            }

            $tmpFileName = $fileName . '.' . uniqid('', true);
            file_put_contents($tmpFileName, $code);
            rename($tmpFileName, $fileName);
        }
    }
704349
public static function create(
        UUID $id,
        MIMEType $mimeType,
        StringLiteral $description,
        StringLiteral $copyrightHolder,
        Url $sourceLocation,
        Language $language
    ) {
        $mediaObject = new self();
        $mediaObject->apply(
            new MediaObjectCreated(
                $id,
                $mimeType,
                $description,
                $copyrightHolder,
                $sourceLocation,
                $language
            )
        );

        return $mediaObject;
    }
83956
protected function processNoDataRelationship($source, DocumentHydrator $hydrator): NoDataRelationship
    {
        $relationship = new NoDataRelationship();

        $hydrator->hydrateObject($relationship, $source);

        return $relationship;
    }
318157
protected function createResourceRepository(
        $class,
        DocumentManager $documentManager,
        ClassMetadata $metadata,
        ResourceInterface $resource = null
    ) {
        if ($resource !== null && is_a($class, BaseRepositoryInterface::class, true)) {
            return new $class($documentManager, $documentManager->getUnitOfWork(), $metadata, $resource);
        }

        return parent::createRepository($documentManager, $metadata->getName());
    }
813628
public function source(string $class): DocumentSource
    {
        $source = $this->define($class, self::D_SOURCE_CLASS);

        if (empty($source)) {
            //Let's use default source
            $source = DocumentSource::class;
        }

        $handles = $source::DOCUMENT;
        if (empty($handles)) {
            //All sources are linked to primary class (i.e. Admin source => User class), unless specified
            //in source directly
            $handles = $class;
        }

        return $this->getFactory()->make($source, [
            'class' => $handles,
            'odm'   => $this
        ]);
    }
672120
public function getHydratorFor(string $className) : HydratorInterface
    {
        if (isset($this->hydrators[$className])) {
            return $this->hydrators[$className];
        }
        $hydratorClassName = str_replace('\\', '', $className) . 'Hydrator';
        $fqn               = $this->hydratorNamespace . '\\' . $hydratorClassName;
        $class             = $this->dm->getClassMetadata($className);

        if (! class_exists($fqn, false)) {
            $fileName = $this->hydratorDir . DIRECTORY_SEPARATOR . $hydratorClassName . '.php';
            switch ($this->autoGenerate) {
                case Configuration::AUTOGENERATE_NEVER:
                    require $fileName;
                    break;

                case Configuration::AUTOGENERATE_ALWAYS:
                    $this->generateHydratorClass($class, $hydratorClassName, $fileName);
                    require $fileName;
                    break;

                case Configuration::AUTOGENERATE_FILE_NOT_EXISTS:
                    if (! file_exists($fileName)) {
                        $this->generateHydratorClass($class, $hydratorClassName, $fileName);
                    }
                    require $fileName;
                    break;

                case Configuration::AUTOGENERATE_EVAL:
                    $this->generateHydratorClass($class, $hydratorClassName, null);
                    break;
            }
        }
        $this->hydrators[$className] = new $fqn($this->dm, $this->unitOfWork, $class);
        return $this->hydrators[$className];
    }
1200280
public function addSource(string $class, string $source): SchemaBuilder
    {
        if (!$this->hasSchema($class)) {
            throw new SchemaException("Unable to add source to '{$class}', class is unknown to ORM");
        }

        $this->sources[$class] = $source;

        return $this;
    }
229779
static public function createFromResource($resource) {
		if (!is_resource($resource))
			throw new Exception('Invalid (resource) parameter! This is not a resource!');
		if (get_resource_type($resource) != 'gd')
			throw new Exception('Invalid (resource) parameter! Resource is not a gd resource!');
		return new self($resource);
	}
716178
public function create($fileUri, $agendaId, $filename, $type = Document::DOCTYPE_PURCHASE, $fileSplit = false, $lacyLoad = false): Document
    {
        $file = new FileInfo($fileUri);
        $mimeType = $file->getMimeType();

        $documentFile = new DocumentFile();
        $documentFile->setType($type)
            ->setAgendaId($agendaId)
            ->setFileSplit($fileSplit)
            ->setFile(
                new UploadFile($file->getFilename(), $mimeType, file_get_contents($file->getPathname()))
            )
        ;

        return current($this->send([$documentFile], $lacyLoad));
    }
401585
private function hydrateCustomerWithSubEntities(
        HalResource $customerResource,
        CustomerRequest $customerRequest
    ): Customer {
        $customerLoader = new CustomerLoader(
            $this->restClient,
            $customerResource,
            $customerRequest->getLinks()
        );
        $customerLoader->load();

        return $customerResource->getEntity();
    }
695479
public function parse(Source $source, array $options = []): DocumentNode
    {
        $this->lexer = $this->createLexer($source, $options);

        return $this->lexDocument();
    }
6073
public function createService(ServiceLocatorInterface $serviceLocator)
    {
        $service = new Environment();

        /** @var Client $pmProxyClient */
        $pmProxyClient = $serviceLocator->get('KmbPmProxy\Client');
        $service->setPmProxyClient($pmProxyClient);

        /** @var HydratorInterface $environmentHydrator */
        $environmentHydrator = $serviceLocator->get('KmbPmProxy\Hydrator\EnvironmentHydrator');
        $service->setEnvironmentHydrator($environmentHydrator);

        return $service;
    }
550436
public function hydrateSubject(HydrationEvent $event)
    {
        $objectHydrator = new ObjectHydrator();

        return $objectHydrator->hydrate($event->getSubject(), $event->getData());
    }
841287
public static function createFromResource(Resource $resource, array $options = [])
    {
        $entity = new self();

        $entity->applyResource($resource);

        return $entity;
    }
293477
public static function create($product, $resource, ClientContext $context)
    {
        $rm = new ResourceMetadata($product, $resource);
        $resourceServiceClass = $rm->resourceServiceClass;
        return new $resourceServiceClass($rm, $context);
    }
672549
public function create(Resource\ResourceData $resourceData)
    {
        return $this->crudRepositoryAdapter->createResource($this->resourceType, $resourceData);
    }