/** * @param array $data * @param \Exolnet\Taxonomy\Taxonomy|null $parent * @return \Exolnet\Taxonomy\Taxonomy */ protected function importTaxonomy(array $data, Taxonomy $parent = null) { $taxonomy = $this->newTaxonomy(); $attributes = array_except($data, 'children'); $children = (array) array_get($data, 'children', []); // Assign attributes $taxonomy->fillWithTranslations($attributes); // Assign computed attributes foreach ($taxonomy->getTranslations() as $translation) { if ($translation->getSlug() === null) { $slug = Str::slug($translation->getName()); $translation->setSlug($slug); } if ($translation->getPermalink() === null) { $permalink = ltrim(($parent ? $parent->getTranslation($translation->getLocale())->getPermalink() . '/' : '') . $translation->getSlug(), '/'); $translation->setPermalink($permalink); } } // Save the taxonomy if ($parent) { $taxonomy->insertAsChildOf($parent); } else { $taxonomy->insertAsRoot(); } // Import children $this->importTaxonomies($children, $taxonomy); return $taxonomy; }