-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from infinityloop-dev/attribute_support
Support for disabled and title attributes
- Loading branch information
Showing
3 changed files
with
161 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of Nepttune (https://www.peldax.com) | ||
* | ||
* Copyright (c) 2018 Václav Pelíšek ([email protected]) | ||
* | ||
* This software consists of voluntary contributions made by many individuals | ||
* and is licensed under the MIT license. For more information, see | ||
* <https://www.peldax.com>. | ||
*/ | ||
|
||
declare(strict_types = 1); | ||
|
||
namespace Nepttune\Form; | ||
|
||
class ResultObject implements \JsonSerializable | ||
{ | ||
private int $id; | ||
private string $text; | ||
private ?string $title = null; | ||
private bool $disabled; | ||
|
||
public function __construct(int $id, string $text, ?string $title = null, bool $disabled = false) | ||
{ | ||
$this->id = $id; | ||
$this->text = $text; | ||
$this->title = $title; | ||
$this->disabled = $disabled; | ||
} | ||
|
||
public function getId() : int | ||
{ | ||
return $this->id; | ||
} | ||
|
||
public function getText() : string | ||
{ | ||
return $this->text; | ||
} | ||
|
||
public function isDisabled() : bool | ||
{ | ||
return $this->disabled; | ||
} | ||
|
||
public function jsonSerialize() | ||
{ | ||
$toReturn = [ | ||
'id' => $this->id, | ||
'text' => $this->text, | ||
]; | ||
|
||
if ($this->title !== null) { | ||
$toReturn['title'] = $this->title; | ||
} | ||
|
||
if ($this->disabled === true) { | ||
$toReturn['disabled'] = $this->disabled; | ||
} | ||
|
||
return $toReturn; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of Nepttune (https://www.peldax.com) | ||
* | ||
* Copyright (c) 2018 Václav Pelíšek ([email protected]) | ||
* | ||
* This software consists of voluntary contributions made by many individuals | ||
* and is licensed under the MIT license. For more information, see | ||
* <https://www.peldax.com>. | ||
*/ | ||
|
||
declare(strict_types = 1); | ||
|
||
namespace Nepttune\Form; | ||
|
||
class ResultObjectSet extends \Infinityloop\Utils\ObjectSet implements \JsonSerializable | ||
{ | ||
protected const INNER_CLASS = ResultObject::class; | ||
|
||
public static function fromArray(array $data) : \Nepttune\Form\ResultObjectSet | ||
{ | ||
$objectSet = []; | ||
|
||
foreach ($data as $key => $value) { | ||
if ($value instanceof \Nepttune\Form\ResultObject) { | ||
$objectSet[] = $value; | ||
|
||
continue; | ||
} | ||
|
||
$objectSet[] = new \Nepttune\Form\ResultObject($key, $value); | ||
} | ||
|
||
return new self($objectSet); | ||
} | ||
|
||
public function jsonSerialize() | ||
{ | ||
$toReturn = []; | ||
|
||
foreach ($this as $object) { | ||
$toReturn[] = $object->jsonSerialize(); | ||
} | ||
|
||
return $toReturn; | ||
} | ||
|
||
public function getRawData() : array | ||
{ | ||
$toReturn = []; | ||
|
||
foreach ($this as $object) { | ||
$toReturn[$object->getId()] = $object->getText(); | ||
} | ||
|
||
return $toReturn; | ||
} | ||
|
||
public function getDisabled() : array | ||
{ | ||
$toReturn = []; | ||
|
||
foreach ($this as $object) { | ||
if ($object->isDisabled()) { | ||
$toReturn[] = $object->getId(); | ||
} | ||
} | ||
|
||
return $toReturn; | ||
} | ||
|
||
public function current() : \Nepttune\Form\ResultObject | ||
{ | ||
return parent::current(); | ||
} | ||
|
||
public function offsetGet($offset) : \Nepttune\Form\ResultObject | ||
{ | ||
return parent::offsetGet($offset); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters