Skip to content

Commit

Permalink
Rename getters: add get prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
xepozz committed Feb 21, 2021
1 parent 2c35b75 commit ba5bc20
Show file tree
Hide file tree
Showing 14 changed files with 88 additions and 95 deletions.
58 changes: 26 additions & 32 deletions src/FormModel.php
Expand Up @@ -38,12 +38,12 @@ abstract class FormModel implements FormModelInterface, PostValidationHookInterf
public function __construct()
{
$this->attributes = $this->collectAttributes();
$this->attributesLabels = $this->attributeLabels();
$this->attributesLabels = $this->getAttributeLabels();
}

public function isAttributeRequired(string $attribute): bool
{
$validators = $this->rules()[$attribute] ?? [];
$validators = $this->getRules()[$attribute] ?? [];

foreach ($validators as $validator) {
if ($validator instanceof Required) {
Expand All @@ -62,37 +62,45 @@ public function getAttributeValue(string $attribute)
return $this->readProperty($attribute);
}

public function attributeLabels(): array
public function getAttributeLabels(): array
{
return [];
}

public function attributeHint(string $attribute): string
public function getAttributeHint(string $attribute): string
{
[$attribute, $nested] = $this->getNestedAttribute($attribute);
if ($nested !== null) {
return $this->readProperty($attribute)->attributeHint($nested);
return $this->readProperty($attribute)->getAttributeHint($nested);
}

$hints = $this->attributeHints();
$hints = $this->getAttributeHints();

return $hints[$attribute] ?? '';
}

public function attributeHints(): array
public function getAttributeHints(): array
{
return [];
}

public function attributeLabel(string $attribute): string
public function getAttributeLabel(string $attribute): string
{
return $this->attributesLabels[$attribute] ?? $this->getAttributeLabel($attribute);
if (array_key_exists($attribute, $this->attributesLabels)) {
return $this->attributesLabels[$attribute];
}

[$attribute, $nested] = $this->getNestedAttribute($attribute);

return $nested !== null
? $this->readProperty($attribute)->getAttributeLabel($nested)
: $this->generateAttributeLabel($attribute);
}

/**
* @return string Returns classname without a namespace part or empty string when class is anonymous
*/
public function formName(): string
public function getFormName(): string
{
if (strpos(static::class, '@anonymous') !== false) {
return '';
Expand All @@ -111,20 +119,20 @@ public function hasAttribute(string $attribute): bool
return array_key_exists($attribute, $this->attributes);
}

public function error(string $attribute): array
public function getError(string $attribute): array
{
return $this->attributesErrors[$attribute] ?? [];
}

public function errors(): array
public function getErrors(): array
{
return $this->attributesErrors;
}

public function errorSummary(bool $showAllErrors): array
public function getErrorSummary(bool $showAllErrors): array
{
$lines = [];
$errors = $showAllErrors ? $this->errors() : [$this->firstErrors()];
$errors = $showAllErrors ? $this->getErrors() : [$this->getFirstErrors()];

foreach ($errors as $error) {
$lines = array_merge($lines, $error);
Expand All @@ -133,7 +141,7 @@ public function errorSummary(bool $showAllErrors): array
return $lines;
}

public function firstError(string $attribute): string
public function getFirstError(string $attribute): string
{
if (empty($this->attributesErrors[$attribute])) {
return '';
Expand All @@ -142,7 +150,7 @@ public function firstError(string $attribute): string
return reset($this->attributesErrors[$attribute]);
}

public function firstErrors(): array
public function getFirstErrors(): array
{
if (empty($this->attributesErrors)) {
return [];
Expand Down Expand Up @@ -172,7 +180,7 @@ public function hasErrors(?string $attribute = null): bool
*/
public function load(array $data, ?string $formName = null): bool
{
$scope = $formName ?? $this->formName();
$scope = $formName ?? $this->getFormName();

/**
* @psalm-var array<string,mixed>
Expand Down Expand Up @@ -232,14 +240,9 @@ public function addError(string $attribute, string $error): void
$this->attributesErrors[$attribute][] = $error;
}

public function rules(): array
{
return [];
}

public function getRules(): array
{
return $this->rules();
return [];
}

/**
Expand Down Expand Up @@ -308,15 +311,6 @@ private function getInflector(): Inflector
return $this->inflector;
}

private function getAttributeLabel(string $attribute): string
{
[$attribute, $nested] = $this->getNestedAttribute($attribute);

return $nested !== null
? $this->readProperty($attribute)->attributeLabel($nested)
: $this->generateAttributeLabel($attribute);
}

/**
* Generates a user friendly attribute label based on the give attribute name.
*
Expand Down
44 changes: 22 additions & 22 deletions src/FormModelInterface.php
Expand Up @@ -25,7 +25,7 @@ interface FormModelInterface extends DataSetInterface
*
* @return array attribute labels (name => label)
*/
public function attributeLabels(): array;
public function getAttributeLabels(): array;

/**
* Returns the text label for the specified attribute.
Expand All @@ -34,9 +34,9 @@ public function attributeLabels(): array;
*
* @return string the attribute label.
*
* {@see attributeLabels()}
* {@see getAttributeLabels()}
*/
public function attributeLabel(string $attribute): string;
public function getAttributeLabel(string $attribute): string;

/**
* Returns the attribute hints.
Expand All @@ -52,7 +52,7 @@ public function attributeLabel(string $attribute): string;
*
* @return array attribute hints (name => hint)
*/
public function attributeHints(): array;
public function getAttributeHints(): array;

/**
* Returns the text hint for the specified attribute.
Expand All @@ -61,9 +61,9 @@ public function attributeHints(): array;
*
* @return string the attribute hint.
*
* {@see attributeHints()}
* {@see getAttributeHints()}
*/
public function attributeHint(string $attribute): string;
public function getAttributeHint(string $attribute): string;

/**
* Returns a value indicating whether the attribute is required.
Expand Down Expand Up @@ -104,10 +104,10 @@ public function addError(string $attribute, string $error): void;
* ]
* ```
*
* {@see firstErrors()}
* {@see firstError()}
* {@see getFirstErrors()}
* {@see getFirstError()}
*/
public function errors(): array;
public function getErrors(): array;

/**
* Returns the errors for single attribute.
Expand All @@ -116,7 +116,7 @@ public function errors(): array;
*
* @return array
*/
public function error(string $attribute): array;
public function getError(string $attribute): array;

/**
* Returns the errors for all attributes as a one-dimensional array.
Expand All @@ -126,10 +126,10 @@ public function error(string $attribute): array;
*
* @return array errors for all attributes as a one-dimensional array. Empty array is returned if no error.
*
* {@see errors()}
* {@see firstErrors(){}
* {@see getErrors()}
* {@see getFirstErrors(){}
*/
public function errorSummary(bool $showAllErrors): array;
public function getErrorSummary(bool $showAllErrors): array;

/**
* Returns a value indicating whether there is any validation error.
Expand All @@ -146,10 +146,10 @@ public function hasErrors(?string $attribute = null): bool;
* @return array the first errors. The array keys are the attribute names, and the array values are the
* corresponding error messages. An empty array will be returned if there is no error.
*
* {@see errors()}
* {@see firstError()}
* {@see getErrors()}
* {@see getFirstError()}
*/
public function firstErrors(): array;
public function getFirstErrors(): array;

/**
* Returns the first error of the specified attribute.
Expand All @@ -158,10 +158,10 @@ public function firstErrors(): array;
*
* @return string the error message. Empty string is returned if there is no error.
*
* {@see errors()}
* {@see firstErrors()}
* {@see getErrors()}
* {@see getFirstErrors()}
*/
public function firstError(string $attribute): string;
public function getFirstError(string $attribute): string;

/**
* Returns the form name that this model class should use.
Expand All @@ -181,7 +181,7 @@ public function firstError(string $attribute): string;
*
* {@see load()}
*/
public function formName(): string;
public function getFormName(): string;

/**
* Populates the model with input data.
Expand All @@ -197,7 +197,7 @@ public function formName(): string;
* }
* ```
*
* `load()` gets the `'FormName'` from the {@see formName()} method (which you may override), unless the
* `load()` gets the `'FormName'` from the {@see getFormName()} method (which you may override), unless the
* `$formName` parameter is given. If the form name is empty string, `load()` populates the model with the whole of
* `$data` instead of `$data['FormName']`.
*
Expand Down Expand Up @@ -240,7 +240,7 @@ public function load(array $data, ?string $formName = null): bool;
*
* @return array Validation rules.
*/
public function rules(): array;
public function getRules(): array;

/**
* Set specified attribute
Expand Down
2 changes: 1 addition & 1 deletion src/Helper/HtmlForm.php
Expand Up @@ -78,7 +78,7 @@ public static function getInputId(FormModelInterface $form, string $attribute, s
*/
public static function getInputName(FormModelInterface $form, string $attribute): string
{
$formName = $form->formName();
$formName = $form->getFormName();

$data = self::parseAttribute($attribute);

Expand Down
2 changes: 1 addition & 1 deletion src/Widget/BooleanInput.php
Expand Up @@ -35,7 +35,7 @@ public function run(): string
$type = $new->type;

if ($new->enclosedByLabel) {
$new->options['label'] ??= $new->data->attributeLabel(HtmlForm::getAttributeName($new->attribute));
$new->options['label'] ??= $new->data->getAttributeLabel(HtmlForm::getAttributeName($new->attribute));
}

if ($new->uncheck) {
Expand Down
2 changes: 1 addition & 1 deletion src/Widget/Error.php
Expand Up @@ -30,7 +30,7 @@ public function run(): string
if ($errorSource !== null) {
$error = $errorSource($new->data, $new->attribute);
} else {
$error = $new->data->firstError(HtmlForm::getAttributeName($new->attribute));
$error = $new->data->getFirstError(HtmlForm::getAttributeName($new->attribute));
}

$tag = ArrayHelper::remove($new->options, 'tag', 'div');
Expand Down
3 changes: 1 addition & 2 deletions src/Widget/ErrorSummary.php
Expand Up @@ -8,7 +8,6 @@
use Yiisoft\Form\FormModelInterface;
use Yiisoft\Html\Html;
use Yiisoft\Widget\Widget;

use function array_merge;
use function array_unique;
use function array_values;
Expand Down Expand Up @@ -84,7 +83,7 @@ private function collectErrors(bool $encode, bool $showAllErrors): array
$lines = [];

foreach ([$new->data] as $form) {
$lines = array_unique(array_merge($lines, $form->errorSummary($showAllErrors)));
$lines = array_unique(array_merge($lines, $form->getErrorSummary($showAllErrors)));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Widget/Hint.php
Expand Up @@ -24,7 +24,7 @@ public function run(): string
{
$new = clone $this;

$hint = ArrayHelper::remove($new->options, 'hint', $new->data->attributeHint($new->attribute));
$hint = ArrayHelper::remove($new->options, 'hint', $new->data->getAttributeHint($new->attribute));

if (empty($hint)) {
return '';
Expand Down
4 changes: 2 additions & 2 deletions src/Widget/Input.php
Expand Up @@ -57,7 +57,7 @@ public function config(FormModelInterface $data, string $attribute, array $optio
$new = clone $this;
$new->data = $data;
$new->attribute = $attribute;
$rules = $data->rules()[$attribute] ?? [];
$rules = $data->getRules()[$attribute] ?? [];
foreach ($rules as $rule) {
if ($rule instanceof HtmlOptionsProvider) {
$new->options = array_merge($new->options, $rule->getHtmlOptions());
Expand Down Expand Up @@ -249,7 +249,7 @@ private function setPlaceholder(): void
{
if (!isset($this->options['placeholder']) && !(in_array($this->type, ['date', 'file', 'hidden', 'color'], true))) {
$attributeName = HtmlForm::getAttributeName($this->attribute);
$this->options['placeholder'] = $this->data->attributeLabel($attributeName);
$this->options['placeholder'] = $this->data->getAttributeLabel($attributeName);
}
}
}
2 changes: 1 addition & 1 deletion src/Widget/Label.php
Expand Up @@ -35,7 +35,7 @@ public function run(): string
$label = ArrayHelper::remove(
$new->options,
'label',
$new->data->attributeLabel(HtmlForm::getAttributeName($new->attribute))
$new->data->getAttributeLabel(HtmlForm::getAttributeName($new->attribute))
);

return Html::label($label, $for, $new->options);
Expand Down
2 changes: 1 addition & 1 deletion src/Widget/TextArea.php
Expand Up @@ -302,7 +302,7 @@ private function setPlaceholder(): void
{
if (!isset($this->options['placeholder'])) {
$attributeName = HtmlForm::getAttributeName($this->attribute);
$this->options['placeholder'] = $this->data->attributeLabel($attributeName);
$this->options['placeholder'] = $this->data->getAttributeLabel($attributeName);
}
}
}

0 comments on commit ba5bc20

Please sign in to comment.