public static function filterAttributes(Tag $tag, array $tagConfig, array $registeredVars, Logger $logger) { if (empty($tagConfig['attributes'])) { $tag->setAttributes(array()); return \true; } foreach ($tagConfig['attributes'] as $attrName => $attrConfig) { if (isset($attrConfig['generator'])) { $tag->setAttribute($attrName, self::executeFilter($attrConfig['generator'], array('attrName' => $attrName, 'logger' => $logger, 'registeredVars' => $registeredVars))); } } foreach ($tag->getAttributes() as $attrName => $attrValue) { if (!isset($tagConfig['attributes'][$attrName])) { $tag->removeAttribute($attrName); continue; } $attrConfig = $tagConfig['attributes'][$attrName]; if (!isset($attrConfig['filterChain'])) { continue; } $logger->setAttribute($attrName); foreach ($attrConfig['filterChain'] as $filter) { $attrValue = self::executeFilter($filter, array('attrName' => $attrName, 'attrValue' => $attrValue, 'logger' => $logger, 'registeredVars' => $registeredVars)); if ($attrValue === \false) { $tag->removeAttribute($attrName); break; } } if ($attrValue !== \false) { $tag->setAttribute($attrName, $attrValue); } $logger->unsetAttribute(); } foreach ($tagConfig['attributes'] as $attrName => $attrConfig) { if (!$tag->hasAttribute($attrName)) { if (isset($attrConfig['defaultValue'])) { $tag->setAttribute($attrName, $attrConfig['defaultValue']); } elseif (!empty($attrConfig['required'])) { return \false; } } } return \true; }
/** * @testdox removeAttribute('foo') unsets attribute 'foo' */ public function testRemoveAttribute() { $tag = new Tag(Tag::START_TAG, 'X', 0, 0); $tag->setAttribute('foo', 'bar'); $tag->setAttribute('baz', 'quux'); $tag->removeAttribute('foo'); $this->assertSame(['baz' => 'quux'], $tag->getAttributes()); }
/** * Filter the attributes of given tag * * @private * * @param Tag $tag Tag being checked * @param array $tagConfig Tag's config * @param array $registeredVars Array of registered vars for use in attribute filters * @param Logger $logger This parser's Logger instance * @return bool Whether the whole attribute set is valid */ public static function filterAttributes(Tag $tag, array $tagConfig, array $registeredVars, Logger $logger) { if (empty($tagConfig['attributes'])) { $tag->setAttributes([]); return true; } // Generate values for attributes with a generator set foreach ($tagConfig['attributes'] as $attrName => $attrConfig) { if (isset($attrConfig['generator'])) { $tag->setAttribute($attrName, self::executeFilter($attrConfig['generator'], ['attrName' => $attrName, 'logger' => $logger, 'registeredVars' => $registeredVars])); } } // Filter and remove invalid attributes foreach ($tag->getAttributes() as $attrName => $attrValue) { // Test whether this attribute exists and remove it if it doesn't if (!isset($tagConfig['attributes'][$attrName])) { $tag->removeAttribute($attrName); continue; } $attrConfig = $tagConfig['attributes'][$attrName]; // Test whether this attribute has a filterChain if (!isset($attrConfig['filterChain'])) { continue; } // Record the name of the attribute being filtered into the logger $logger->setAttribute($attrName); foreach ($attrConfig['filterChain'] as $filter) { $attrValue = self::executeFilter($filter, ['attrName' => $attrName, 'attrValue' => $attrValue, 'logger' => $logger, 'registeredVars' => $registeredVars]); if ($attrValue === false) { $tag->removeAttribute($attrName); break; } } // Update the attribute value if it's valid if ($attrValue !== false) { $tag->setAttribute($attrName, $attrValue); } // Remove the attribute's name from the logger $logger->unsetAttribute(); } // Iterate over the attribute definitions to handle missing attributes foreach ($tagConfig['attributes'] as $attrName => $attrConfig) { // Test whether this attribute is missing if (!$tag->hasAttribute($attrName)) { if (isset($attrConfig['defaultValue'])) { // Use the attribute's default value $tag->setAttribute($attrName, $attrConfig['defaultValue']); } elseif (!empty($attrConfig['required'])) { // This attribute is missing, has no default value and is required, which means // the attribute set is invalid return false; } } } return true; }