Replacing shortcode implementation
This commit is contained in:
87
classes/ProcessedShortcode.php
Normal file
87
classes/ProcessedShortcode.php
Normal file
@ -0,0 +1,87 @@
|
||||
<?php namespace Sensory5\Shortcode\Classes;
|
||||
|
||||
class ProcessedShortcode implements ShortcodeInterface
|
||||
{
|
||||
|
||||
private $attributes;
|
||||
private $content;
|
||||
private $name;
|
||||
|
||||
private function __construct()
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Create from shortcode data
|
||||
*/
|
||||
public static function create($attributes, $content, $tagName)
|
||||
{
|
||||
|
||||
$self = new self();
|
||||
$self->attributes = $attributes;
|
||||
$self->content = $content;
|
||||
$self->name = $tagName;
|
||||
|
||||
return $self;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns new instance of given shortcode with changed content
|
||||
*
|
||||
* @param string $content
|
||||
*
|
||||
* @return self
|
||||
*/
|
||||
public function withContent($content)
|
||||
{
|
||||
$self = clone $this;
|
||||
return $self->content = $content;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns shortcode name
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getName()
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns associative array(name => value) of shortcode parameters
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getParameters()
|
||||
{
|
||||
return $this->attributes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns parameter value using its name, will return null for parameter
|
||||
* without value
|
||||
*
|
||||
* @param string $name Parameter name
|
||||
* @param null $default Value returned if there is no parameter with given name
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function getParameter($name, $default = null)
|
||||
{
|
||||
return array_get($this->attributes, $name, $default);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns shortcode content (data between opening and closing tag). Null
|
||||
* means that shortcode had no content (was self closing), do not confuse
|
||||
* that with empty string (hint: use strict comparison operator ===).
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
public function getContent()
|
||||
{
|
||||
return $this->content;
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user