Skip to content

Commit

Permalink
Changed return type to static instead of the concrete class name
Browse files Browse the repository at this point in the history
  • Loading branch information
JPustkuchen committed Jul 22, 2021
1 parent 13aa36f commit 2a55579
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 62 deletions.
31 changes: 15 additions & 16 deletions src/Elements/HtmlAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function __construct(array $attributes = []) {
/**
* Returns attribute by name.
*
* @param string] $name
* @param string $name
*/
public function getAttribute(string $name) {
return $this->offsetGet($name);
Expand All @@ -48,22 +48,21 @@ public function getAttributes(): array {
* Sets an attribute.
*
* @param string $name
* @param mixed] $value
* @return HtmlAttributes
* @param mixed $value
* @return static
*/
public function setAttribute(string $name, $value): HtmlAttributes {
$this
->offsetSet($name, $value);
public function setAttribute(string $name, $value): static {
$this->offsetSet($name, $value);
return $this;
}

/**
* Removes an attribute.
*
* @param string $name
* @return HtmlAttributes
* @return static
*/
public function removeAttribute(string $name): HtmlAttributes {
public function removeAttribute(string $name): static {
$this->offsetUnset($name);
return $this;
}
Expand Down Expand Up @@ -91,11 +90,11 @@ public function getClasses(): array {
* Adds all classes from array.
*
* @param array $classes
* @return HtmlAttributes
* @return static
*/
public function addClassesFromArray(array $classes): HtmlAttributes{
if(!empty($classes)){
foreach($classes as $class){
public function addClassesFromArray(array $classes): static {
if (!empty($classes)) {
foreach ($classes as $class) {
$this->addClass($class);
}
}
Expand All @@ -107,9 +106,9 @@ public function addClassesFromArray(array $classes): HtmlAttributes{
* Adds a class.
*
* @param string $class
* @return HtmlAttributes
* @return static
*/
public function addClass(string $class): HtmlAttributes {
public function addClass(string $class): static {
$classes = $this->getClasses();
if (empty($classes)) {
$classes = [];
Expand All @@ -124,9 +123,9 @@ public function addClass(string $class): HtmlAttributes {
* Removes a class.
*
* @param string $class
* @return HtmlAttributes
* @return static
*/
public function removeClass(string $class): HtmlAttributes {
public function removeClass(string $class): static {
if ($this->hasClass($class)) {
$classes = $this->getClasses();
unset($classes[$class]);
Expand Down
20 changes: 9 additions & 11 deletions src/Elements/HtmlEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,21 @@ public function __construct(?HtmlAttributes $attributes = null) {
* Sets an attribute.
*
* @param string $name
* @param mixed] $value
* @return HtmlEntity
* @param mixed $value
* @return static
*/
public function setAttribute(string $name, $value): HtmlEntity{
public function setAttribute(string $name, $value): static{
$this->getAttributes()->setAttribute($name, $value);

return $this;
}

/**
* Adds all classes from array.
*
* @param array $classes
* @return HtmlEntity
* @return static
*/
public function addClassesFromArray(array $classes): HtmlEntity{
public function addClassesFromArray(array $classes): static{
$this->getAttributes()->addClassesFromArray($classes);

return $this;
Expand All @@ -46,11 +45,10 @@ public function addClassesFromArray(array $classes): HtmlEntity{
* Adds a class.
*
* @param string $class
* @return HtmlEntity
* @return static
*/
public function addClass(string $class): HtmlEntity {
public function addClass(string $class): static {
$this->getAttributes()->addClass($class);

return $this;
}

Expand All @@ -67,10 +65,10 @@ public function getAttributes(): HtmlAttributes {
* Set attributes object.
*
* @param HtmlAttributes $attributes
* @return static
*/
protected function setAttributes(HtmlAttributes $attributes): HtmlEntity {
protected function setAttributes(HtmlAttributes $attributes): static {
$this->attributes = $attributes;

return $this;
}
}
28 changes: 14 additions & 14 deletions src/Elements/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ public function __construct(Environment $twig, ?TableRow $header = null, array $
* Creates the table header from an indexed array.
*
* @param array $headerArray
* @return Table
* @return static
*/
public function setHeaderFromArray(array $headerArray): Table {
public function setHeaderFromArray(array $headerArray): static {
$headerTableRow = TableRow::createFromArray($headerArray);
// Hide rows with empty title (===null)
$headerTableRow->iterateCells(function (TableCell $cell, $index) {
Expand All @@ -65,9 +65,9 @@ public function setHeaderFromArray(array $headerArray): Table {
* Adds rows from an array of indexed arrays.
*
* @param array $rowsArray
* @return Table
* @return static
*/
public function addRowsFromArray(array $rowsArray): Table {
public function addRowsFromArray(array $rowsArray): static {
if (!empty($rowsArray)) {
foreach ($rowsArray as $rowArray) {
$this->addRow(TableRow::createFromArray($rowArray));
Expand All @@ -80,9 +80,9 @@ public function addRowsFromArray(array $rowsArray): Table {
* Adds a single row.
*
* @param TableRow $row
* @return Table
* @return static
*/
public function addRow(TableRow $row): Table {
public function addRow(TableRow $row): static {
$this->rows[] = $row;
return $this;
}
Expand All @@ -91,9 +91,9 @@ public function addRow(TableRow $row): Table {
* Sets the header.
*
* @param TableRow $header
* @return Table
* @return static
*/
public function setHeader(TableRow $header): Table {
public function setHeader(TableRow $header): static {
$this->header = $header;
return $this;
}
Expand All @@ -102,9 +102,9 @@ public function setHeader(TableRow $header): Table {
* Sets the rows.
*
* @param array $rows
* @return Table
* @return static
*/
public function setRows(array $rows = []): Table {
public function setRows(array $rows = []): static {
$this->rows = [];
if (!empty($rows)) {
foreach ($rows as $row) {
Expand Down Expand Up @@ -161,9 +161,9 @@ public function render() {
*
* Only useful if header was set before.
*
* @return Table
* @return static
*/
public function removeHeaderlessColumns(): Table {
public function removeHeaderlessColumns(): static {
$cellKeys = $this->header->getCellKeys();
if (!empty($this->rows) && !empty($cellKeys)) {
foreach ($this->rows as $row) {
Expand All @@ -188,9 +188,9 @@ public function removeHeaderlessColumns(): Table {
*
* This
* @param stromg $class The class name to hide the cell.
* @return Table
* @return static
*/
public function hideHeaderlessColumns(): Table {
public function hideHeaderlessColumns(): static {
// Exclude hidden (value === null) header cells.
$cellKeys = $this->header->getCellKeys(true);
if (!empty($this->rows) && !empty($cellKeys)) {
Expand Down
26 changes: 12 additions & 14 deletions src/Elements/TableCell.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ public function getKey() {
/**
* Set the value of key.
*
* @return self
* @return static
*/
private function setKey($key) {
private function setKey($key): static {
$this->key = $key;

return $this;
Expand All @@ -74,9 +74,9 @@ public function getValue() {
/**
* Set the value of value.
*
* @return self
* @return static
*/
public function setValue($value) {
public function setValue($value): static {
$this->value = $value;

return $this;
Expand All @@ -89,7 +89,7 @@ public function setValue($value) {
*/
public function toArray() {
// TODO - make class changable from outside:
if($this->hidden){
if ($this->hidden) {
// Add class 'hidden' if this is hidden:
$this->getAttributes()->addClass('hidden');
}
Expand Down Expand Up @@ -120,9 +120,9 @@ public function getAfterValueRaw() {
* Use with care and not for user input!
* This will not be auto-escaped!
*
* @return self
* @return static
*/
public function setAfterValueRaw($afterValueRaw) {
public function setAfterValueRaw($afterValueRaw): static {
$this->afterValueRaw = $afterValueRaw;

return $this;
Expand All @@ -142,9 +142,9 @@ public function getBeforeValueRaw() {
* Use with care and not for user input!
* This will not be auto-escaped!
*
* @return self
* @return static
*/
public function setBeforeValueRaw($beforeValueRaw) {
public function setBeforeValueRaw($beforeValueRaw): static {
$this->beforeValueRaw = $beforeValueRaw;

return $this;
Expand All @@ -153,10 +153,9 @@ public function setBeforeValueRaw($beforeValueRaw) {
/**
* Set the cell hidden.
*
* @return self
* @return static
*/
public function setHidden(bool $hidden)
{
public function setHidden(bool $hidden): static {
$this->hidden = $hidden;

return $this;
Expand All @@ -167,8 +166,7 @@ public function setHidden(bool $hidden)
*
* @return bool
*/
public function isHidden()
{
public function isHidden() {
return $this->hidden;
}
}
20 changes: 13 additions & 7 deletions src/Elements/TableRow.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ public function getCells(): array {
/**
* Set the value of cells
*
* @return self
* @return static
*/
public function setCells(array $cells): TableRow {
public function setCells(array $cells): static {
$this->cells = [];
if (!empty($cells)) {
foreach ($cells as $cell) {
Expand All @@ -82,9 +82,9 @@ public function setCells(array $cells): TableRow {
* Adds a cell by its unique key.
*
* @param TableCell $cell
* @return TableRow
* @return static
*/
public function addCell(TableCell $cell): TableRow {
public function addCell(TableCell $cell): static {
$cellKey = $cell->getKey();
if ($this->hasCell($cellKey)) {
throw new Exception("Cell with Key {$cellKey} already exists in this row. Can not be added twice!");
Expand All @@ -98,15 +98,21 @@ public function addCell(TableCell $cell): TableRow {
*
* @param string] $key
* @param TableCell $cell
* @return TableRow
* @return static
*/
protected function setCell(string $key, TableCell $cell): TableRow {
protected function setCell(string $key, TableCell $cell): static {
$this->cells[] = $cell;
$this->cellsByKey[$key] = $cell;
return $this;
}

public function removeCell(string $key): TableRow {
/**
* Removes a cell by $key.
*
* @param string $key
* @return static
*/
public function removeCell(string $key): static {
foreach ($this->cells as $index => $cell) {
if ($cell->getKey() === $key) {
unset($this->cellsByKey[$key]);
Expand Down

0 comments on commit 2a55579

Please sign in to comment.