From 1bc13d9693715f9ce2d4e733a82be131f45d6682 Mon Sep 17 00:00:00 2001 From: prilvesh Date: Sat, 1 Aug 2020 02:15:27 +1200 Subject: [PATCH] Added a new h4 element Added a new h4 element in the elements folder --- src/Facebook/InstantArticles/Elements/H4.php | 131 +++++++++++++++++++ 1 file changed, 131 insertions(+) create mode 100644 src/Facebook/InstantArticles/Elements/H4.php diff --git a/src/Facebook/InstantArticles/Elements/H4.php b/src/Facebook/InstantArticles/Elements/H4.php new file mode 100644 index 00000000..9a523be5 --- /dev/null +++ b/src/Facebook/InstantArticles/Elements/H4.php @@ -0,0 +1,131 @@ + This is the h4 element Instant Article + * or + *

This is a h4 element in the Instant Article

+ */ +class H4 extends TextContainer +{ + /** + * @var string text align. Values: "op-left"|"op-center"|"op-right" + */ + private $textAlignment; + + /** + * @var string text position. Values: "op-vertical-below"|"op-vertical-above"|"op-vertical-center" + */ + private $position; + + private function __construct() + { + } + + public static function create() + { + return new self(); + } + + + /** + * The Text alignment that will be used. + * + * @see Caption::ALIGN_RIGHT + * @see Caption::ALIGN_LEFT + * @see Caption::ALIGN_CENTER + * + * @param string $text_alignment option that will be used. + */ + public function withTextAlignment($text_alignment) + { + Type::enforceWithin( + $text_alignment, + [ + Caption::ALIGN_RIGHT, + Caption::ALIGN_LEFT, + Caption::ALIGN_CENTER + ] + ); + $this->textAlignment = $text_alignment; + + return $this; + } + + /** + * @deprecated + * + * @param string $position + * @return $this + */ + public function withPostion($position) + { + return $this->withPosition($position); + } + + /** + * The Text position that will be used. + * + * @see Caption::POSITION_ABOVE + * @see Caption::POSITION_BELOW + * @see Caption::POSITION_CENTER + * + * @param string $position + * @return $this + */ + public function withPosition($position) + { + Type::enforceWithin( + $position, + [ + Caption::POSITION_ABOVE, + Caption::POSITION_BELOW, + Caption::POSITION_CENTER + ] + ); + $this->position = $position; + + return $this; + } + + /** + * Structure and create the H4 in a DOMElement. + * + * @param \DOMDocument $document - The document where this element will be appended (optional). + */ + public function toDOMElement($document = null) + { + if (!$document) { + $document = new \DOMDocument(); + } + + $h4 = $document->createElement('h4'); + + $classes = []; + if ($this->position) { + $classes[] = $this->position; + } + if ($this->textAlignment) { + $classes[] = $this->textAlignment; + } + if (!empty($classes)) { + $h4->setAttribute('class', implode(' ', $classes)); + } + + $h4->appendChild($this->textToDOMDocumentFragment($document)); + + return $h4; + } +}