public static function getAttributeValueByPath(EntityInterface $entity, $value_path) { if (!mb_strpos($value_path, self::PATH_DELIMITER)) { return $entity->getValue($value_path); } // prepare path tuples $split_path = self::splitPath($value_path); $path_tuples = $split_path['path_tuples']; $target_attribute = $split_path['target_attribute']; $current_type = $entity->getType(); $current_entity = $entity; // loop into path foreach ($path_tuples as $path_tuple) { $offset_spec = self::parseOffsetExpression($path_tuple[1]); $current_attribute = $current_type->getAttribute($path_tuple[0]); $entity_collection = $current_entity->getValue($current_attribute->getName()); // try to find the next entity that matches the current offset_spec $type_offsets = array('_all' => 0); $current_entity = null; foreach ($entity_collection as $next_entity) { $type_prefix = $next_entity->getType()->getPrefix(); if (!isset($type_offsets[$type_prefix])) { $type_offsets[$type_prefix] = 0; } if (self::entityMatchesOffsetSpec($next_entity, $offset_spec, $type_offsets)) { $current_entity = $next_entity; break; } $type_offsets['_all']++; $type_offsets[$type_prefix]++; } // the value_path/offset_spec is valid, but doesn't match any entities in question if (!$current_entity) { return null; } // prepare for next iteration by switching the current_type to the next level if ($current_attribute instanceof EmbeddedEntityListAttribute) { $current_type = $current_attribute->getEmbeddedTypeByPrefix($offset_spec['entity_type']); } else { throw new RuntimeException('Invalid attribute-type given within attribute-value-path.' . 'Only Reference- and EmbeddedEntityListAttributes are supported.'); } } return $target_attribute ? $current_entity->getValue($target_attribute) : $current_entity; }
/** * @return ArrayConfig */ protected function getGlanceRenderConfig(EntityInterface $entity, $additional_config = []) { $view_scope = $this->getOption('view_scope'); $entity_type = $entity->getType(); if (!$this->glance_config instanceof ConfigInterface) { // global view_config $global_view_config = $this->view_config_service->getViewConfig(self::GLANCE_CONFIG_GLOBAL_SCOPE); $global_view_config_settings = $global_view_config->getSettings(); $global_glance_config = $global_view_config_settings->get('glance_config', new Settings()); // view_config for specific view $view_config = $this->view_config_service->getViewConfig($view_scope); $view_config_settings = $view_config->getSettings(); $view_glance_config = $view_config_settings->get('glance_config', new Settings()); // view_template settings for current embedded-entity-list $list_glance_config = $this->settings->get('glance_config', new Settings()); $this->glance_config = new ArrayConfig(array_replace_recursive($global_glance_config->toArray(), $view_glance_config->toArray(), $list_glance_config->toArray())); } // view_config for resource type. $type_name = $entity_type->getName(); if (!isset($this->entity_type_glance_configs[$type_name])) { $renderer_config_entity_type = $this->view_config_service->getRendererConfig($view_scope, $this->output_format, $entity_type->getScopeKey()); $this->entity_type_glance_configs[$type_name] = $renderer_config_entity_type->get('glance_config', new Settings()); } return new ArrayConfig(array_replace_recursive($this->glance_config->toArray(), $this->entity_type_glance_configs[$type_name]->toArray(), $additional_config)); }
/** * Tells whether this entity is considered equal to another given entity. * Entities are equal when they have the same type and values. * * @param EntityInterface $entity * * @return boolean true on both entities have the same type and values; false otherwise. */ public function isEqualTo(EntityInterface $entity) { if ($entity->getType() !== $this->getType()) { return false; } if ($this->getType()->getAttributes()->getSize() !== $this->value_holder_map->getSize()) { return false; } foreach ($this->getType()->getAttributes()->getKeys() as $attribute_name) { $value_holder = $this->value_holder_map->getItem($attribute_name); if (!$value_holder->sameValueAs($entity->getValue($attribute_name))) { return false; } } return true; }
/** * Transform an incoming value, which is described by the given attributespec, * to it's input (entity compatible) representation and set result on the given entity. * * @param mixed $input_value * @param EntityInterface $entity * @param SpecificationInterface $specification * * @return void */ public function revert($input_value, EntityInterface $entity, SpecificationInterface $specification) { $attribute_name = $specification->getOption('attribute', $specification->getName()); $entity->setValue($attribute_name, $input_value); }
/** * Sets either given default value or value from option to the given attribute. * * @param Entity $entity the entity to modify * @param string $attribute_name the name of the attribute to set a value for * @param mixed $default_value Default value to set. * @param array $options Array containing a `attribute_name => $mixed` entry. * $mixed is set as value instead of $default_value. * If $mixed is a closure it will be called and used. * $mixed may also be another callable like an array * `array($class, "$methodName")` or a string like * `'Your\Namespace\Foo::getStaticTrololo'`. * * @return void */ protected function setValue(EntityInterface $entity, AttributeInterface $attribute, $default_value, array $options = array()) { $attribute_name = $attribute->getName(); $attribute_options = array(); if (!empty($options[self::OPTION_FIELD_VALUES]) && is_array($options[self::OPTION_FIELD_VALUES])) { $attribute_options = $options[self::OPTION_FIELD_VALUES]; } if (empty($attribute_options[$attribute_name])) { $entity->setValue($attribute_name, $default_value); } else { $option = $attribute_options[$attribute_name]; if (is_callable($option)) { $entity->setValue($attribute_name, call_user_func($option)); } else { $entity->setValue($attribute_name, $option); } } }
protected function getEmbeddedEntityTypeFor(EntityInterface $projection, EmbeddedEntityEventInterface $event) { $embed_attribute = $projection->getType()->getAttribute($event->getParentAttributeName()); return $embed_attribute->getEmbeddedTypeByPrefix($event->getEmbeddedEntityType()); }