Return a new second entity

@param array   $data              Data array that represents entity's attributes
@param string  $secondEntityName  Name of the entity

@return EntityInterface|null
971871
private function createAndReturnNewSecondEntity($data, $secondEntityName)
    {
        /** @var EntityInterface $entity */
        $entity = $this->getSecondEntityByName($secondEntityName);
        if (is_array($data)) {
            return $this->mapAttributesFromFirstEntityToSecondEntityAndReturnEntity($data, $entity);
        }

        return null;
    }
660470
public function denormalize($data, $entityClass, $factory = null, $toEntity = null)
    {
        $context = $this->getDenormalizeContext($this->denormalizeContext, $toEntity);

        if ($factory) {
            $entityClass = $entityClass::$factory($data);
        }

        $entity = $this->serializer->denormalize($data, $entityClass, null, $context);

        // TODO: Ameliorer ce mechanisme
        EntityNormalizer::$isFirstCall = true;

        return $entity;
    }
1244639
public static function getActivityRelatedEntity($data) {
    $related_object = $data['related_object'][0];

    // We return parent comment as related object as comment
    // for create_comment_reply messages.
    if ($data['message_template'] === 'create_comment_reply') {
      $comment_storage = \Drupal::entityTypeManager()->getStorage('comment');
      // @TODO: Check if comment published?
      $comment = $comment_storage->load($related_object['target_id']);
      $parent_comment = $comment->getParentComment();
      if (!empty($parent_comment)) {
        $related_object = [
          'target_type' => $parent_comment->getEntityTypeId(),
          'target_id' => $parent_comment->id(),
        ];
      }
    }
    // We return commented entity as related object for all other comments.
    elseif (isset($related_object['target_type']) && $related_object['target_type'] === 'comment') {
      $comment_storage = \Drupal::entityTypeManager()->getStorage('comment');
      // @todo: Check if comment published?
      $comment = $comment_storage->load($related_object['target_id']);
      if ($comment) {
        $commented_entity = $comment->getCommentedEntity();
        if (!empty($commented_entity)) {
          $related_object = [
            'target_type' => $commented_entity->getEntityTypeId(),
            'target_id' => $commented_entity->id(),
          ];
        }
      }
    }
    // We return Event as related object for all Event Enrollments.
    elseif (isset($related_object['target_type']) && $related_object['target_type'] === 'event_enrollment') {
      $entity_storage = \Drupal::entityTypeManager()
        ->getStorage($related_object['target_type']);
      $entity = $entity_storage->load($related_object['target_id']);

      /** @var \Drupal\social_event\Entity\EventEnrollment $entity */
      $event_id = $entity->getFieldValue('field_event', 'target_id');
      if (!empty($event_id)) {
        $related_object = [
          'target_type' => 'node',
          'target_id' => $event_id,
        ];
      }
    }
    return $related_object;
  }
252556
public function make( array $data = null )
    {

        if( !$this->makeableSafetyCheck() ) {
            return null;
        }

        $entity = $this->entityFactory();
        if( $data !== null ) {
            $entity->set(
                $data,
                // as data is a array the second argument doesn't make much sense here
                null,
                // set the inital values and see that the correct input validation happens
                $entity->inputValidate
             );
        }
        return $entity;

    }
149787
public function toEntity(): EntityInterface
    {
        $data = [];

        $entityProvider = $this->getEntityProvider();
        foreach (get_object_vars($this) as $property => $value) {
            $method = Inflector::variable('get ' . $property);

            if (method_exists($this, $method) && is_callable([$this, $method])) {
                $field = Inflector::underscore($property);
                $data[$field] = $this->$method();
            }
        }

        $entity = new $entityProvider($data);

        return $entity;
    }
946302
public function create($data)
	{
		// Process data given.
		$processPayload = $this->process(__FUNCTION__, $data);

		if( ! $processPayload->isStatus(Payload::STATUS_VALID))
		{
			return $processPayload;
		}

		// Get a properly formatted Entity Class string.
		$entityContext = $this->formatEntityType($processPayload->getData()['entity_type']);

		// Create the entity with the given data.
		$entityPayload = $this->aggregate[$entityContext]->create($processPayload->getData());

		// No need to procede if dynamic content was not created.
		if($entityPayload->getStatus() != 'created')
		{
			return $entityPayload;
		}

		// Create the newly created entity's Identifier.
		$identifierPayload = $this->createIdentifier($entityPayload->getData());

		if($identifierPayload->getStatus() != 'created')
		{
			return $identifierPayload;
		}

		// @todo: The following currently adds an additional query just for the ease of getting the full entity to return.
		$createdEntityPayload = $this->getOneByUuid($entityPayload->getData()->getUuid());

		if($createdEntityPayload->getStatus() !== 'found')
		{
			return $createdEntityPayload;
		}

		return new Payload($createdEntityPayload->getData(), $entityPayload->getStatus());
	}
979131
public function provide($data = null, array $options = [])
    {
        if (empty($options['entity'])) {
            return null;
        }

        $table = $this->config->getTable();

        // return null in cases where no table or a dummy table was provided by the config class
        try {
            $primaryKey = $table->getPrimaryKey();
        } catch (Exception $e) {
            return null;
        }

        if (! is_string($primaryKey)) {
            throw new UnsupportedPrimaryKeyException();
        }

        if ($options['entity'] instanceof EntityInterface) {
            return $options['entity']->get($primaryKey);
        }

        if ($options['entity'] instanceof ServerRequest) {
            return $options['entity']->getData($primaryKey);
        }

        return null;
    }
162154
public function denormalise(array $data, array $options = [])
    {
        if (empty($data)) {
            return $data;
        }

        if (!isset($options["entityClass"])) {
            throw new NormalisationException("Cannot denormalise data without knowing the main entity class");
        }
        if (!isset($options["metadataProvider"])) {
            throw new NormalisationException("Cannot denormalise data without a metadata provider");
        }

        /** @var EntityMetadataProviderInterface $metadataProvider */
        $this->metadataProvider = $options["metadataProvider"];
        $metadata = $this->metadataProvider->getEntityMetadata($options["entityClass"]);

        // split fields based on prefix
        $prefixedFields = [];
        foreach ($data[0] as $field => $value) {
            $fieldParts = explode("__", $field);
            // if this field has no prefix, set it to an empty string
            if (count($fieldParts) == 1) {
                array_unshift($fieldParts, "");
            }
            // add the field to the prefix's array
            $prefix = $fieldParts[0];
            if (empty($prefixedFields[$prefix])) {
                $prefixedFields[$prefix] = [];
            }
            $prefixedFields[$prefix][$fieldParts[1]] = $field;
        }

        // handle case where only 1 prefix is present
        if (count($prefixedFields) == 1) {
            $fields = array_pop($prefixedFields);
            $primaryKey = $metadata->getPrimaryKey();
            if (isset($fields[$primaryKey])) {
                $this->primaryKeyFields = [$fields[$primaryKey] => true];
            }
            return $this->denormaliseData($data, $fields);
        }

        // complex result set
        if (!isset($options["entityMap"])) {
            throw new NormalisationException("Cannot denormalise data without an entity map");
        }

        $entityMap = $options["entityMap"];

        $collection = $metadata->getCollection();

        if (empty($prefixedFields[$collection])) {
            throw new NormalisationException("The collection '$collection' was not found in the fields array for this record set: '" . implode("', '", array_keys($prefixedFields)) . "'");
        }

        // reset primary key fields
        $this->primaryKeyFields = [];
        $this->treedEntities = [];
        $this->childRowAliases = [];

        $this->createFieldTree($prefixedFields, $entityMap, $collection, $options["entityClass"]);

        reset($data);
        $result = $this->denormaliseData($data, $prefixedFields[$collection], $collection);

        return  $result;
    }
555130
public function triggerAfterSelect($data, EntityCollection $entities)
    {
        $event = new \Slick\Orm\Event\Select(
            null,
            [
                'data' => $data,
                'entityCollection' => $entities
            ]
        );
        $event->setAction(\Slick\Orm\Event\Select::ACTION_AFTER_SELECT);
        return Orm::getEmitter($this->getEntityClassName())
            ->emit($event);
    }
469995
public function getNew(): AbstractEntity
    {
        $newEntity = $this->getRepository()->getNew();

        $this->prepareEntityForNew($newEntity);

        return $newEntity;
    }
927969
public function dataToEntity(array $data)
    {
        $cartItem = new \ShopgateExternalCoupon();
        foreach ($data as $key => $value) {
            $method = 'set' . SimpleDataObjectConverter::snakeCaseToUpperCamelCase($key);
            if (method_exists($cartItem, $method)) {
                $cartItem->{$method}($value);
            }
        }

        return $cartItem;
    }
832460
protected function _createEntity(array $data)
    {
        //create an instance with its unique keys first
        $keys = array();
        $description = $this->_description;

        $keys = $description->getIdentifyingValues($data);

        if (empty($keys)) {
            throw new AnDomainRepositoryException('Trying to create an entity witout any identiftying keys');
        }

        //if an entity found with them same key already
        //instantiated then return that entity to avoid having
        //duplicate objects
        $entity = $this->find($keys, false);

        if ($entity) {
            return $entity;
        }

        $inheritance_column = $description->getInheritanceColumn();

        if ($inheritance_column &&
                isset($data[$inheritance_column->key()])) {
            $identifier = $data[$inheritance_column->key()];
            $identifier = substr($identifier, strrpos($identifier, ',') + 1);
        } else {
            $identifier = $this->_prototype->getIdentifier();
        }

        $entity = $this->_instantiateEntity($identifier, $data);

        //insert the identity
        $this->_space->insertEntity($entity, $keys);

        $context = new AnCommandContext();
        $context->data = $data;
        $context->keys = $keys;

        $entity->execute('after.fetch', $context); //call after fetch

        $entity->reset(); //reset the entity
        return $entity;
    }
971737
public static function create($data = [], Entity $entity = null)
    {
        $record = new static($data, $entity);

        $record->save();

        return $record;
    }
724416
public function create($data) {
		$dt = $this->getDataTable();
		
		if (! $dt)
			return $this->createResponse($this->getNameManager()->getTableNames(), 401, "Tabels");
		
		return $this->createResponse([], 200, "{$this->getNameManager()->entityName}: No Create");
	}
858788
private static function mapDataToEntity($entityName, \stdClass $data)
    {
        $entity = new $entityName();

        if (!($entity instanceof Entity)) {
            throw new \RuntimeException(
                sprintf(
                    'Object "%s" must extend Entity',
                    $entityName
                )
            );
        }

        $foreignKeyEntityMap = $entity->foreignKeyEntityMap();

        $properties = array_keys(get_object_vars($data));

        foreach ($properties as $propertyName) {
            if (!property_exists($entity, $propertyName)) {
                throw new \UnexpectedValueException(
                    sprintf(
                        'Property %s->%s does not exist',
                        get_class($entity),
                        $propertyName
                    )
                );
            }

            if (isset($foreignKeyEntityMap[$propertyName])) {
                $entity->$propertyName = self::mapDataToEntity(
                    $foreignKeyEntityMap[$propertyName],
                    $data->$propertyName
                );
            } elseif (is_string($data->$propertyName) && preg_match('/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}/', $data->$propertyName)) {
                $entity->$propertyName = new \DateTime($data->$propertyName);
            } else {
                $entity->$propertyName = json_decode(json_encode($data->$propertyName), true);
            }
        }

        return $entity;
    }
1148948
protected static function buildEntity($data)
    {
        $entities = static::getDefinedEntitiesArray();

        if (isset($data['entity']))
        {
            if (in_array($data['entity'], $entities))
            {
                $class = static::getEntityClass($data['entity']);
                $entity = new $class;
            }
            else
            {
                $entity = new static;
            }
        }
        else
        {
            $entity = new static;
        }

        $entity->fill($data);

        return $entity;
    }
1220690
public function createNew($data)
    {
        if ($data instanceof File) {
            /** @var $data File */
            $media = new Media();
            if (method_exists($data, 'getClientOriginalName')) {
                $media->setOriginalFilename($data->getClientOriginalName());
            } else {
                $media->setOriginalFilename($data->getFilename());
            }
            $media->setContent($data);

            $contentType = $this->mimeTypeGuesser->guess($media->getContent()->getPathname());
            $media->setContentType($contentType);

            return $media;
        }

        return null;
    }