Skip to content

Commit

Permalink
Merge pull request #27 from Icinga/code-quality-and-conventions
Browse files Browse the repository at this point in the history
Code quality and conventions
  • Loading branch information
lippserd authored Mar 12, 2020
2 parents 810fbdb + 1299ef4 commit 204c29b
Show file tree
Hide file tree
Showing 31 changed files with 1,974 additions and 1,238 deletions.
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
"sort-packages": true
},
"require": {
"php": ">=5.4.0",
"ipl/stdlib": ">=0.4.0",
"php": ">=5.6.0",
"ipl/stdlib": ">=0.5.0",
"ipl/validator": ">=0.1.0",
"psr/http-message": "~1.0"
},
"autoload": {
Expand Down
10 changes: 6 additions & 4 deletions doc/40-Forms.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ In addition to this, there might be:
* validators (`ipl\Validator`)
* sub forms, field sets, buttons...

Please note that default HTTP method to submit `ipl\Html\Form` with is **POST**.

Create your very first form
---------------------------

Expand All @@ -37,7 +39,7 @@ echo $form;
This outputs:

```html
<form action="/your/url"><input name="name" type="text" /></form>
<form action="/your/url" method="POST"><input name="name" type="text" /></form>
```

Select
Expand Down Expand Up @@ -155,8 +157,8 @@ use ipl\Html\Html;
$form = new Form();

echo $form
->registerElement('text', 'first_name')
->registerElement('text', 'last_name')
->registerElement($form->createElement('text', 'first_name'))
->registerElement($form->createElement('text', 'last_name'))
->add(Html::tag('div', [
$form->getElement('first_name'),
Html::tag('br'),
Expand All @@ -167,7 +169,7 @@ echo $form
The result looks as expected:

```html
<form><div>
<form method="POST"><div>
<input name="first_name" type="text" />
<br />
<input name="last_name" type="text" />
Expand Down
169 changes: 83 additions & 86 deletions src/Attribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,13 @@ class Attribute
/** @var string|array|bool|null */
protected $value;

/** @var string Glue string to join elements if the attribute's value is an array */
protected $glue = ' ';

/**
* Create a new HTML attribute from the given name and value
*
* @param string $name The name of the attribute
* @param string|bool|array|null $value The value of the attribute
* @param string $name The name of the attribute
* @param string|bool|array|null $value The value of the attribute
*
* @throws InvalidArgumentException If the name of the attribute contains special characters
* @throws InvalidArgumentException If the name of the attribute contains special characters
*/
public function __construct($name, $value = null)
{
Expand All @@ -40,12 +37,12 @@ public function __construct($name, $value = null)
/**
* Create a new HTML attribute from the given name and value
*
* @param string $name The name of the attribute
* @param string|bool|array|null $value The value of the attribute
* @param string $name The name of the attribute
* @param string|bool|array|null $value The value of the attribute
*
* @return static
* @return static
*
* @throws InvalidArgumentException If the name of the attribute contains special characters
* @throws InvalidArgumentException If the name of the attribute contains special characters
*/
public static function create($name, $value)
{
Expand All @@ -57,21 +54,77 @@ public static function create($name, $value)
*
* The value of the attribute will be null after construction.
*
* @param string $name The name of the attribute
* @param string $name The name of the attribute
*
* @return static
* @return static
*
* @throws InvalidArgumentException If the name of the attribute contains special characters
* @throws InvalidArgumentException If the name of the attribute contains special characters
*/
public static function createEmpty($name)
{
return new static($name, null);
}

/**
* Escape the name of an attribute
*
* Makes sure that the name of an attribute really is a string.
*
* @param string $name
*
* @return string
*/
public static function escapeName($name)
{
return (string) $name;
}

/**
* Escape the value of an attribute
*
* If the value is an array, returns the string representation
* of all array elements joined with the specified glue string.
*
* Values are escaped according to the HTML5 double-quoted attribute value syntax:
* {@link https://html.spec.whatwg.org/multipage/syntax.html#attributes-2 }.
*
* @param string|array $value
* @param string $glue Glue string to join elements if value is an array
*
* @return string
*/
public static function escapeValue($value, $glue = ' ')
{
if (is_array($value)) {
$value = implode($glue, $value);
}

// We force double-quoted attribute value syntax so let's start by escaping double quotes
$value = str_replace('"', '&quot;', $value);

// In addition, values must not contain ambiguous ampersands
$value = preg_replace_callback(
'/&[0-9A-Z]+;/i',
function ($match) {
$subject = $match[0];

if (htmlspecialchars_decode($subject, ENT_COMPAT | ENT_HTML5) === $subject) {
// Ambiguous ampersand
return str_replace('&', '&amp;', $subject);
}

return $subject;
},
$value
);

return $value;
}

/**
* Get the name of the attribute
*
* @return string
* @return string
*/
public function getName()
{
Expand All @@ -81,11 +134,11 @@ public function getName()
/**
* Set the name of the attribute
*
* @param string $name
* @param string $name
*
* @return $this
* @return $this
*
* @throws InvalidArgumentException If the name contains special characters
* @throws InvalidArgumentException If the name contains special characters
*/
protected function setName($name)
{
Expand All @@ -104,7 +157,7 @@ protected function setName($name)
/**
* Get the value of the attribute
*
* @return string|bool|array|null
* @return string|bool|array|null
*/
public function getValue()
{
Expand All @@ -114,9 +167,9 @@ public function getValue()
/**
* Set the value of the attribute
*
* @param string|bool|array|null $value
* @param string|bool|array|null $value
*
* @return $this
* @return $this
*/
public function setValue($value)
{
Expand All @@ -128,9 +181,9 @@ public function setValue($value)
/**
* Add the given value(s) to the attribute
*
* @param string|array $value The value(s) to add
* @param string|array $value The value(s) to add
*
* @return $this
* @return $this
*/
public function addValue($value)
{
Expand All @@ -150,9 +203,9 @@ public function addValue($value)
*
* Does nothing if there is no such value to remove.
*
* @param string|array $value The value(s) to remove
* @param string|array $value The value(s) to remove
*
* @return $this
* @return $this
*/
public function removeValue($value)
{
Expand All @@ -172,7 +225,7 @@ public function removeValue($value)
/**
* Test and return true if the attribute is boolean, false otherwise
*
* @return bool
* @return bool
*/
public function isBoolean()
{
Expand All @@ -184,7 +237,7 @@ public function isBoolean()
*
* Null and the empty array will be considered empty.
*
* @return bool
* @return bool
*/
public function isEmpty()
{
Expand All @@ -203,7 +256,7 @@ public function isEmpty()
*
* Escaping of the attribute's value takes place automatically using {@link Attribute::escapeValue()}.
*
* @return string
* @return string
*/
public function render()
{
Expand All @@ -229,7 +282,7 @@ public function render()
/**
* Render the name of the attribute to HTML
*
* @return string
* @return string
*/
public function renderName()
{
Expand All @@ -239,66 +292,10 @@ public function renderName()
/**
* Render the value of the attribute to HTML
*
* @return string
* @return string
*/
public function renderValue()
{
return static::escapeValue($this->value, $this->glue);
}

/**
* Escape the name of an attribute
*
* Makes sure that the name of an attribute really is a string.
*
* @param string $name
*
* @return string
*/
public static function escapeName($name)
{
return (string) $name;
}

/**
* Escape the value of an attribute
*
* If the value is an array, returns the string representation
* of all array elements joined with the specified glue string.
*
* Values are escaped according to the HTML5 double-quoted attribute value syntax:
* {@link https://html.spec.whatwg.org/multipage/syntax.html#attributes-2 }.
*
* @param string|array $value
* @param string $glue Glue string to join elements if value is an array
*
* @return string
*/
public static function escapeValue($value, $glue = ' ')
{
if (is_array($value)) {
$value = implode($glue, $value);
}

// We force double-quoted attribute value syntax so let's start by escaping double quotes
$value = str_replace('"', '&quot;', $value);

// In addition, values must not contain ambiguous ampersands
$value = preg_replace_callback(
'/&[0-9A-Z]+;/i',
function ($match) {
$subject = $match[0];

if (htmlspecialchars_decode($subject, ENT_COMPAT | ENT_HTML5) === $subject) {
// Ambiguous ampersand
return str_replace('&', '&amp;', $subject);
}

return $subject;
},
$value
);

return $value;
return static::escapeValue($this->value);
}
}
Loading

0 comments on commit 204c29b

Please sign in to comment.