Skip to content

Commit

Permalink
API Strong typing for the view layer
Browse files Browse the repository at this point in the history
  • Loading branch information
GuySartorelli committed Aug 22, 2024
1 parent 27f9d9c commit a772c22
Showing 1 changed file with 21 additions and 49 deletions.
70 changes: 21 additions & 49 deletions src/ORM/FieldType/MultiValueField.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

namespace Symbiote\MultiValueField\ORM\FieldType;

use SilverStripe\Forms\FormField;
use SilverStripe\ORM\FieldType\DBComposite;
use SilverStripe\ORM\FieldType\DBVarchar;
use SilverStripe\ORM\ArrayList;
use SilverStripe\View\ArrayData;
use SilverStripe\View\ViewableData;
use Symbiote\MultiValueField\Fields\MultiValueTextField;

/**
* A DB field that serialises an array before writing it to the db, and returning the array
Expand All @@ -15,24 +18,16 @@
*/
class MultiValueField extends DBComposite
{
/**
* @param array
*/
private static $composite_db = array(
private static array $composite_db = [
"Value" => "Text",
);
];

/**
*
* @var boolean
*/
protected $changed = false;
protected bool $changed = false;

/**
* Returns the value of this field.
* @return mixed
*/
public function getValue()
public function getValue(): mixed
{
$value = $this->value;
if (is_null($value)) {
Expand All @@ -42,7 +37,7 @@ public function getValue()
return $this->value;
}

public function getValues()
public function getValues(): mixed
{
return $this->getValue();
}
Expand All @@ -51,14 +46,9 @@ public function getValues()
* Set the value on the field. Ensures the underlying composite field
* logic that looks for Value will trigger if the value set is
*
*
* For a multivalue field, this will deserialise the value if it is a string
*
* @param mixed $value
* @param array $record
* @return $this
*/
public function setValue($value, $record = null, $markChanged = true)
public function setValue(mixed $value, null|array|ViewableData $record = null, bool $markChanged = true): static
{
$this->changed = $this->changed || $markChanged;
if (!is_null($value)) {
Expand Down Expand Up @@ -90,14 +80,10 @@ protected function serializeValue($value)

/**
* Unserialises data, depending on new or old format
*
* @param string $data
*
* @return array
*/
protected function unserializeData($data)
protected function unserializeData(mixed $data): mixed
{
$value = null;
$value = [];
// if we're not deserialised yet, do so
if (is_string($data) && strlen($data ?? '') > 1) {
// are we json encoded?
Expand All @@ -110,12 +96,7 @@ protected function unserializeData($data)
return $value;
}

/**
* (non-PHPdoc).
*
* @see core/model/fieldtypes/DBField#prepValueForDB($value)
*/
public function prepValueForDB($value)
public function prepValueForDB(mixed $value): mixed
{
if ($value instanceof MultiValueField) {
$value = $value->getValue();
Expand All @@ -127,38 +108,34 @@ public function prepValueForDB($value)
return parent::prepValueForDB($value);
}

public function isChanged()
public function isChanged(): bool
{
return $this->changed;
}

public function scaffoldFormField($title = null, $params = null)
public function scaffoldFormField(?string $title = null, array $params = []): FormField
{
return new \Symbiote\MultiValueField\Fields\MultiValueTextField($this->name, $title);
return new MultiValueTextField($this->name, $title);
}

/**
* Convert to a textual list of items.
*/
public function csv()
public function csv(): string
{
return $this->Implode(',');
}

/**
* Return all items separated by a separator, defaulting to a comma and
* space.
*
* @param string $separator
*
* @return string
*/
public function Implode($separator = ', ')
public function Implode(string $separator = ', '): string
{
return implode($separator ?? '', $this->getValue());
}

public function __toString()
public function __toString(): string
{
if ($this->getValue()) {
return $this->csv();
Expand All @@ -167,7 +144,7 @@ public function __toString()
return '';
}

public function ItemByKey()
public function ItemByKey(): ArrayData
{
$values = $this->getValue();
if (array_keys($values ?? []) == range(0, count($values ?? []) - 1)) {
Expand All @@ -176,12 +153,7 @@ public function ItemByKey()
return new ArrayData($values);
}

public function Items()
{
return $this->forTemplate();
}

public function forTemplate()
public function Items(): ArrayList
{
$items = [];
$value = $this->getValue();
Expand All @@ -190,7 +162,7 @@ public function forTemplate()
$v = new DBVarchar('Value');
$v->setValue($item);

$obj = new ArrayData([
$obj = new ArrayData([
'Value' => $v,
'Key' => $key,
'Title' => $item,
Expand Down

0 comments on commit a772c22

Please sign in to comment.