Creates an entity and adds it.

@param string       $entityClass
@param array|string $values
@param null|string  $key

@return \Core\Entity\EntityInterface
@since 0.29
351812
public function createAttachedEntity($entityClass, $values = [], $key = null)
    {
        $this->job->createAttachedEntity($entityClass, $values, $key);
        return $this;
    }
1212351
public function register($entity, $entityMap = null)
    {
        // If an object is provider, get the class name from it
        if (!is_string($entity)) {
            $entity = get_class($entity);
        }

        if ($this->isRegisteredEntity($entity)) {
            throw new MappingException("Entity $entity is already registered.");
        }

        if (!class_exists($entity)) {
            throw new MappingException("Class $entity does not exists");
        }

        if ($entityMap === null) {
            $entityMap = $this->getEntityMapInstanceFor($entity);
        }

        if (is_string($entityMap)) {
            $entityMap = new $entityMap();
        }

        if (!$entityMap instanceof EntityMap) {
            throw new MappingException(get_class($entityMap).' must be an instance of EntityMap.');
        }

        $entityMap->setClass($entity);

        $this->entityClasses[$entity] = $entityMap;
    }
966807
public function setAll(array $values): void {
		$skipped = array();
		
		foreach ($values as $key => $value) {
			try {
				$this->set($key, $value);
			} catch (EntityException $e) {
				$skipped[$key] = $value;
			}
		}
		
		if (($skippedCount = sizeof($skipped)) > 0) {
			$message = sprintf("Unknown %s (%s) in %s.",
				$skippedCount === 1 ? "property" : "properties",
				join(", ", $skipped),
				get_class()
			);
			
			throw new EntityException($message, EntityException::UNKNOWN_PROPERTY);
		}
	}
908825
public function set(string $key, $value): void {
		if (!property_exists($this, $key)) {
			throw new EntityException(
				sprintf("Unknown property (%s) in %s.", $key, get_class()),
				EntityException::UNKNOWN_PROPERTY
			);
		}
		
		$this->{$key} = $value;
	}
707780
private function buildEntityClass()
    {
        foreach ($this->registries as $registry) {
            if ($registry->getKey() === 'identifier') {
                $this->entity->setObjectIdentifier($registry->getValue());
            } else {
                $this->entity->setObjectProperty($registry->getKey(), $registry->getValue());
            }
        }
    }
274504
public function createEntityEvent($event, $entityType, $entityId, array $properties = [], DateTime $eventTime = null)
    {
        $event = new EntityEvent($event, $entityType, $entityId);
        $event->setProperties($properties);
        $event->setEventTime($eventTime);
        $response = $this->createEvent($event->toArray());

        return $response;
    }
971737
public static function create($data = [], Entity $entity = null)
    {
        $record = new static($data, $entity);

        $record->save();

        return $record;
    }
646839
public function createBatchDeleteForm($entityClass, array $entities = null)
    {
        if (null !== $entities && empty($entities)) {
            throw new FormException(
                sprintf('Unable to create batch delete form for entity class "%s": entity array is empty.', $entityClass)
            );
        }

        $options = [
            'entity_class' => $entityClass,
        ];

        if (!empty($entities)) {
            $options = array_merge($options, [
                'action'   => $this->adminRouter->generate(reset($entities), $entityClass, AdminRouter::TYPE_BATCH_DELETE),
                'entities' => $entities,
            ]);
        }

        return $this->genericFormFactory->create(BatchDeleteType::class, null, $options);
    }
1032529
public function createFromArray($class, array $attributes)
    {
        $entity = new $class();
        foreach ($attributes as $name => $value) {
            if (property_exists($entity, $name)) {
                $methodName = $this->getSetterName($entity, $name);
                if ($methodName) {
                    $entity->{$methodName}($value);
                } else {
                    throw new InvalidPropertyException('Add/Setter method for property '.$name.' not found');
                }
            }
        }

        return $entity;
    }
1209864
public function setEntityType(string $entityType): void {
		if (!class_exists($entityType)) {
			throw new EntityFactoryException("Unknown entity: $entityType.", EntityFactoryException::UNKNOWN_ENTITY);
		}
		
		// now that we know a class with the name of entityType exists, we need
		// to see if it is an implementation of our EntityInterface object.  the
		// use of the Aura DI container to inject dependencies seems to prevent
		// the use of the PHP is_a() and is_subclass_of() functions.  instead,
		// we'll try to instantiate it like it's an EntityInterface and then we
		// can use the instanceof operator to be sure.
		
		$this->entityType = $entityType;
		$temp = $this->newEntity([]);
		
		if (!($temp instanceof EntityInterface)) {
			
			// if that didn't work, then not only do we want to throw an
			// exception, but we also want to be sure that we set the
			// entityType property back to null so that it doesn't accidentally
			// get used elsewhere after the exception is caught.
			
			$this->entityType = null;
			throw new EntityFactoryException("Not an entity: $entityType.", EntityFactoryException::NOT_AN_ENTITY);
		}
	}
1063036
public function createEntity($className, array $fields = [])
    {
        $metadata = $this->getEntityMetadata($className);

        if (is_null($metadata)) {
            return null;
        }

        $reflection = $metadata->getReflectionClass();

        try {
            $entity = $reflection->newInstance();
        } catch (\Exception $e) {
            $entity = $reflection->newInstanceWithoutConstructor();
        }

        foreach ($fields as $fieldName => $fieldValue) {
            if (self::IDENTITY === $fieldName) {
                $property = $reflection->getProperty($this->getEntityIdName($className));
            } else {
                $property = $reflection->getProperty($fieldName);
            }

            try {
                $property->setValue($entity, $fieldValue);
            } catch (\Exception $e) {
                $property->setAccessible(true);
                $property->setValue($entity, $fieldValue);
                $property->setAccessible(false);
            }
        }

        return $entity;
    }
623679
protected function createAndFill(array $attributes)
    {
        $entityClass = static::makes();
        $entity = new $entityClass;

        foreach($attributes as $attribute => $value) {
            $entity->$attribute = $value;
        }

        return $entity;
    }
796917
public function createDeleteForm($entity, $entityClass)
    {
        return $this->createIdForm($entity, 'delete_', $this->adminRouter->generate($entity, $entityClass, AdminRouter::TYPE_DELETE));
    }
1220839
public function createEntity(string $entity_id, array $parents)
    {
        $entity = new Entity($this, $entity_id, $parents);
        return $entity;
    }
1034907
private function getEntity($id, $entityClass = null)
    {
        if (empty($entityClass)) {
            $entityClass = $this->entityClass;
        }

        $entity = $this->getEntityManager()->find($entityClass, $id);

        if (empty($entity)) {
            throw $this->createNotFoundException(
                sprintf('Unable to find entity "%s" by ID "%d".', $entityClass, $id)
            );
        }

        return $entity;
    }
113686
public function entityCreate($entity_type, $values, $save = TRUE) {
    if (!is_array($values)) {
      // Cast an object to array to be compatible with nodeCreate().
      $values = (array) $values;
    }

    $entity = $this->getStubEntity($entity_type, $values);

    foreach ($values as $name => $value) {
      $definition = $entity->getFieldDefinition($name);
      $settings = $definition->getSettings();
      switch ($definition->getType()) {
        case 'entity_reference':
          if (in_array($settings['target_type'], ['node', 'taxonomy_term'])) {
            // @todo: only supports single values for the moment.
            $id = $this->getEntityIdByLabel($settings['target_type'], NULL, $value);
            $entity->{$name}->setValue($id);
          }
          break;

        case 'entity_reference_revisions':
          $entities = [];
          foreach ($value as $target_values) {
            Assert::keyExists($target_values, 'type', __METHOD__ . ": Required fields 'type' not found.");
            $entities[] = $this->entityCreate($settings['target_type'], $target_values, FALSE);
          }

          $entity->{$name}->setValue($entities);
          break;

        case 'image':
          $entity->{$name}->setValue(['target_id' => $this->saveFile($value)->id()]);
          break;
      }
    }

    if ($save) {
      $entity->save();
    }

    return $entity;
  }
15943
public static function createFromArray(array $tree, EntityInterface $parent = null)
    {
        $childrenRelationIndex = with(new static)->getChildrenRelationIndex();
        $entities = [];

        foreach ($tree as $item) {
            $children = array_pull($item, $childrenRelationIndex);

            /**
             * @var Entity $entity
             */
            $entity = new static($item);
            $entity->parent_id = $parent ? $parent->getKey() : null;
            $entity->save();

            if (!is_null($children)) {
                $children = static::createFromArray($children, $entity);
                $entity->setRelation($childrenRelationIndex, $children);
                $entity->addChildren($children->all());
            }

            $entities[] = $entity;
        }

        return new Collection($entities);
    }