Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions src/Block/Jpeg/Jpeg.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@

use FileEye\MediaProbe\Block\RawData;
use FileEye\MediaProbe\Collection\CollectionFactory;
use FileEye\MediaProbe\Collection\CollectionInterface;
use FileEye\MediaProbe\Data\DataElement;
use FileEye\MediaProbe\Data\DataException;
use FileEye\MediaProbe\Data\DataFormat;
use FileEye\MediaProbe\ItemDefinition;
use FileEye\MediaProbe\Model\BlockBase;
use FileEye\MediaProbe\Model\MediaTypeBlockInterface;
use FileEye\MediaProbe\Model\RootBlockBase;
use FileEye\MediaProbe\Utility\ConvertBytes;

/**
Expand All @@ -27,12 +29,19 @@ class Jpeg extends BlockBase implements MediaTypeBlockInterface
*/
const JPEG_HEADER = "\xFF\xD8\xFF";

public function __construct(
protected CollectionInterface $collection,
RootBlockBase $parent,
) {
parent::__construct(new ItemDefinition($collection), $parent, null, false);
}

public static function isDataMatchingMediaType(DataElement $dataElement): bool
{
return $dataElement->getBytes(0, 3) === static::JPEG_HEADER;
}

public function doParseData(DataElement $data): void
public function fromDataElement(DataElement $data): Jpeg
{
assert($this->debugInfo(['dataElement' => $data]));

Expand Down Expand Up @@ -69,7 +78,7 @@ public function doParseData(DataElement $data): void
$offset = $newOffset;
} catch (DataException $e) {
$this->error($e->getMessage());
return;
return $this;
}

// Get the JPEG segment id.
Expand Down Expand Up @@ -119,6 +128,8 @@ public function doParseData(DataElement $data): void
if (!$this->getElement("jpegSegment[@name='EOI']")) {
$this->error('Missing EOI (End Of Image) JPEG marker');
}

return $this;
}

/**
Expand Down
19 changes: 11 additions & 8 deletions src/Media.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,6 @@ public static function createFromFile(
return $media;
}

/**
* Creates a Media object from data.
*
* @param DataElement $dataElement
* The data element providing the data.
*/
public function fromDataElement(DataElement $dataElement): Media
{
$this->getStopwatch()->start('media-parsing');
Expand All @@ -79,8 +73,12 @@ public function fromDataElement(DataElement $dataElement): Media
// Build the Media immediate child object, that represents the actual media. Then
// parse the media according to the media format.
$mediaTypeHandler = $mediaTypeCollection->getHandler();
$mediaTypeBlock = new $mediaTypeHandler(new ItemDefinition($mediaTypeCollection), $this);
$mediaTypeBlock->parseData($dataElement);
$mediaTypeBlock = new $mediaTypeHandler(
collection: $mediaTypeCollection,
parent: $this,
);
$mediaTypeBlock->fromDataElement($dataElement);
$this->graftBlock($mediaTypeBlock);
$this->level = $mediaTypeBlock->level();
} catch (MediaProbeException $e) {
assert($this->debugInfo(['dataElement' => $dataElement]));
Expand All @@ -92,6 +90,11 @@ public function fromDataElement(DataElement $dataElement): Media
return $this;
}

public function graftBlock($mediaTypeBlock): void
{
$this->DOMNode->appendChild($mediaTypeBlock->DOMNode);
}

/**
* Save the Media object as a file.
*
Expand Down
10 changes: 8 additions & 2 deletions src/Model/BlockBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,14 @@
ItemDefinition $definition,
?BlockInterface $parent = null,
?BlockInterface $reference = null,
bool $graft = TRUE,
) {
$this->definition = $definition;

parent::__construct($this->getCollection()->getPropertyValue('DOMNode'), $parent, $reference);
parent::__construct($this->getCollection()->getPropertyValue('DOMNode'), $parent, $reference, $graft);

if (!isset($this->DOMNode)) {
return;
throw new MediaProbeException(sprintf('No DOM node specified for %s', __CLASS__));

Check failure on line 55 in src/Model/BlockBase.php

View workflow job for this annotation

GitHub Actions / php-version (8.2)

Instantiated class FileEye\MediaProbe\Model\MediaProbeException not found.
}

if ($this->getCollection()->hasProperty('item')) {
Expand Down Expand Up @@ -111,6 +112,11 @@
$this->executePostParseCallbacks($data);
}

public function fromDataElement(DataElement $dataElement): BlockInterface
{
throw new \LogicException(sprintf('%s does not implement %s()', get_class($this), 'fromDataElement'));
}

/**
* Invoke post-parse callbacks.
*
Expand Down
2 changes: 2 additions & 0 deletions src/Model/BlockInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,6 @@ public function getDefinition(): ItemDefinition;
public function getCollection(): CollectionInterface;

public function parseData(DataElement $dataElement, int $start = 0, ?int $size = null): void;

public function fromDataElement(DataElement $dataElement): BlockInterface;
}
22 changes: 14 additions & 8 deletions src/Model/ElementBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,25 @@ abstract class ElementBase implements ElementInterface, LoggerInterface
* (Optional) if specified, the new element will be inserted
* before the reference element.
*/
public function __construct(string $dom_node_name, ?ElementInterface $parent = null, ?ElementInterface $reference = null)
{
public function __construct(
string $dom_node_name,
protected ?ElementInterface $parent = null,
?ElementInterface $reference = null,
bool $graft = TRUE,
) {
// If $parent is null, this Element is the root of the DOM document that
// stores the media structure.
if (isset($parent) && isset($parent->DOMNode)) {
$doc = $parent->DOMNode->ownerDocument;
$parent_node = $parent->DOMNode;
$this->DOMNode = $doc->createElement($dom_node_name);
if ($reference) {
assert($reference instanceof ElementBase);
$parent_node->insertBefore($this->DOMNode, $reference->DOMNode);
} else {
$parent_node->appendChild($this->DOMNode);
if ($graft) {
if ($reference) {
assert($reference instanceof ElementBase);
$parent_node->insertBefore($this->DOMNode, $reference->DOMNode);
} else {
$parent_node->appendChild($this->DOMNode);
}
}
// Assign this Element as the payload of the DOM node.
$this->DOMNode->setMediaProbeElement($this);
Expand All @@ -83,7 +89,7 @@ public function getParentElement(): ?ElementInterface
if ($domNode->getMediaProbeElement() !== $this->getRootElement()) {
$parentDomNode = $this->DOMNode->parentNode;
assert($parentDomNode instanceof DOMElement);
return $parentDomNode->getMediaProbeElement();
return $parentDomNode ? $parentDomNode->getMediaProbeElement() : $this->parent;
}
return null;
}
Expand Down
Loading