Skip to content

Commit

Permalink
MediaEmbed: moved attribute creation out of add()
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshyPHP committed Dec 9, 2017
1 parent 805b1ae commit 1e56d2a
Showing 1 changed file with 52 additions and 40 deletions.
92 changes: 52 additions & 40 deletions src/Plugins/MediaEmbed/Configurator.php
Expand Up @@ -11,7 +11,6 @@
use RuntimeException;
use s9e\TextFormatter\Configurator\Helpers\RegexpBuilder;
use s9e\TextFormatter\Configurator\Items\Attribute;
use s9e\TextFormatter\Configurator\Items\AttributeFilters\RegexpFilter;
use s9e\TextFormatter\Configurator\Items\AttributePreprocessor;
use s9e\TextFormatter\Configurator\Items\Tag;
use s9e\TextFormatter\Plugins\ConfiguratorBase;
Expand Down Expand Up @@ -211,45 +210,7 @@ public function add($siteId, array $siteConfig = null)
$hasRequiredAttribute = false;
foreach ($attributes as $attrName => $attrConfig)
{
$attribute = $tag->attributes->add($attrName);

if (isset($attrConfig['preFilter']))
{
$this->appendFilter($attribute, $attrConfig['preFilter']);
}

// Add a filter depending on the attribute's type or regexp
if (isset($attrConfig['type']))
{
// If "type" is "url", get the "#url" filter
$filter = $this->configurator->attributeFilters['#' . $attrConfig['type']];
$attribute->filterChain->append($filter);
}
elseif (isset($attrConfig['regexp']))
{
$attribute->filterChain->append(new RegexpFilter($attrConfig['regexp']));
}

if (isset($attrConfig['required']))
{
$attribute->required = $attrConfig['required'];
}
else
{
// Non-id attributes are marked as optional
$attribute->required = ($attrName === 'id');
}

if (isset($attrConfig['postFilter']))
{
$this->appendFilter($attribute, $attrConfig['postFilter']);
}

if (isset($attrConfig['defaultValue']))
{
$attribute->defaultValue = $attrConfig['defaultValue'];
}

$attribute = $this->addAttribute($tag, $attrName, $attrConfig);
$hasRequiredAttribute |= $attribute->required;
}

Expand Down Expand Up @@ -315,6 +276,57 @@ public function appendTemplate($template = '')
// Internal methods
//==========================================================================

/**
* Add an attribute to given tag
*
* @param Tag $tag
* @param string $attrName
* @param array $attrConfig
* @return Attribute
*/
protected function addAttribute(Tag $tag, $attrName, array $attrConfig)
{
$attribute = $tag->attributes->add($attrName);
if (isset($attrConfig['preFilter']))
{
$this->appendFilter($attribute, $attrConfig['preFilter']);
}

// Add a filter depending on the attribute's type or regexp
if (isset($attrConfig['type']))
{
// If "type" is "url", get the "#url" filter
$filter = $this->configurator->attributeFilters['#' . $attrConfig['type']];
$attribute->filterChain->append($filter);
}
elseif (isset($attrConfig['regexp']))
{
$attribute->filterChain->append('#regexp')->setRegexp($attrConfig['regexp']);
}

if (isset($attrConfig['required']))
{
$attribute->required = $attrConfig['required'];
}
else
{
// Non-id attributes are marked as optional
$attribute->required = ($attrName === 'id');
}

if (isset($attrConfig['postFilter']))
{
$this->appendFilter($attribute, $attrConfig['postFilter']);
}

if (isset($attrConfig['defaultValue']))
{
$attribute->defaultValue = $attrConfig['defaultValue'];
}

return $attribute;
}

/**
* Add the defined scrapes to given tag
*
Expand Down

0 comments on commit 1e56d2a

Please sign in to comment.