/** * Extract EntityAbstract data to array using specified list of attributes. * If no attributes are specified, only simple and Many2One attributes will be extracted. * If you need to get One2Many and Many2Many attributes, you need to explicitly specify a list of attributes. * * @param array $attributes Ex: 'title,author.name,comments.id,comments.text' * * @return array */ public function extractData($attributes = []) { $checkKey = get_class($this->entity) . '-' . $this->entity->getId(); if (self::$loadedEntities->keyExists($checkKey)) { return ['__reference__' => true, 'class' => get_class($this->entity), 'id' => $this->entity->getId()->getValue()]; } else { self::$loadedEntities->key($checkKey, true); } if ($this->isEmpty($attributes)) { $attributes = $this->getDefaultAttributes(); } $data = []; $attributes = $this->buildEntityFields($attributes); foreach ($attributes as $attr => $subAttributes) { $entityAttribute = $this->entity->getAttribute($attr); $entityAttributeValue = $entityAttribute->getValue(); $isOne2Many = $this->isInstanceOf($entityAttribute, AttributeType::ONE2MANY); $isMany2Many = $this->isInstanceOf($entityAttribute, AttributeType::MANY2MANY); $isMany2One = $this->isInstanceOf($entityAttribute, AttributeType::MANY2ONE); if ($isMany2One) { if ($this->isNull($entityAttributeValue)) { $data[$attr] = null; continue; } if (self::$currentLevel < $this->nestedLevel) { self::$currentLevel++; $attrDataExtractor = new EntityDataExtractor($entityAttributeValue, $this->nestedLevel); $data[$attr] = $attrDataExtractor->extractData($subAttributes); self::$currentLevel--; } } elseif ($isOne2Many || $isMany2Many) { $data[$attr] = []; foreach ($entityAttributeValue as $item) { if (self::$currentLevel < $this->nestedLevel) { self::$currentLevel++; $attrDataExtractor = new EntityDataExtractor($item, $this->nestedLevel); $data[$attr][] = $attrDataExtractor->extractData($subAttributes); self::$currentLevel--; } } } else { $data[$attr] = $entityAttribute->getToArrayValue(); } } self::$loadedEntities->removeKey($checkKey); return $data; }
/** * Convert AbstractEntity to array with specified fields. * If no fields are specified, array will contain all simple and Many2One attributes * * @param string $fields List of fields to extract * * @return array */ public function toArray($fields = '') { $dataExtractor = new EntityDataExtractor($this); return $dataExtractor->extractData($fields); }
/** * Convert EntityAbstract to array with specified fields. * If no fields are specified, array will contain all simple and Many2One attributes * * @param string $fields List of fields to extract * * @param int $nestedLevel How many levels to extract (Default: 1, means SELF + 1 level) * * @return array */ public function toArray($fields = '', $nestedLevel = 1) { $dataExtractor = new EntityDataExtractor($this, $nestedLevel); return $dataExtractor->extractData($fields); }