Skip to content

Commit

Permalink
add character count options to text elements, allow nameExpression in…
Browse files Browse the repository at this point in the history
… CollectionElement
  • Loading branch information
bernhard-efler committed Feb 10, 2021
1 parent 0fb0b8b commit 5bff7f2
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 5 deletions.
63 changes: 63 additions & 0 deletions src/Element/AbstractTextElement.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php
/**
* @link https://github.com/ixocreate
* @copyright IXOLIT GmbH
* @license MIT License
*/

declare(strict_types=1);

namespace Ixocreate\Schema\Element;

abstract class AbstractTextElement extends AbstractSingleElement
{
/** @var bool */
protected $characterCount = false;

/** @var array */
protected $characterBoundaries = null;

/**
* @return bool
*/
public function characterCount(): bool
{
return $this->characterCount;
}

public function withCharacterCount(bool $characterCount): AbstractTextElement
{
$element = clone $this;
$element->characterCount = $characterCount;

return $element;
}

/**
* @return array
*/
public function characterBoundaries(): ?array
{
return $this->characterBoundaries;
}

public function withCharacterBoundaries(?int $min, ?int $max): AbstractTextElement
{
$element = clone $this;
$element->characterBoundaries = [
'min' => $min,
'max' => $max,
];

return $element;
}

public function jsonSerialize()
{
$array = parent::jsonSerialize();
$array['characterCount'] = $this->characterCount;
$array['characterBoundaries'] = $this->characterBoundaries;

return $array;
}
}
8 changes: 7 additions & 1 deletion src/Element/CollectionElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,20 @@ public function jsonSerialize()
* @param string $name
* @param string $label
* @param SchemaInterface $schema
* @param string $nameExpression
* @throws \Exception
* @return CollectionElement
*/
public function addCollectionElement(string $name, string $label, SchemaInterface $schema): CollectionElement
public function addCollectionElement(string $name, string $label, SchemaInterface $schema, string $nameExpression = ''): CollectionElement
{
$group = $this->builder->create(GroupElement::class, $name);
$group = $group->withLabel($label);
$group = $group->withElements($schema->elements());

if ($nameExpression != '') {
$group = $group->withNameExpression($nameExpression);
}

return $this->withAddedElement($group);
}
}
2 changes: 1 addition & 1 deletion src/Element/GroupElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ final class GroupElement extends AbstractGroup implements StructuralGroupingInte
/**
* @var string
*/
private $nameExpression = "";
private $nameExpression = '';

/**
* @return string
Expand Down
2 changes: 1 addition & 1 deletion src/Element/HtmlElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

use Ixocreate\Schema\Type\HtmlType;

final class HtmlElement extends AbstractSingleElement
final class HtmlElement extends AbstractTextElement
{
public function type(): string
{
Expand Down
2 changes: 1 addition & 1 deletion src/Element/TextElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

use Ixocreate\Schema\Type\TypeInterface;

final class TextElement extends AbstractSingleElement
final class TextElement extends AbstractTextElement
{
public function type(): string
{
Expand Down
2 changes: 1 addition & 1 deletion src/Element/TextareaElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

use Ixocreate\Schema\Type\TypeInterface;

final class TextareaElement extends AbstractSingleElement
final class TextareaElement extends AbstractTextElement
{
public function type(): string
{
Expand Down

0 comments on commit 5bff7f2

Please sign in to comment.