Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API Strong typing for the view layer #118

Merged
merged 1 commit into from
Aug 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading