Hoa central
Public Member Functions | Protected Attributes | List of all members
Hoa\Xml\Attribute Class Reference

Public Member Functions

 __construct ($string=null)
 
 readAttributes ()
 
 readAttribute ($name)
 
 attributeExists ($name)
 
 readAttributesAsList ()
 
 readAttributeAsList ($name)
 
 writeAttributes (Array $attributes)
 
 writeAttribute ($name, $value)
 
 removeAttribute ($name)
 
 __toString ()
 

Protected Attributes

 $_attributes = []
 

Detailed Description

Class .

Parse and manipulate attributes.

Definition at line 47 of file Attribute.php.

Constructor & Destructor Documentation

Hoa\Xml\Attribute::__construct (   $string = null)

Parse a string as attributes.

Parameters
string$stringString that might represent attributes.
Returns
void
Exceptions

Definition at line 66 of file Attribute.php.

67  {
68  if (null === $string) {
69  return;
70  }
71 
72  $out = preg_match_all(
73  '#(\w+)\s*(=\s*(?<!\\\)(?:("|\')|)(?(3)(.*?)(?<!\\\)\3|(\w+))\s*)?#',
74  trim($string),
75  $attributes,
76  PREG_SET_ORDER
77  );
78 
79  if (0 === $out) {
80  throw new Exception(
81  'The string %s does not represent attributes.',
82  0,
83  $string
84  );
85  }
86 
87  foreach ($attributes as $i => $attribute) {
88  if (!isset($attribute[2])) {
89  // Boolean: abc
90  $this->_attributes[$attribute[1]] = $attribute[1];
91  } elseif (!isset($attribute[5])) {
92  // Quote: abc="def" or abc='def'
93  $this->_attributes[$attribute[1]] = str_replace(
94  '\\' . $attribute[3],
95  $attribute[3],
96  $attribute[4]
97  );
98  } else {
99  // No-quote: abc=def
100  $this->_attributes[$attribute[1]] = $attribute[5];
101  }
102  }
103  }

Member Function Documentation

Hoa\Xml\Attribute::__toString ( )

Format attributes.

Returns
string

Definition at line 217 of file Attribute.php.

218  {
219  $out = null;
220 
221  foreach ($this->_attributes as $key => $value) {
222  $out .= $key . '="' . str_replace('"', '\"', $value) . '" ';
223  }
224 
225  return substr($out, 0, -1);
226  }
Hoa\Xml\Attribute::attributeExists (   $name)

Whether an attribute exists.

Parameters
string$nameAttribute's name.
Returns
bool

Definition at line 136 of file Attribute.php.

137  {
138  return true === array_key_exists($name, $this->_attributes);
139  }

Here is the caller graph for this function:

Hoa\Xml\Attribute::readAttribute (   $name)

Read a specific attribute.

Parameters
string$nameAttribute's name.
Returns
string

Definition at line 121 of file Attribute.php.

122  {
123  if (false === $this->attributeExists($name)) {
124  return null;
125  }
126 
127  return $this->_attributes[$name];
128  }
attributeExists($name)
Definition: Attribute.php:136

Here is the call graph for this function:

Here is the caller graph for this function:

Hoa\Xml\Attribute::readAttributeAsList (   $name)

Read a attribute value as a list.

Parameters
string$nameAttribute's name.
Returns
array

Definition at line 163 of file Attribute.php.

164  {
165  return explode(' ', $this->readAttribute($name));
166  }
readAttribute($name)
Definition: Attribute.php:121

Here is the call graph for this function:

Hoa\Xml\Attribute::readAttributes ( )

Read all attributes.

Returns
array

Definition at line 110 of file Attribute.php.

111  {
112  return $this->_attributes;
113  }
Hoa\Xml\Attribute::readAttributesAsList ( )

Read attributes value as a list.

Returns
array

Definition at line 146 of file Attribute.php.

147  {
148  $attributes = $this->_attributes;
149 
150  foreach ($attributes as $name => &$value) {
151  $value = explode(' ', $value);
152  }
153 
154  return $attributes;
155  }
Hoa\Xml\Attribute::removeAttribute (   $name)

Remove an attribute.

Parameters
string$nameName.
Returns
void

Definition at line 205 of file Attribute.php.

206  {
207  unset($this->_attributes[$name]);
208 
209  return;
210  }
Hoa\Xml\Attribute::writeAttribute (   $name,
  $value 
)

Write an attribute. If the attribute does not exist, it will be created.

Parameters
string$nameName.
string$valueValue.
Returns
void

Definition at line 192 of file Attribute.php.

193  {
194  $this->_attributes[$name] = $value;
195 
196  return;
197  }

Here is the caller graph for this function:

Hoa\Xml\Attribute::writeAttributes ( Array  $attributes)

Write attributes. If an attribute does not exist, it will be created.

Parameters
array$attributesAttributes.
Returns
void

Definition at line 175 of file Attribute.php.

176  {
177  foreach ($attributes as $name => $value) {
178  $this->writeAttribute($name, $value);
179  }
180 
181  return;
182  }
writeAttribute($name, $value)
Definition: Attribute.php:192

Here is the call graph for this function:

Member Data Documentation

Hoa\Xml\Attribute::$_attributes = []
protected

Definition at line 54 of file Attribute.php.


The documentation for this class was generated from the following file: