Skip to content

Commit

Permalink
Choices namespaced
Browse files Browse the repository at this point in the history
  • Loading branch information
dergel committed Sep 8, 2024
1 parent 13cb06b commit 7a13d04
Show file tree
Hide file tree
Showing 11 changed files with 106 additions and 244 deletions.
11 changes: 7 additions & 4 deletions boot.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@
use Yakamara\YForm\Rest\Rest;
use Yakamara\YForm\YForm;

/**
* @var rex_addon $this
* @psalm-scope-this rex_addon
*/
/** @var rex_addon $this */

// Aliases are deprecated and will be removed in the next major version

class_alias('Yakamara\YForm\YForm', 'rex_yform');
class_alias('Yakamara\YForm\Rest\Rest', 'rex_yform_rest');
Expand All @@ -30,6 +29,10 @@ class_alias('Yakamara\YForm\Manager\Field', 'rex_yform_manager_field');
class_alias('Yakamara\YForm\Manager\Manager', 'rex_yform_manager_manager');
class_alias('Yakamara\YForm\Manager\Query', 'rex_yform_manager_query');
class_alias('Yakamara\YForm\Manager\Search', 'rex_yform_manager_search');
class_alias('Yakamara\YForm\Choice\GroupView', 'rex_yform_choice_group_view');
class_alias('Yakamara\YForm\Choice\ChoiceList', 'rex_yform_choice_list');
class_alias('Yakamara\YForm\Choice\ListView', 'rex_yform_choice_list_view');
class_alias('Yakamara\YForm\Choice\View', 'rex_yform_choice_view');

YForm::addTemplatePath(rex_path::addon('yform', 'ytemplates'));

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
<?php

class rex_yform_choice_list
namespace Yakamara\YForm\Choice;

use rex_i18n;

use function call_user_func;
use function count;
use function in_array;
use function is_array;
use function is_callable;

class ChoiceList
{
public $choices = [];

private $options;

private $choicesByValues = [];

public function __construct($options)
Expand Down Expand Up @@ -89,7 +97,7 @@ public function createView(array $requiredAttributes = [])
} else {
$nestedLabel = rex_escape($nestedLabel);
}
$view = new rex_yform_choice_view($nestedValue, $nestedLabel, $choiceAttributes, $requiredAttributes);
$view = new View($nestedValue, $nestedLabel, $choiceAttributes, $requiredAttributes);

if ($preferredChoices && call_user_func($preferredChoices, $nestedValue, $nestedLabel)) {
$preferredGroupChoices[] = $view;
Expand All @@ -99,10 +107,10 @@ public function createView(array $requiredAttributes = [])
}

if (count($preferredGroupChoices)) {
$preferredViews[] = new rex_yform_choice_group_view($label, $preferredGroupChoices);
$preferredViews[] = new GroupView($label, $preferredGroupChoices);
}
if (count($otherGroupChoices)) {
$otherViews[] = new rex_yform_choice_group_view($label, $otherGroupChoices);
$otherViews[] = new GroupView($label, $otherGroupChoices);
}

continue;
Expand All @@ -114,7 +122,7 @@ public function createView(array $requiredAttributes = [])
$label = rex_escape($label);
}

$view = new rex_yform_choice_view($value, $label, $choiceAttributes, $requiredAttributes);
$view = new View($value, $label, $choiceAttributes, $requiredAttributes);

if ($preferredChoices && call_user_func($preferredChoices, $value, $label)) {
$preferredViews[] = $view;
Expand All @@ -124,17 +132,17 @@ public function createView(array $requiredAttributes = [])
}

foreach ($preferredViews as $index => $view) {
if ($view instanceof rex_yform_choice_group_view && 0 === count($view->getChoices())) {
if ($view instanceof GroupView && 0 === count($view->getChoices())) {
unset($preferredViews[$index]);
}
}
foreach ($otherViews as $index => $view) {
if ($view instanceof rex_yform_choice_group_view && 0 === count($view->getChoices())) {
if ($view instanceof GroupView && 0 === count($view->getChoices())) {
unset($otherViews[$index]);
}
}

return new rex_yform_choice_list_view($otherViews, $preferredViews);
return new ListView($otherViews, $preferredViews);
}

public function getDefaultValues(array $defaultChoices = [])
Expand Down
31 changes: 31 additions & 0 deletions lib/Choice/GroupView.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Yakamara\YForm\Choice;

class GroupView
{
public string $label;
public array $choices;

/**
* Creates a new choice group view.
*
* @param string $label The label of the group
* @param array<\Yakamara\YForm\Choice\View> $choices the choice views in the group
*/
public function __construct(string $label, array $choices = [])
{
$this->label = $label;
$this->choices = $choices;
}

public function getChoices(): array
{
return $this->choices;
}

public function getLabel(): string
{
return $this->label;
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
<?php

class rex_yform_choice_list_view
namespace Yakamara\YForm\Choice;

class ListView
{
public $choices;
public $preferredChoices;

/**
* Creates a new choice list view.
*
* @param rex_yform_choice_group_view[]|rex_yform_choice_view[] $choices The choice views
* @param rex_yform_choice_group_view[]|rex_yform_choice_view[] $preferredChoices the preferred choice views
* @param array<\Yakamara\YForm\Choice\GroupView>|array<\Yakamara\YForm\Choice\View> $choices The choice views
* @param array<\Yakamara\YForm\Choice\GroupView>|array<\Yakamara\YForm\Choice\View> $preferredChoices the preferred choice views
*/
public function __construct(array $choices = [], array $preferredChoices = [])
{
Expand All @@ -28,10 +30,10 @@ public function hasPlaceholder()
{
if ($this->preferredChoices) {
$firstChoice = reset($this->preferredChoices);
return $firstChoice instanceof rex_yform_choice_view && '' === $firstChoice->value;
return $firstChoice instanceof View && '' === $firstChoice->value;
}
$firstChoice = reset($this->choices);
return $firstChoice instanceof rex_yform_choice_view && '' === $firstChoice->value;
return $firstChoice instanceof View && '' === $firstChoice->value;
}

public function getChoices()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
<?php

class rex_yform_choice_view
namespace Yakamara\YForm\Choice;

use rex_string;

use function call_user_func;
use function is_array;
use function is_callable;

class View
{
public $label;
public $value;
Expand Down Expand Up @@ -59,7 +67,7 @@ public function getAttributes()

public function getAttributesAsString()
{
return \rex_string::buildAttributes($this->getAttributes());
return rex_string::buildAttributes($this->getAttributes());
}

public function getLabel()
Expand Down
171 changes: 0 additions & 171 deletions lib/radio.php

This file was deleted.

29 changes: 0 additions & 29 deletions lib/yform/value/Choice/rex_yform_choice_group_view.php

This file was deleted.

Loading

0 comments on commit 7a13d04

Please sign in to comment.