/** * Hydrates a node by querying the database for it * and updating it's reserved attributes from the * queried record. * * @param Cartalyst\NestedSets\Nodes\NodeInterface $node * @return void */ public function hydrateNode(NodeInterface $node) { $table = $this->getTable(); $attributes = $this->getReservedAttributeNames(); $keyName = $this->baseNode->getKeyName(); $result = $this->connection->table("{$table}")->where($keyName, '=', $key = $node->getAttribute($keyName))->first(array_values($attributes)); if ($result === null) { throw new \RuntimeException("Attempting to hydrate non-existent node [{$key}]."); } // We only want to update the attributes which // affect nested sets. $attributes = array_intersect_key((array) $result, array_flip($attributes)); foreach ($attributes as $key => $value) { $node->setAttribute($key, $value); } }
/** * Fires the "after update" trigger for a node.* * * @param Cartalyst\NestedSets\Nodes\NodeInterface $node * @return void */ public function afterUpdateNode(NodeInterface $node) { $node->syncOriginal(); $node->afterUpdate(); }
/** * Actually does the magic for presenting nodes. * * @param Cartalyst\NestedSets\Nodes\NodeInterface $node * @param string $format * @param string|Closure $attribute * @param int $depth * @return mixed */ public function recursivelyPresentAs(NodeInterface $node, $format, $attribute, $depth = 0, $includeNode = false) { $present = []; $node->findChildren($depth); foreach ($node->getChildren() as $child) { $extracted = $this->extractPresentable($child, $attribute); if ($child->getChildren()) { $present[$extracted] = $this->presentChildrenAs($child, 'array', $attribute, $depth, false); } else { $present[] = $extracted; } } if ($includeNode === true) { $extracted = $this->extractPresentable($node, $attribute); $present = [$extracted => $present]; } return $this->{'presentArrayAs' . ucfirst($format)}($present); }