Skip to content
This repository has been archived by the owner on Apr 20, 2023. It is now read-only.

Added a new h4 element #354

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
131 changes: 131 additions & 0 deletions src/Facebook/InstantArticles/Elements/H4.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
<?php
/**
* Copyright (c) 2016-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the license found in the
* LICENSE file in the root directory of this source tree.
*/
namespace Facebook\InstantArticles\Elements;

use Facebook\InstantArticles\Validators\Type;

/**
* Title for the Document
*
* Example:
* <h4> This is the h4 element Instant Article</h4>
* or
* <h4> This is a <b> h4 element </b> in the Instant Article</h4>
*/
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;
}
}