/** * Opens a list and returns the contents of the list. * By default a list is interpreted as the own-list of the current bean. * If the list begins with the prefix 'shared-' the shared list of the * bean will be opened instead. Internal method. * * @return array */ private function openList() { $listOfBeans = array(); $listName = strpos($this->list, 'shared-') === 0 ? 'shared' . ucfirst(substr($this->list, 7)) : 'own' . ucfirst($this->list); if ($this->sqlSnippet) { if (preg_match('/^(ORDER|GROUP|HAVING|LIMIT|OFFSET|TOP)\\s+/i', ltrim($this->sqlSnippet))) { $beans = $this->bean->with($this->sqlSnippet, $this->sqlBindings)->{$listName}; } else { $beans = $this->bean->withCondition($this->sqlSnippet, $this->sqlBindings)->{$listName}; } } else { $beans = $this->bean->{$listName}; } foreach ($beans as $listBean) { $listOfBeans[] = $listBean->export(); } return $this->resp($listOfBeans); }
/** * Returns the bean identified by the RESTful path. * For instance: * * $user * /site/1/page/3 * * returns page with ID 3 in ownPage of site 1 in ownSite of * $user bean. * * Works with shared lists as well: * * $user * /site/1/page/3/shared-ad/4 * * Note that this method will open all intermediate beans so you can * attach access control rules to each bean in the path. * * @param RedBean_OODBBean $bean * @param array $steps (an array representation of a REST path) * * @return RedBean_OODBBean * * @throws RedBean_Exception_Security */ public function findByPath($bean, $steps) { $numberOfSteps = count($steps); if (!$numberOfSteps) { return $bean; } if ($numberOfSteps % 2) { throw new RedBean_Exception_Security('Invalid path: needs 1 more element.'); } for ($i = 0; $i < $numberOfSteps; $i += 2) { $steps[$i] = trim($steps[$i]); if ($steps[$i] === '') { throw new RedBean_Exception_Security('Cannot access list.'); } if (strpos($steps[$i], 'shared-') === FALSE) { $listName = 'own' . ucfirst($steps[$i]); $listType = $this->toolbox->getWriter()->esc($steps[$i]); } else { $listName = 'shared' . ucfirst(substr($steps[$i], 7)); $listType = $this->toolbox->getWriter()->esc(substr($steps[$i], 7)); } $list = $bean->withCondition(" {$listType}.id = ? ", array($steps[$i + 1]))->{$listName}; if (!isset($list[$steps[$i + 1]])) { throw new RedBean_Exception_Security('Cannot access bean.'); } $bean = $list[$steps[$i + 1]]; } return $bean; }