1
This commit is contained in:
7
vendor/phpoffice/math/src/Math/Element/AbstractElement.php
vendored
Normal file
7
vendor/phpoffice/math/src/Math/Element/AbstractElement.php
vendored
Normal file
@@ -0,0 +1,7 @@
|
||||
<?php
|
||||
|
||||
namespace PhpOffice\Math\Element;
|
||||
|
||||
abstract class AbstractElement
|
||||
{
|
||||
}
|
||||
35
vendor/phpoffice/math/src/Math/Element/AbstractGroupElement.php
vendored
Normal file
35
vendor/phpoffice/math/src/Math/Element/AbstractGroupElement.php
vendored
Normal file
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
namespace PhpOffice\Math\Element;
|
||||
|
||||
abstract class AbstractGroupElement extends AbstractElement
|
||||
{
|
||||
/**
|
||||
* @var AbstractElement[]
|
||||
*/
|
||||
protected $elements = [];
|
||||
|
||||
public function add(AbstractElement $element): self
|
||||
{
|
||||
$this->elements[] = $element;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function remove(AbstractElement $element): self
|
||||
{
|
||||
$this->elements = array_filter($this->elements, function ($child) use ($element) {
|
||||
return $child != $element;
|
||||
});
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return AbstractElement[]
|
||||
*/
|
||||
public function getElements(): array
|
||||
{
|
||||
return $this->elements;
|
||||
}
|
||||
}
|
||||
46
vendor/phpoffice/math/src/Math/Element/Fraction.php
vendored
Normal file
46
vendor/phpoffice/math/src/Math/Element/Fraction.php
vendored
Normal file
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
namespace PhpOffice\Math\Element;
|
||||
|
||||
class Fraction extends AbstractElement
|
||||
{
|
||||
/**
|
||||
* @var AbstractElement
|
||||
*/
|
||||
protected $denominator;
|
||||
|
||||
/**
|
||||
* @var AbstractElement
|
||||
*/
|
||||
protected $numerator;
|
||||
|
||||
public function __construct(AbstractElement $numerator, AbstractElement $denominator)
|
||||
{
|
||||
$this->setNumerator($numerator);
|
||||
$this->setDenominator($denominator);
|
||||
}
|
||||
|
||||
public function getDenominator(): AbstractElement
|
||||
{
|
||||
return $this->denominator;
|
||||
}
|
||||
|
||||
public function getNumerator(): AbstractElement
|
||||
{
|
||||
return $this->numerator;
|
||||
}
|
||||
|
||||
public function setDenominator(AbstractElement $element): self
|
||||
{
|
||||
$this->denominator = $element;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setNumerator(AbstractElement $element): self
|
||||
{
|
||||
$this->numerator = $element;
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
21
vendor/phpoffice/math/src/Math/Element/Identifier.php
vendored
Normal file
21
vendor/phpoffice/math/src/Math/Element/Identifier.php
vendored
Normal file
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
namespace PhpOffice\Math\Element;
|
||||
|
||||
class Identifier extends AbstractElement
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $value;
|
||||
|
||||
public function __construct(string $value)
|
||||
{
|
||||
$this->value = $value;
|
||||
}
|
||||
|
||||
public function getValue(): string
|
||||
{
|
||||
return $this->value;
|
||||
}
|
||||
}
|
||||
21
vendor/phpoffice/math/src/Math/Element/Numeric.php
vendored
Normal file
21
vendor/phpoffice/math/src/Math/Element/Numeric.php
vendored
Normal file
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
namespace PhpOffice\Math\Element;
|
||||
|
||||
class Numeric extends AbstractElement
|
||||
{
|
||||
/**
|
||||
* @var float
|
||||
*/
|
||||
protected $value;
|
||||
|
||||
public function __construct(float $value)
|
||||
{
|
||||
$this->value = $value;
|
||||
}
|
||||
|
||||
public function getValue(): float
|
||||
{
|
||||
return $this->value;
|
||||
}
|
||||
}
|
||||
21
vendor/phpoffice/math/src/Math/Element/Operator.php
vendored
Normal file
21
vendor/phpoffice/math/src/Math/Element/Operator.php
vendored
Normal file
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
namespace PhpOffice\Math\Element;
|
||||
|
||||
class Operator extends AbstractElement
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $value;
|
||||
|
||||
public function __construct(string $value)
|
||||
{
|
||||
$this->value = $value;
|
||||
}
|
||||
|
||||
public function getValue(): string
|
||||
{
|
||||
return $this->value;
|
||||
}
|
||||
}
|
||||
7
vendor/phpoffice/math/src/Math/Element/Row.php
vendored
Normal file
7
vendor/phpoffice/math/src/Math/Element/Row.php
vendored
Normal file
@@ -0,0 +1,7 @@
|
||||
<?php
|
||||
|
||||
namespace PhpOffice\Math\Element;
|
||||
|
||||
class Row extends AbstractGroupElement
|
||||
{
|
||||
}
|
||||
33
vendor/phpoffice/math/src/Math/Element/Semantics.php
vendored
Normal file
33
vendor/phpoffice/math/src/Math/Element/Semantics.php
vendored
Normal file
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace PhpOffice\Math\Element;
|
||||
|
||||
class Semantics extends AbstractGroupElement
|
||||
{
|
||||
/**
|
||||
* @var array<string, string>
|
||||
*/
|
||||
protected $annotations = [];
|
||||
|
||||
public function addAnnotation(string $encoding, string $annotation): self
|
||||
{
|
||||
$this->annotations[$encoding] = $annotation;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getAnnotation(string $encoding): ?string
|
||||
{
|
||||
return $this->annotations[$encoding] ?? null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, string>
|
||||
*/
|
||||
public function getAnnotations(): array
|
||||
{
|
||||
return $this->annotations;
|
||||
}
|
||||
}
|
||||
46
vendor/phpoffice/math/src/Math/Element/Superscript.php
vendored
Normal file
46
vendor/phpoffice/math/src/Math/Element/Superscript.php
vendored
Normal file
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
namespace PhpOffice\Math\Element;
|
||||
|
||||
class Superscript extends AbstractElement
|
||||
{
|
||||
/**
|
||||
* @var AbstractElement
|
||||
*/
|
||||
protected $base;
|
||||
|
||||
/**
|
||||
* @var AbstractElement
|
||||
*/
|
||||
protected $superscript;
|
||||
|
||||
public function __construct(AbstractElement $base, AbstractElement $superscript)
|
||||
{
|
||||
$this->setBase($base);
|
||||
$this->setSuperscript($superscript);
|
||||
}
|
||||
|
||||
public function getBase(): AbstractElement
|
||||
{
|
||||
return $this->base;
|
||||
}
|
||||
|
||||
public function getSuperscript(): AbstractElement
|
||||
{
|
||||
return $this->superscript;
|
||||
}
|
||||
|
||||
public function setBase(AbstractElement $element): self
|
||||
{
|
||||
$this->base = $element;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setSuperscript(AbstractElement $element): self
|
||||
{
|
||||
$this->superscript = $element;
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
7
vendor/phpoffice/math/src/Math/Exception/InvalidInputException.php
vendored
Normal file
7
vendor/phpoffice/math/src/Math/Exception/InvalidInputException.php
vendored
Normal file
@@ -0,0 +1,7 @@
|
||||
<?php
|
||||
|
||||
namespace PhpOffice\Math\Exception;
|
||||
|
||||
class InvalidInputException extends MathException
|
||||
{
|
||||
}
|
||||
9
vendor/phpoffice/math/src/Math/Exception/MathException.php
vendored
Normal file
9
vendor/phpoffice/math/src/Math/Exception/MathException.php
vendored
Normal file
@@ -0,0 +1,9 @@
|
||||
<?php
|
||||
|
||||
namespace PhpOffice\Math\Exception;
|
||||
|
||||
use Exception;
|
||||
|
||||
class MathException extends Exception
|
||||
{
|
||||
}
|
||||
7
vendor/phpoffice/math/src/Math/Exception/NotImplementedException.php
vendored
Normal file
7
vendor/phpoffice/math/src/Math/Exception/NotImplementedException.php
vendored
Normal file
@@ -0,0 +1,7 @@
|
||||
<?php
|
||||
|
||||
namespace PhpOffice\Math\Exception;
|
||||
|
||||
class NotImplementedException extends MathException
|
||||
{
|
||||
}
|
||||
9
vendor/phpoffice/math/src/Math/Math.php
vendored
Normal file
9
vendor/phpoffice/math/src/Math/Math.php
vendored
Normal file
@@ -0,0 +1,9 @@
|
||||
<?php
|
||||
|
||||
namespace PhpOffice\Math;
|
||||
|
||||
use PhpOffice\Math\Element\AbstractGroupElement;
|
||||
|
||||
class Math extends AbstractGroupElement
|
||||
{
|
||||
}
|
||||
136
vendor/phpoffice/math/src/Math/Reader/MathML.php
vendored
Normal file
136
vendor/phpoffice/math/src/Math/Reader/MathML.php
vendored
Normal file
@@ -0,0 +1,136 @@
|
||||
<?php
|
||||
|
||||
namespace PhpOffice\Math\Reader;
|
||||
|
||||
use DOMDocument;
|
||||
use DOMElement;
|
||||
use DOMNode;
|
||||
use DOMXPath;
|
||||
use PhpOffice\Math\Element;
|
||||
use PhpOffice\Math\Exception\InvalidInputException;
|
||||
use PhpOffice\Math\Exception\NotImplementedException;
|
||||
use PhpOffice\Math\Math;
|
||||
|
||||
class MathML implements ReaderInterface
|
||||
{
|
||||
/** @var Math */
|
||||
private $math;
|
||||
|
||||
/** @var DOMDocument */
|
||||
private $dom;
|
||||
|
||||
/** @var DOMXpath */
|
||||
private $xpath;
|
||||
|
||||
public function read(string $content): ?Math
|
||||
{
|
||||
$content = str_replace(
|
||||
[
|
||||
'⁢',
|
||||
],
|
||||
[
|
||||
'<mchar name="InvisibleTimes"/>',
|
||||
],
|
||||
$content
|
||||
);
|
||||
|
||||
$this->dom = new DOMDocument();
|
||||
$this->dom->loadXML($content, LIBXML_DTDLOAD);
|
||||
|
||||
$this->math = new Math();
|
||||
$this->parseNode(null, $this->math);
|
||||
|
||||
return $this->math;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Math|Element\AbstractGroupElement $parent
|
||||
*/
|
||||
protected function parseNode(?DOMNode $nodeRowElement, $parent): void
|
||||
{
|
||||
$this->xpath = new DOMXpath($this->dom);
|
||||
foreach ($this->xpath->query('*', $nodeRowElement) ?: [] as $nodeElement) {
|
||||
if ($parent instanceof Element\Semantics
|
||||
&& $nodeElement instanceof DOMElement
|
||||
&& $nodeElement->nodeName == 'annotation') {
|
||||
$parent->addAnnotation(
|
||||
$nodeElement->getAttribute('encoding'),
|
||||
trim($nodeElement->nodeValue)
|
||||
);
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
$element = $this->getElement($nodeElement);
|
||||
$parent->add($element);
|
||||
|
||||
if ($element instanceof Element\AbstractGroupElement) {
|
||||
$this->parseNode($nodeElement, $element);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected function getElement(DOMNode $nodeElement): Element\AbstractElement
|
||||
{
|
||||
$nodeValue = trim($nodeElement->nodeValue);
|
||||
switch ($nodeElement->nodeName) {
|
||||
case 'mfrac':
|
||||
$nodeList = $this->xpath->query('*', $nodeElement);
|
||||
if ($nodeList && $nodeList->length == 2) {
|
||||
return new Element\Fraction(
|
||||
$this->getElement($nodeList->item(0)),
|
||||
$this->getElement($nodeList->item(1))
|
||||
);
|
||||
}
|
||||
|
||||
throw new InvalidInputException(sprintf(
|
||||
'%s : The tag `%s` has not two subelements',
|
||||
__METHOD__,
|
||||
$nodeElement->nodeName
|
||||
));
|
||||
case 'mi':
|
||||
return new Element\Identifier($nodeValue);
|
||||
case 'mn':
|
||||
return new Element\Numeric(floatval($nodeValue));
|
||||
case 'mo':
|
||||
if (empty($nodeValue)) {
|
||||
$nodeList = $this->xpath->query('*', $nodeElement);
|
||||
if (
|
||||
$nodeList
|
||||
&& $nodeList->length == 1
|
||||
&& $nodeList->item(0)->nodeName == 'mchar'
|
||||
&& $nodeList->item(0) instanceof DOMElement
|
||||
&& $nodeList->item(0)->hasAttribute('name')
|
||||
) {
|
||||
$nodeValue = $nodeList->item(0)->getAttribute('name');
|
||||
}
|
||||
}
|
||||
|
||||
return new Element\Operator($nodeValue);
|
||||
case 'mrow':
|
||||
return new Element\Row();
|
||||
case 'msup':
|
||||
$nodeList = $this->xpath->query('*', $nodeElement);
|
||||
if ($nodeList && $nodeList->length == 2) {
|
||||
return new Element\Superscript(
|
||||
$this->getElement($nodeList->item(0)),
|
||||
$this->getElement($nodeList->item(1))
|
||||
);
|
||||
}
|
||||
|
||||
throw new InvalidInputException(sprintf(
|
||||
'%s : The tag `%s` has not two subelements',
|
||||
__METHOD__,
|
||||
$nodeElement->nodeName
|
||||
));
|
||||
case 'semantics':
|
||||
return new Element\Semantics();
|
||||
default:
|
||||
throw new NotImplementedException(sprintf(
|
||||
'%s : The tag `%s` is not implemented',
|
||||
__METHOD__,
|
||||
$nodeElement->nodeName
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
133
vendor/phpoffice/math/src/Math/Reader/OfficeMathML.php
vendored
Normal file
133
vendor/phpoffice/math/src/Math/Reader/OfficeMathML.php
vendored
Normal file
@@ -0,0 +1,133 @@
|
||||
<?php
|
||||
|
||||
namespace PhpOffice\Math\Reader;
|
||||
|
||||
use DOMDocument;
|
||||
use DOMNode;
|
||||
use DOMXPath;
|
||||
use PhpOffice\Math\Element;
|
||||
use PhpOffice\Math\Exception\InvalidInputException;
|
||||
use PhpOffice\Math\Exception\NotImplementedException;
|
||||
use PhpOffice\Math\Math;
|
||||
|
||||
class OfficeMathML implements ReaderInterface
|
||||
{
|
||||
/** @var DOMDocument */
|
||||
protected $dom;
|
||||
|
||||
/** @var Math */
|
||||
protected $math;
|
||||
|
||||
/** @var DOMXpath */
|
||||
protected $xpath;
|
||||
|
||||
/** @var string[] */
|
||||
protected $operators = ['+', '-', '/', '∗'];
|
||||
|
||||
public function read(string $content): ?Math
|
||||
{
|
||||
$nsMath = 'xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math"';
|
||||
$nsWord = 'xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"';
|
||||
|
||||
$content = str_replace(
|
||||
$nsMath,
|
||||
$nsMath . ' ' . $nsWord,
|
||||
$content
|
||||
);
|
||||
|
||||
$this->dom = new DOMDocument();
|
||||
$this->dom->loadXML($content);
|
||||
|
||||
$this->math = new Math();
|
||||
$this->parseNode(null, $this->math);
|
||||
|
||||
return $this->math;
|
||||
}
|
||||
|
||||
/**
|
||||
* @see https://devblogs.microsoft.com/math-in-office/officemath/
|
||||
* @see https://learn.microsoft.com/fr-fr/archive/blogs/murrays/mathml-and-ecma-math-omml
|
||||
*
|
||||
* @param Math|Element\AbstractGroupElement $parent
|
||||
*/
|
||||
protected function parseNode(?DOMNode $nodeRowElement, $parent): void
|
||||
{
|
||||
$this->xpath = new DOMXpath($this->dom);
|
||||
foreach ($this->xpath->query('*', $nodeRowElement) ?: [] as $nodeElement) {
|
||||
$element = $this->getElement($nodeElement);
|
||||
$parent->add($element);
|
||||
|
||||
if ($element instanceof Element\AbstractGroupElement) {
|
||||
$this->parseNode($nodeElement, $element);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected function getElement(DOMNode $nodeElement): Element\AbstractElement
|
||||
{
|
||||
switch ($nodeElement->nodeName) {
|
||||
case 'm:f':
|
||||
// Numerator
|
||||
$nodeNumerator = $this->xpath->query('m:num/m:r/m:t', $nodeElement);
|
||||
if ($nodeNumerator && $nodeNumerator->length == 1) {
|
||||
$value = $nodeNumerator->item(0)->nodeValue;
|
||||
if (is_numeric($value)) {
|
||||
$numerator = new Element\Numeric(floatval($value));
|
||||
} else {
|
||||
$numerator = new Element\Identifier($value);
|
||||
}
|
||||
} else {
|
||||
throw new InvalidInputException(sprintf(
|
||||
'%s : The tag `%s` has no numerator defined',
|
||||
__METHOD__,
|
||||
$nodeElement->nodeName
|
||||
));
|
||||
}
|
||||
// Denominator
|
||||
$nodeDenominator = $this->xpath->query('m:den/m:r/m:t', $nodeElement);
|
||||
if ($nodeDenominator && $nodeDenominator->length == 1) {
|
||||
$value = $nodeDenominator->item(0)->nodeValue;
|
||||
if (is_numeric($value)) {
|
||||
$denominator = new Element\Numeric(floatval($value));
|
||||
} else {
|
||||
$denominator = new Element\Identifier($value);
|
||||
}
|
||||
} else {
|
||||
throw new InvalidInputException(sprintf(
|
||||
'%s : The tag `%s` has no denominator defined',
|
||||
__METHOD__,
|
||||
$nodeElement->nodeName
|
||||
));
|
||||
}
|
||||
|
||||
return new Element\Fraction($numerator, $denominator);
|
||||
case 'm:r':
|
||||
$nodeText = $this->xpath->query('m:t', $nodeElement);
|
||||
if ($nodeText && $nodeText->length == 1) {
|
||||
$value = trim($nodeText->item(0)->nodeValue);
|
||||
if (in_array($value, $this->operators)) {
|
||||
return new Element\Operator($value);
|
||||
}
|
||||
if (is_numeric($value)) {
|
||||
return new Element\Numeric(floatval($value));
|
||||
}
|
||||
|
||||
return new Element\Identifier($value);
|
||||
}
|
||||
|
||||
throw new InvalidInputException(sprintf(
|
||||
'%s : The tag `%s` has no tag `m:t` defined',
|
||||
__METHOD__,
|
||||
$nodeElement->nodeName
|
||||
));
|
||||
case 'm:oMath':
|
||||
return new Element\Row();
|
||||
default:
|
||||
throw new NotImplementedException(sprintf(
|
||||
'%s : The tag `%s` is not implemented',
|
||||
__METHOD__,
|
||||
$nodeElement->nodeName
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
10
vendor/phpoffice/math/src/Math/Reader/ReaderInterface.php
vendored
Normal file
10
vendor/phpoffice/math/src/Math/Reader/ReaderInterface.php
vendored
Normal file
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
namespace PhpOffice\Math\Reader;
|
||||
|
||||
use PhpOffice\Math\Math;
|
||||
|
||||
interface ReaderInterface
|
||||
{
|
||||
public function read(string $content): ?Math;
|
||||
}
|
||||
131
vendor/phpoffice/math/src/Math/Writer/MathML.php
vendored
Normal file
131
vendor/phpoffice/math/src/Math/Writer/MathML.php
vendored
Normal file
@@ -0,0 +1,131 @@
|
||||
<?php
|
||||
|
||||
namespace PhpOffice\Math\Writer;
|
||||
|
||||
use PhpOffice\Math\Element;
|
||||
use PhpOffice\Math\Exception\NotImplementedException;
|
||||
use PhpOffice\Math\Math;
|
||||
use XMLWriter;
|
||||
|
||||
class MathML implements WriterInterface
|
||||
{
|
||||
/** @var XMLWriter */
|
||||
private $output;
|
||||
|
||||
/**
|
||||
* @param Math $math
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function write(Math $math): string
|
||||
{
|
||||
$this->output = new XMLWriter();
|
||||
$this->output->openMemory();
|
||||
$this->output->startDocument('1.0', 'UTF-8');
|
||||
$this->output->writeDtd('math', '-//W3C//DTD MathML 2.0//EN', 'http://www.w3.org/Math/DTD/mathml2/mathml2.dtd');
|
||||
$this->output->startElement('math');
|
||||
$this->output->writeAttribute('xmlns', 'http://www.w3.org/1998/Math/MathML');
|
||||
|
||||
foreach ($math->getElements() as $element) {
|
||||
$this->writeElementItem($element);
|
||||
}
|
||||
|
||||
$this->output->endElement();
|
||||
$this->output->endDocument();
|
||||
|
||||
return $this->output->outputMemory();
|
||||
}
|
||||
|
||||
protected function writeElementItem(Element\AbstractElement $element): void
|
||||
{
|
||||
$tagName = $this->getElementTagName($element);
|
||||
|
||||
// Element\AbstractGroupElement
|
||||
if ($element instanceof Element\AbstractGroupElement) {
|
||||
$this->output->startElement($tagName);
|
||||
foreach ($element->getElements() as $childElement) {
|
||||
$this->writeElementItem($childElement);
|
||||
}
|
||||
$this->output->endElement();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// Element\Superscript
|
||||
if ($element instanceof Element\Superscript) {
|
||||
$this->output->startElement($tagName);
|
||||
$this->writeElementItem($element->getBase());
|
||||
$this->writeElementItem($element->getSuperscript());
|
||||
$this->output->endElement();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// Element\Fraction
|
||||
if ($element instanceof Element\Fraction) {
|
||||
$this->output->startElement($tagName);
|
||||
$this->writeElementItem($element->getNumerator());
|
||||
$this->writeElementItem($element->getDenominator());
|
||||
$this->output->endElement();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if ($element instanceof Element\Identifier
|
||||
|| $element instanceof Element\Numeric
|
||||
|| $element instanceof Element\Operator) {
|
||||
$this->output->startElement($tagName);
|
||||
$this->output->text((string) $element->getValue());
|
||||
$this->output->endElement();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
/*
|
||||
throw new NotImplementedException(sprintf(
|
||||
'%s : The class `%s` is not implemented',
|
||||
__METHOD__,
|
||||
get_class($element)
|
||||
));
|
||||
*/
|
||||
}
|
||||
|
||||
protected function getElementTagName(Element\AbstractElement $element): string
|
||||
{
|
||||
// Group
|
||||
if ($element instanceof Element\Row) {
|
||||
return 'mrow';
|
||||
}
|
||||
if ($element instanceof Element\AbstractGroupElement) {
|
||||
/*
|
||||
throw new NotImplementedException(sprintf(
|
||||
'%s : The element of the class `%s` has no tag name',
|
||||
__METHOD__,
|
||||
get_class($element)
|
||||
));
|
||||
*/
|
||||
}
|
||||
|
||||
if ($element instanceof Element\Superscript) {
|
||||
return 'msup';
|
||||
}
|
||||
if ($element instanceof Element\Fraction) {
|
||||
return 'mfrac';
|
||||
}
|
||||
if ($element instanceof Element\Identifier) {
|
||||
return 'mi';
|
||||
}
|
||||
if ($element instanceof Element\Numeric) {
|
||||
return 'mn';
|
||||
}
|
||||
if ($element instanceof Element\Operator) {
|
||||
return 'mo';
|
||||
}
|
||||
|
||||
throw new NotImplementedException(sprintf(
|
||||
'%s : The element of the class `%s` has no tag name',
|
||||
__METHOD__,
|
||||
get_class($element)
|
||||
));
|
||||
}
|
||||
}
|
||||
102
vendor/phpoffice/math/src/Math/Writer/OfficeMathML.php
vendored
Normal file
102
vendor/phpoffice/math/src/Math/Writer/OfficeMathML.php
vendored
Normal file
@@ -0,0 +1,102 @@
|
||||
<?php
|
||||
|
||||
namespace PhpOffice\Math\Writer;
|
||||
|
||||
use PhpOffice\Math\Element;
|
||||
use PhpOffice\Math\Exception\NotImplementedException;
|
||||
use PhpOffice\Math\Math;
|
||||
use XMLWriter;
|
||||
|
||||
class OfficeMathML implements WriterInterface
|
||||
{
|
||||
/** @var XMLWriter */
|
||||
private $output;
|
||||
|
||||
/**
|
||||
* @param Math $math
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function write(Math $math): string
|
||||
{
|
||||
$this->output = new XMLWriter();
|
||||
$this->output->openMemory();
|
||||
$this->output->startElement('m:oMathPara');
|
||||
$this->output->writeAttribute('xmlns:m', 'http://schemas.openxmlformats.org/officeDocument/2006/math');
|
||||
$this->output->startElement('m:oMath');
|
||||
|
||||
foreach ($math->getElements() as $element) {
|
||||
$this->writeElementItem($element);
|
||||
}
|
||||
|
||||
$this->output->endElement();
|
||||
$this->output->endElement();
|
||||
|
||||
return $this->output->outputMemory();
|
||||
}
|
||||
|
||||
protected function writeElementItem(Element\AbstractElement $element): void
|
||||
{
|
||||
// Element\Row
|
||||
if ($element instanceof Element\Row) {
|
||||
foreach ($element->getElements() as $childElement) {
|
||||
$this->writeElementItem($childElement);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// Element\Fraction
|
||||
if ($element instanceof Element\Fraction) {
|
||||
$this->output->startElement($this->getElementTagName($element));
|
||||
$this->output->startElement('m:num');
|
||||
$this->writeElementItem($element->getNumerator());
|
||||
$this->output->endElement();
|
||||
$this->output->startElement('m:den');
|
||||
$this->writeElementItem($element->getDenominator());
|
||||
$this->output->endElement();
|
||||
$this->output->endElement();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if ($element instanceof Element\Identifier
|
||||
|| $element instanceof Element\Numeric
|
||||
|| $element instanceof Element\Operator) {
|
||||
$this->output->startElement('m:r');
|
||||
$this->output->startElement('m:t');
|
||||
$this->output->text((string) $element->getValue());
|
||||
$this->output->endElement();
|
||||
$this->output->endElement();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// Check if managed
|
||||
$this->getElementTagName($element);
|
||||
}
|
||||
|
||||
protected function getElementTagName(Element\AbstractElement $element): string
|
||||
{
|
||||
// Group
|
||||
if ($element instanceof Element\AbstractGroupElement) {
|
||||
/*
|
||||
throw new NotImplementedException(sprintf(
|
||||
'%s : The element of the class `%s` has no tag name',
|
||||
__METHOD__,
|
||||
get_class($element)
|
||||
));
|
||||
*/
|
||||
}
|
||||
|
||||
if ($element instanceof Element\Fraction) {
|
||||
return 'm:f';
|
||||
}
|
||||
|
||||
throw new NotImplementedException(sprintf(
|
||||
'%s : The element of the class `%s` has no tag name',
|
||||
__METHOD__,
|
||||
get_class($element)
|
||||
));
|
||||
}
|
||||
}
|
||||
10
vendor/phpoffice/math/src/Math/Writer/WriterInterface.php
vendored
Normal file
10
vendor/phpoffice/math/src/Math/Writer/WriterInterface.php
vendored
Normal file
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
namespace PhpOffice\Math\Writer;
|
||||
|
||||
use PhpOffice\Math\Math;
|
||||
|
||||
interface WriterInterface
|
||||
{
|
||||
public function write(Math $math): string;
|
||||
}
|
||||
Reference in New Issue
Block a user