Skip to content
This repository has been archived by the owner on Jun 9, 2023. It is now read-only.

Commit

Permalink
Deprecated the getSectionByKey method from Library.php and added in a…
Browse files Browse the repository at this point in the history
… polymorphic getSection method, which makes it possible to get a section by its key or by its title.
  • Loading branch information
nickbart committed Apr 7, 2013
1 parent 7da014a commit 9a31fb3
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 5 deletions.
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,9 @@ Get items at the library level

// Sections
$server->getLibrary()->getSections();
$server->getLibrary()->getSectionByKey(1);
$server->getLibrary()->getSectionByKey(4);
$server->getLibrary()->getSectionByKey(5);
$server->getLibrary()->getSectionByKey(6);
$server->getLibrary()->getSection('Movies');
$server->getLibrary()->getSection('TV Shows');
$server->getLibrary()->getSection('Music');

Movies

Expand Down
46 changes: 45 additions & 1 deletion Server/Library.php
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,9 @@ public function getSections()
* @return Plex_Server_Library_Section The request library section.
*
* @throws Plex_Exception_Server_Library()
*
* @deprecated This method is deprectated in lieu of the new getSection()
* method.
*/
public function getSectionByKey($key)
{
Expand All @@ -274,7 +277,48 @@ public function getSectionByKey($key)
array('section', $key)
);
}


/**
* Returns a Plex library section by its given key or by a exact match on
* title. Here we simply run self::getSections() because the endpoint
* /library/sections/ID does not return full section data, it returns the
* categories below the section.
*
* @param integer|string $polymorphicData The key or title of the requested
* section.
*
* @uses Plex_Server_Library::getSections()
* @uses Plex_Server_Library_Section::getKey()
* @uses Plex_Server_Library_Section::getTitle()
*
* @return Plex_Server_Library_Section The request library section.
*
* @throws Plex_Exception_Server_Library()
*/
public function getSection($polymorphicData)
{
// If we have an integer we are getting the section by key.
if (is_int($polymorphicData)) {
foreach ($this->getSections() as $section) {
if ($section->getKey() === $polymorphicData) {
return $section;
}
}
// If we have a string we are getting the section by title.
} else if (is_string($polymorphicData)) {
foreach ($this->getSections() as $section) {
if ($section->getTitle() === $polymorphicData) {
return $section;
}
}
}

throw new Plex_Exception_Server_Library(
'RESOURCE_NOT_FOUND',
array('section', $polymorphicData)
);
}

/**
* Returns the recently added items at the library level.
*
Expand Down

0 comments on commit 9a31fb3

Please sign in to comment.