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

Commit f6a9941

Browse files
committed
Added a polymorphic function for retrieving a single Plex library item by rating key, key, or by exact title match.
1 parent b7bfa42 commit f6a9941

File tree

1 file changed

+76
-1
lines changed

1 file changed

+76
-1
lines changed

Server/Library/SectionAbstract.php

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php
1+
0;95;c<?php
22

33
/**
44
* Plex Server Library Section
@@ -496,6 +496,81 @@ protected function getItemsByYear($year)
496496
);
497497
}
498498

499+
/**
500+
* A generic method to allow polymorphic retrieval of a single library item.
501+
* This will take a rating key, a key, or a string that it will use to
502+
* attempt an exact title match.
503+
*
504+
* @param integer|string $polymorphicData Either a rating key, a key, or a
505+
* title for an exact title match that will be used to retrieve a single
506+
* library item.
507+
*
508+
* @uses Plex_MachineAbstract::getCallingFunction()
509+
* @uses Plex_Server_Library::getItems()
510+
* @uses Plex_Server_Library::functionToType()
511+
* @uses Plex_Server_Library::ENDPOINT_METADATA
512+
* @uses Plex_Server_Library::ENDPOINT_LIBRARY
513+
* @uses Plex_Server_Library_ItemAbstract::getTitle()
514+
*
515+
* @return Plex_Server_Library_ItemAbstract The request Plex library item.
516+
*/
517+
protected function getPolymorphicItem($polymorphicData)
518+
{
519+
if (is_int($polymorphicData)) {
520+
// If we have an integer then we can assume we have a rating key.
521+
return reset(
522+
$this->getItems(
523+
sprintf(
524+
'%s/%d',
525+
Plex_Server_Library::ENDPOINT_METADATA,
526+
$polymorphicData
527+
)
528+
)
529+
);
530+
} else if (strpos($polymorphicData, Plex_Server_Library::ENDPOINT_METADATA)
531+
!= FALSE) {
532+
// If the single item endpoint appears in the polymorphic data then
533+
// is assumed we are dealing with a key, which is already a valid
534+
// endpoint.
535+
536+
// A key will contain the library endpoint, which will be added back
537+
// by our getItems method, so we strip it out here. The buildUrl
538+
// method will handle stripping out and double slashes caused by
539+
// this.
540+
$endpoint = str_replace(
541+
Plex_Server_Library::ENDPOINT_LIBRARY,
542+
'',
543+
$polymorphicData
544+
);
545+
546+
return reset($this->getItems($endpoint));
547+
} else {
548+
549+
// If we don't have a rating key or a key then we just assume we're
550+
// doing an exact title match.
551+
552+
// Find the item type.
553+
$itemType = $this->functionToType(
554+
$this->getCallingFunction()
555+
);
556+
557+
// Find the search method and make sure it exists in the search
558+
// class.
559+
$searchMethod = sprintf('search%ss', ucfirst($itemType));
560+
561+
if (method_exists($this, $searchMethod)) {
562+
foreach ($this->{$searchMethod}($polymorphicData) as $item) {
563+
if ($item->getTitle() === $polymorphicData) {
564+
return $item;
565+
}
566+
}
567+
}
568+
569+
// Tried to do an exact title match and came up empty.
570+
return FALSE;
571+
}
572+
}
573+
499574
/**
500575
* Returns a list of collections for the child class's section. We use
501576
* makeCall directly here, because we want to return just the raw array of

0 commit comments

Comments
 (0)