Get the content of an attribute of an entity given.

@param BusinessPage $entity
@param string       $field

@return mixed
499841
protected function getEntityAttributeValue($entity, $field)
    {
        $functionName = 'get'.ucfirst($field);

        $fieldValue = call_user_func([$entity, $functionName]);

        return $fieldValue;
    }
1043335
public function getEntityAccessFields($entity, $bundle) {
    $fields = [];
    $field_definitions = $this->entityManager->getFieldDefinitions($entity, $bundle->id());
    foreach ($field_definitions as $field_name => $field_definition) {
      if (!empty($field_definition->getTargetBundle())) {
        if ($field_definition->getType() === 'entity_access_field') {
          $fields[$field_name] = $field_definition;
        }
      }
    }
    return $fields;
  }
1237064
protected function getFieldContent($entity, $fieldName, array $fieldAttr, array $mappings)
    {
        if (isset($fieldAttr['widget'])) {
            $widgetAlias = $fieldAttr['widget']['alias'];

            return $this->widgetPool->getWidget($widgetAlias)->getContent(
                $entity,
                $fieldAttr['widget']['options'],
                $fieldName
            );
        }
        if (isset($fieldAttr['service'])) {
            $method = $fieldAttr['service']['method'];

            return $this->container->get($fieldAttr['service']['id'])->$method($entity, $fieldAttr['service']['options']);
        }
        if (isset($fieldAttr['callback'])) {
            $class = $fieldAttr['callback']['class'];
            $method = $fieldAttr['callback']['method'];

            return $class::$method($entity, $fieldAttr['callback']['options']);
        }

        $content = $this->propertyAccessor->getValue($entity, $fieldName);

        return isset($mappings[$fieldName]['type'])
            ? $this->stringifier->stringify($content, $mappings[$fieldName]['type'])
            : $content;
    }
1157289
public function setBusinessPropertyInstance($string, BusinessProperty $businessProperty, $entity)
    {
        //test parameters
        if ($entity === null) {
            throw new \Exception('The parameter entity can not be null');
        }

        //the attribute to set
        $entityProperty = $businessProperty->getEntityProperty();

        //the string to replace
        $stringToReplace = '{{item.'.$entityProperty.'}}';

        //the value of the attribute
        $attributeValue = $entity->getEntityAttributeValue($entityProperty);

        //we provide a default value
        if ($attributeValue === null) {
            $attributeValue = '';
        }

        //we replace the string
        $string = str_replace($stringToReplace, $attributeValue, $string);

        return $string;
    }
646268
protected function generateFieldSizeStatement(Entity $entity, FieldDescriptorProto $field)
    {
        $generator = new SerializedSizeFieldStatementGenerator($this->context);
        $statement = $generator->generateFieldSizeStatement($entity, $field);

        return $statement;
    }
368348
public function generateFieldReadStatement(Entity $entity, FieldDescriptorProto $field)
    {
        $body      = [];
        $reference = null;
        $type      = $field->getType();
        $name      = $field->getName();
        $rule      = $field->getLabel();
        $tag       = $field->getNumber();
        $options   = $field->getOptions();
        $isPack    = $options ? $options->getPacked() : false;
        $variable  = $this->targetVar ?: '$this->' . $name;
        $breakSttm = $this->getBreakStatement($variable);

        if ($field->hasTypeName()) {
            $typeName   = $field->getTypeName();
            $typeEntity = $this->getEntity($typeName);
            $reference  = $typeEntity->getNamespacedName();
        }

        if ( ! $isPack) {
            $body[] = sprintf('\Protobuf\WireFormat::assertWireType($wire, %s);', $type->value());
            $body[] = null;
        }

        if ($rule === Label::LABEL_REPEATED() && $isPack) {
            $readSttm = ($type === Type::TYPE_ENUM())
                ? $reference . '::valueOf(' . $this->generateReadScalarStatement($type->value()) .')'
                : $this->generateReadScalarStatement($type->value());

            $body[] = '$innerSize  = $reader->readVarint($stream);';
            $body[] = '$innerLimit = $stream->tell() + $innerSize;';
            $body[] = null;
            $body[] = 'if (' . $variable . ' === null) {';
            $body[] = '    ' . $variable . ' = new ' . $this->getCollectionClassName($field) . '();';
            $body[] = '}';
            $body[] = null;
            $body[] = 'while ($stream->tell() < $innerLimit) {';
            $body[] = '    ' . $variable . '->add(' . $readSttm . ');';
            $body[] = '}';
            $body[] = null;
            $body[] = $breakSttm;

            return $body;
        }

        if ($type === Type::TYPE_MESSAGE() && $rule === Label::LABEL_REPEATED()) {
            $body[] = '$innerSize    = $reader->readVarint($stream);';
            $body[] = '$innerMessage = new ' . $reference . '();';
            $body[] = null;
            $body[] = 'if (' . $variable . ' === null) {';
            $body[] = '    ' . $variable . ' = new \Protobuf\MessageCollection();';
            $body[] = '}';
            $body[] = null;
            $body[] = $variable . '->add($innerMessage);';
            $body[] = null;
            $body[] = '$context->setLength($innerSize);';
            $body[] = '$innerMessage->readFrom($context);';
            $body[] = '$context->setLength($length);';
            $body[] = null;
            $body[] = $breakSttm;

            return $body;
        }

        if ($type === Type::TYPE_ENUM() && $rule === LABEL::LABEL_REPEATED()) {
            $body[] = 'if (' . $variable . ' === null) {';
            $body[] = '    ' . $variable . ' = new ' . $this->getCollectionClassName($field) . '();';
            $body[] = '}';
            $body[] = null;
            $body[] = $variable . '->add(' . $reference . '::valueOf(' . $this->generateReadScalarStatement($type->value()) . '));';
            $body[] = null;
            $body[] = $breakSttm;

            return $body;
        }

        if ($type === Type::TYPE_MESSAGE()) {
            $body[] = '$innerSize    = $reader->readVarint($stream);';
            $body[] = '$innerMessage = new ' . $reference . '();';
            $body[] = null;
            $body[] = $variable . ' = $innerMessage;';
            $body[] = null;
            $body[] = '$context->setLength($innerSize);';
            $body[] = '$innerMessage->readFrom($context);';
            $body[] = '$context->setLength($length);';
            $body[] = null;
            $body[] = $breakSttm;

            return $body;
        }

        if ($type === Type::TYPE_ENUM()) {
            $body[] = $variable . ' = ' . $reference . '::valueOf(' . $this->generateReadScalarStatement($type->value()) . ');';
            $body[] = null;
            $body[] = $breakSttm;

            return $body;
        }

        if ($rule !== LABEL::LABEL_REPEATED()) {
            $body[] = $variable . ' = ' . $this->generateReadScalarStatement($type->value()) . ';';
            $body[] = null;
            $body[] = $breakSttm;

            return $body;
        }

        $body[] = 'if (' . $variable . ' === null) {';
        $body[] = '    ' . $variable . ' = new ' . $this->getCollectionClassName($field) . '();';
        $body[] = '}';
        $body[] = null;
        $body[] = $variable . '->add(' . $this->generateReadScalarStatement($type->value()) . ');';
        $body[] = null;
        $body[] = $breakSttm;

        return $body;
    }
1153509
public function getRelativePath($entity, $field)
    {
        if (is_object($entity)) {
            $entity = get_class($entity);
        }
        $entity = strtolower(Str::classname($entity));

        return $entity . '/' . $field;
    }
711032
protected function generateFieldCondition(Entity $entity, FieldDescriptorProto $field)
    {
        $body      = [];
        $fieldName = $field->getName();
        $format    = 'if ($this->%s !== null) {';
        $sttm      = $this->generateFieldWriteStatement($entity, $field);
        $lines     = $this->addIndentation($sttm, 1);

        $body[] = sprintf($format, $fieldName);
        $body   = array_merge($body, $lines);
        $body[] = '}';

        return $body;
    }
386948
public function buildReference(View $businessPage, EntityManager $em)
    {
        $referenceId = ViewReferenceHelper::generateViewReferenceId($businessPage);
        $businessPageReference = new BusinessPageReference();
        $businessPageReference->setId($referenceId);
        $businessPageReference->setLocale($businessPage->getCurrentLocale());
        $businessPageReference->setName($businessPage->getName());
        $businessPageReference->setViewId($businessPage->getId());
        $businessPageReference->setTemplateId($businessPage->getTemplate()->getId());
        $businessPageReference->setSlug($businessPage->getSlug());
        $businessPageReference->setEntityId($businessPage->getBusinessEntity()->getId());
        $businessPageReference->setEntityNamespace($em->getClassMetadata(get_class($businessPage->getBusinessEntity()))->name);
        $businessPageReference->setViewNamespace($em->getClassMetadata(get_class($businessPage))->name);
        if ($parent = $businessPage->getParent()) {
            $parent->setCurrentLocale($businessPage->getCurrentLocale());
            $businessPageReference->setParent(ViewReferenceHelper::generateViewReferenceId($parent));
        }

        return $businessPageReference;
    }
960801
protected function guessSingularField($entity, $field = null)
    {
        return $field ?: Inflector::singularize(
            lcfirst(basename(str_replace('\\', '/', $entity)))
        );
    }
1049834
public function findByEntityInstance($entity)
    {
        $businessEntity = null;
        $class = new \ReflectionClass($entity);
        while (!$businessEntity && $class && $class->name !== null) {
            $businessEntity = $this->findByEntityClassname($class->name);
            $class = $class->getParentClass();
        }

        return $businessEntity;
    }
633688
public Attribute getEntityAttribute(Class clazz, String fieldName)
    {
        if (entityTypes != null && entityTypes.containsKey(clazz))
        {
            EntityType entityType = entityTypes.get(clazz);
            return entityType.getAttribute(fieldName);
        }
        throw new IllegalArgumentException("No entity found: " + clazz);
    }
1232736
protected function generateExtensionMethod(Entity $entity, FieldDescriptorProto $field)
    {
        $fieldName  = $field->getName();
        $descriptor = $entity->getDescriptor();
        $methodName = $this->getCamelizedName($field);
        $bodyGen    = new ExtensionsGenerator($this->context);
        $body       = implode(PHP_EOL, $bodyGen->generateBody($entity, $field));
        $method     = MethodGenerator::fromArray([
            'static'     => true,
            'body'       => $body,
            'name'       => $methodName,
            'docblock'   => [
                'shortDescription' => "Extension field : $fieldName",
                'tags'             => [
                    [
                        'name'        => 'return',
                        'description' => '\Protobuf\Extension',
                    ]
                ]
            ]
        ]);

        $method->getDocblock()->setWordWrap(false);

        return $method;
    }
1349989
protected function generateHasMethod(Entity $entity, FieldDescriptorProto $field)
    {
        $fieldName  = $field->getName();
        $methodName = $this->getAccessorName('has', $field);
        $method     = MethodGenerator::fromArray([
            'name'       => $methodName,
            'body'       => 'return $this->' . $fieldName . ' !== null;',
            'docblock'   => [
                'shortDescription' => "Check if '$fieldName' has a value",
                'tags'             => [
                    [
                        'name'        => 'return',
                        'description' => 'bool',
                    ]
                ]
            ]
        ]);

        return $method;
    }
992083
protected function generateRequiredFieldException(Entity $entity, FieldDescriptorProto $field)
    {
        $name       = $field->getName();
        $label      = $field->getLabel();
        $tag        = $field->getNumber();
        $isRequired = $label === Label::LABEL_REQUIRED();

        if ( ! $isRequired) {
            return [];
        }

        $class   = $entity->getNamespacedName();
        $format  = 'Field "%s#%s" (tag %s) is required but has no value.';
        $message = var_export(sprintf($format, $class, $name, $tag), true);

        $body[] = 'if ($this->' . $name . ' === null) {';
        $body[] = '    throw new \UnexpectedValueException(' . $message . ');';
        $body[] = '}';
        $body[] = null;

        return $body;
    }
899822
private function getContentFromEntity( $entity ) {
		switch ( $entity->getType() ) {
			case Item::ENTITY_TYPE:
				return new ItemContent( $entity );
			case Property::ENTITY_TYPE:
				return new PropertyContent( $entity );
			default:
				throw new RuntimeException( 'I cant get a content for this type of entity' );
		}
	}
618002
public function page($entity) {
		if ($entity->type !== 'namespace') {
			return false;
		}
		return Pages::find('first', [
			'conditions' => [
				'index' => $entity->index,
				'name' => $entity->name
			]
		]);
	}