3 namespace RemexHtml\Tokenizer;
6 * An Attributes implementation which defers interpretation of regex match
7 * results until the caller requires them.
9 * This should not be directly instantiated outside of Tokenizer.
11 class LazyAttributes implements Attributes {
17 public function __construct( $data, callable $interpreter ) {
18 $this->interpreter = $interpreter;
22 private function init() {
23 if ( $this->attributes === null ) {
24 $func = $this->interpreter;
25 $this->attributes = $func( $this->data );
26 $this->interpreter = null;
30 public function offsetExists( $offset ) {
31 if ( $this->attributes === null ) {
34 return isset( $this->attributes[$offset] );
37 public function &offsetGet( $offset ) {
38 if ( $this->attributes === null ) {
41 return $this->attributes[$offset];
44 public function offsetSet( $offset, $value ) {
45 if ( $this->attributes === null ) {
48 $this->attributes[$offset] = $value;
51 public function offsetUnset( $offset ) {
52 if ( $this->attributes === null ) {
55 unset( $this->attributes[$offset] );
58 public function getValues() {
59 if ( $this->attributes === null ) {
62 return $this->attributes;
65 public function getObjects() {
66 if ( $this->attrObjects === null ) {
67 if ( $this->attributes === null ) {
71 foreach ( $this->attributes as $name => $value ) {
72 $result[$name] = new Attribute( $name, null, null, $name, $value );
74 $this->attrObjects = $result;
76 return $this->attrObjects;
79 public function count() {
80 return is_object( $this->data ) ? $this->data->count() : count( $this->data );
83 public function getIterator() {
84 if ( $this->attributes === null ) {
87 return new \ArrayIterator( $this->attributes );
90 public function merge( Attributes $other ) {
91 if ( $this->attributes === null ) {
94 foreach ( $other as $name => $value ) {
95 if ( !isset( $this->attributes[$name] ) ) {
96 $this->attributes[$name] = $value;