Spine¶
The Spine class manages the reading order of the content within an EPUB file: the <spine> section of the OPF file.
Key Methods¶
-
__construct(SimpleXMLElement $opfXml, ?Manifest $manifest = null): Parses the spine. The manifest is optional; it is needed to resolve items ingetItems()and to validateadd(). -
get(): array: Returns the manifest item ids (idrefs) in reading order. -
getItems(): array: ReturnsSpineItemobjects in reading order. Each hasidref,linear(falsefor auxiliary content markedlinear="no", such as notes) anditem, the referencedManifestItem(with itspathrelative to the book root), ornullwithout a manifest. -
contains(string $idref): bool: Whether an item is in the reading order. -
add(string $idref, ?int $position = null, bool $linear = true): void: Adds a manifest item to the reading order, at a zero-based position (0 to the spine length) or at the end. Throws if the item is not in the manifest or is already in the spine, or the position is out of range. -
remove(string $idref): void: Removes an item from the reading order (the manifest is not touched). Throws if it is not in the spine. -
addMany(array $entries): void: Appends many manifest items to the reading order at once (each entry has anidref, optionallylinearandproperties);add()rewrites the whole spine each time, which is slow for thousands of items. Throws if an item is unknown or already in the spine (or listed twice), a property is invalid, or properties are given for a package that is not EPUB 3. Nothing is added then. -
removeAt(int $position): void: Removes the itemref at a zero-based position, e.g. a duplicate that is not the first (remove()takes the first). Throws if the position is outside the spine. -
getToc(): ?stringandsetToc(?string $id): void: The id of the NCX manifest item the spine names (<spine toc="...">), ornull. EPUB 2 reading systems find the NCX this way;nullremoves the attribute. Setting throws if the item is not in the manifest. -
move(string $idref, int $position): void: Moves an item to a new zero-based position (0 to the spine length - 1). Throws if it is not in the spine or the position is out of range. -
setLinear(string $idref, bool $linear): void: Marks an entry as part of the main reading order or as auxiliary content (linear="no"), keeping its other attributes. Throws if it is not in the spine. -
getPageProgressionDirection(): ?stringandsetPageProgressionDirection(?string $direction): void: The EPUB 3page-progression-directionof the spine:ltr,rtl(right-to-left books such as manga) ordefault;nullwhen not set, and passingnullremoves it. Setting throws for any other value and for EPUB 2 packages, which do not have the attribute. -
getItemProperties(string $idref): arrayandsetItemProperties(string $idref, array $properties): void: The EPUB 3propertiesof a spine entry (e.g.page-spread-left,rendition:layout-pre-paginated) as a list of tokens; setting replaces them and[]removes them. Throws if the item is not in the spine, a token is empty or has white space, or the package is EPUB 2. -
getPageSpread(string $idref): ?stringandsetPageSpread(string $idref, ?string $side): void: The side of a fixed-layout spread a page belongs on:left(page-spread-left),right(page-spread-right) orcenter(rendition:page-spread-center);nullremoves it. Other properties are kept. -
getItemRendition(string $idref, string $aspect): ?stringandsetItemRendition(string $idref, string $aspect, ?string $value): void: The per-item rendition override of an aspect (layout,orientation,spreadorflow), e.g.pre-paginatedforrendition:layout-pre-paginated;nullremoves it. The values are those of the book-levelMetadata::setRenditionLayout()and its siblings. Writing arendition:property declares therenditionprefix in the package. See EPUB 3 Features. -
isModified(): bool: Whether the reading order changed since loading or the last save.
Changes are made to the OPF in memory; EpubFile::save() writes them (and refreshes the EPUB 3 modified date). Other <itemref> attributes, such as id and properties, are kept.
Usage Example¶
use PhpEpub\EpubFile;
$epubFile = EpubFile::open('/path/to/your.epub');
$spine = $epubFile->getSpine();
foreach ($spine->getItems() as $spineItem) {
$type = $spineItem->linear ? 'main' : 'auxiliary';
echo "{$spineItem->idref} ({$type}): {$spineItem->item?->path}\n";
}
// Show the notes right after the first chapter, then save.
$spine->move('notes', 1);
$epubFile->save();