Skip to content

Commit

Permalink
Declare strict types
Browse files Browse the repository at this point in the history
  • Loading branch information
campbell-m committed Jun 1, 2024
1 parent 01063a7 commit 529d072
Show file tree
Hide file tree
Showing 53 changed files with 218 additions and 203 deletions.
21 changes: 18 additions & 3 deletions web/lib/MRBS/Form/Element.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php

declare(strict_types=1);
namespace MRBS\Form;

// This is a limited implementation of the HTML5 DOM and is optimised for use by
Expand Down Expand Up @@ -397,7 +397,17 @@ public function toHTML($no_whitespace=false)
if (isset($value) && ($value !== true))
{
// boolean attributes, eg 'required', don't need a value
$html .= '="' . htmlspecialchars($value) . '"';
$html .= '="';
if (is_int($value))
{
// No need to escape these
$html .= $value;
}
else
{
$html .= htmlspecialchars($value);
}
$html .= '"';
}
}

Expand Down Expand Up @@ -448,7 +458,12 @@ public function toHTML($no_whitespace=false)

private static function escapeText($text, $raw=false)
{
return ($raw) ? $text : htmlspecialchars($text);
if ($raw || is_int($text))
{
return $text;
}

return htmlspecialchars($text);
}

}
8 changes: 4 additions & 4 deletions web/lib/MRBS/Form/ElementA.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<?php

declare(strict_types=1);
namespace MRBS\Form;

class ElementA extends Element
{

public function __construct()
{
parent::__construct('a');
}
}

}
8 changes: 4 additions & 4 deletions web/lib/MRBS/Form/ElementButton.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<?php

declare(strict_types=1);
namespace MRBS\Form;

class ElementButton extends Element
{

public function __construct()
{
parent::__construct('button');
}
}

}
6 changes: 3 additions & 3 deletions web/lib/MRBS/Form/ElementDatalist.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php

declare(strict_types=1);
namespace MRBS\Form;

class ElementDatalist extends Element
Expand All @@ -9,5 +9,5 @@ public function __construct()
{
parent::__construct('datalist');
}
}

}
6 changes: 3 additions & 3 deletions web/lib/MRBS/Form/ElementDiv.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php

declare(strict_types=1);
namespace MRBS\Form;

class ElementDiv extends Element
Expand All @@ -9,5 +9,5 @@ public function __construct()
{
parent::__construct('div');
}
}

}
16 changes: 8 additions & 8 deletions web/lib/MRBS/Form/ElementFieldset.php
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
<?php

declare(strict_types=1);
namespace MRBS\Form;


class ElementFieldset extends Element
{

public function __construct()
{
parent::__construct('fieldset');
}


// $legend can be
// (a) an ElementLegend object, or
// (b) another element, or
Expand All @@ -27,7 +27,7 @@ public function addLegend($legend)
else
{
$element = new ElementLegend();

if (is_string($legend))
{
$element->setText($legend);
Expand All @@ -38,9 +38,9 @@ public function addLegend($legend)
$element->addElement($legend);
}
}

$this->addElement($element);
return $this;
}
}

}
8 changes: 4 additions & 4 deletions web/lib/MRBS/Form/ElementImg.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<?php

declare(strict_types=1);
namespace MRBS\Form;

class ElementImg extends Element
{

public function __construct()
{
parent::__construct('img', true);
}
}

}
8 changes: 4 additions & 4 deletions web/lib/MRBS/Form/ElementInput.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<?php

declare(strict_types=1);
namespace MRBS\Form;

abstract class ElementInput extends Element
{

public function __construct()
{
parent::__construct('input', $self_closing=true);
$this->setAttribute('type', 'text');
}
}

}
12 changes: 6 additions & 6 deletions web/lib/MRBS/Form/ElementInputCheckbox.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php

declare(strict_types=1);
namespace MRBS\Form;

class ElementInputCheckbox extends ElementInput
Expand All @@ -10,8 +10,8 @@ public function __construct()
parent::__construct();
$this->setAttribute('type', 'checkbox');
}


public function setChecked($checked=true)
{
if ($checked)
Expand All @@ -22,8 +22,8 @@ public function setChecked($checked=true)
{
$this->removeAttribute('checked');
}

return $this;
}
}

}
26 changes: 13 additions & 13 deletions web/lib/MRBS/Form/ElementInputDatalist.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?php

declare(strict_types=1);
namespace MRBS\Form;

class ElementInputDatalist extends ElementInput
{
private static $list_prefix = "mrbs_";
private static $list_number = 1;

public function __construct()
{
// One problem with using a datalist with an input element is the way different browsers
Expand All @@ -20,34 +20,34 @@ public function __construct()
// tied to the visible input, but this isn't as important for a text input as it is, say, for a checkbox
// or radio button.
parent::__construct();

// Provide a unique id to link the list with the input.
// Doesn't matter what it is as it won't be used elsewhere.
$list_id = self::$list_prefix . self::$list_number;
self::$list_number++;

$this->setAttributes(array('type' => 'text',
'list' => $list_id));

$datalist = new ElementDatalist();
$datalist->setAttribute('id', $list_id);

$this->next($datalist);
}


public function addDatalistOptions(array $options, $associative=null)
{
{
// Put a <select> wrapper around the options so that browsers that don't
// support <datalist> will still have the options in their DOM and then
// the JavaScript polyfill can find them and do something with them
$select = new ElementSelect();
$select->addClass('none');
$select->addSelectOptions($options, null, $associative);

$this->next($this->next()->addElement($select));

return $this;
}
}

}
6 changes: 3 additions & 3 deletions web/lib/MRBS/Form/ElementInputDate.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php

declare(strict_types=1);
namespace MRBS\Form;

class ElementInputDate extends ElementInput
Expand All @@ -10,5 +10,5 @@ public function __construct()
parent::__construct();
$this->setAttribute('type', 'date');
}
}

}
6 changes: 3 additions & 3 deletions web/lib/MRBS/Form/ElementInputEmail.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php

declare(strict_types=1);
namespace MRBS\Form;

class ElementInputEmail extends ElementInput
Expand All @@ -10,5 +10,5 @@ public function __construct()
parent::__construct();
$this->setAttribute('type', 'email');
}
}

}
6 changes: 3 additions & 3 deletions web/lib/MRBS/Form/ElementInputFile.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php

declare(strict_types=1);
namespace MRBS\Form;

class ElementInputFile extends ElementInput
Expand All @@ -10,5 +10,5 @@ public function __construct()
parent::__construct();
$this->setAttribute('type', 'file');
}
}

}
2 changes: 1 addition & 1 deletion web/lib/MRBS/Form/ElementInputHidden.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php

declare(strict_types=1);
namespace MRBS\Form;

class ElementInputHidden extends ElementInput
Expand Down
6 changes: 3 additions & 3 deletions web/lib/MRBS/Form/ElementInputImage.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php

declare(strict_types=1);
namespace MRBS\Form;

class ElementInputImage extends ElementInput
Expand All @@ -10,5 +10,5 @@ public function __construct()
parent::__construct();
$this->setAttribute('type', 'image');
}
}

}
6 changes: 3 additions & 3 deletions web/lib/MRBS/Form/ElementInputNumber.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php

declare(strict_types=1);
namespace MRBS\Form;

class ElementInputNumber extends ElementInput
Expand All @@ -11,5 +11,5 @@ public function __construct()
$this->setAttributes(array('type' => 'number',
'step' => '1'));
}
}

}
6 changes: 3 additions & 3 deletions web/lib/MRBS/Form/ElementInputPassword.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php

declare(strict_types=1);
namespace MRBS\Form;

class ElementInputPassword extends ElementInput
Expand All @@ -10,5 +10,5 @@ public function __construct()
parent::__construct();
$this->setAttribute('type', 'password');
}
}

}
Loading

0 comments on commit 529d072

Please sign in to comment.