/** * get or set CSS values in style attributes * * @param string|array $property * @param NULL|string|object Closure $value * @return string|object FluentDOMStyle */ public function css($property, $value = NULL) { if (is_string($property) && is_null($value)) { try { $firstNode = $this->_getContentElement($this->_array); $properties = new FluentDOMCssProperties($firstNode->getAttribute('style')); if (isset($properties[$property])) { return $properties[$property]; } } catch (UnexpectedValueException $e) { } return NULL; } elseif (is_string($property)) { $propertyList = array($property => $value); } elseif (is_array($property) || $property instanceof Traversable) { $propertyList = $property; } else { throw new InvalidArgumentException('Invalid css property name argument type.'); } //set list of properties to all elements foreach ($this->_array as $index => $node) { if ($node instanceof DOMElement) { $properties = new FluentDOMCssProperties($node->getAttribute('style')); foreach ($propertyList as $name => $value) { $properties[$name] = $properties->compileValue($value, $node, $index, isset($properties[$name]) ? $properties[$name] : NULL); } if (count($properties) > 0) { $node->setAttribute('style', (string) $properties); } elseif ($node->hasAttribute('style')) { $node->removeAttribute('style'); } } } }
/** * Get an iterator for the properties * * @see IteratorAggregate::getIterator() * @return ArrayIterator */ public function getIterator() { if (isset($this->_fd[0]) && $this->_fd[0] instanceof DOMElement) { $properties = new FluentDOMCssProperties($this->_fd[0]->getAttribute('style')); return $properties->getIterator(); } return new ArrayIterator(array()); }
/** * @covers FluentDOMCssProperties::compileValue */ public function testCompileValueWithCallback() { $dom = new DOMDocument(); $dom->appendChild($dom->createElement('sample')); $css = new FluentDOMCssProperties(''); $this->assertSame('success', $css->compileValue(array($this, 'callbackForCompileValue'), $dom->documentElement, 23, 'success')); }