Skip to content

Commit

Permalink
cs whitespace
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Dec 12, 2021
1 parent 8d88cf8 commit fd785f8
Show file tree
Hide file tree
Showing 27 changed files with 159 additions and 1 deletion.
15 changes: 15 additions & 0 deletions src/Bridges/FormsLatte/FormMacros.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,16 @@ public function macroForm(MacroNode $node, PhpWriter $writer)
if ($node->modifiers) {
throw new CompileException('Modifiers are not allowed in ' . $node->getNotation());
}

if ($node->prefix) {
throw new CompileException('Did you mean <form n:name=...> ?');
}

$name = $node->tokenizer->fetchWord();
if ($name == null) { // null or false
throw new CompileException('Missing form name in ' . $node->getNotation());
}

$node->replaced = true;
$node->tokenizer->reset();
return $writer->write(
Expand All @@ -80,13 +83,16 @@ public function macroFormContext(MacroNode $node, PhpWriter $writer)
if ($node->modifiers) {
throw new CompileException('Modifiers are not allowed in ' . $node->getNotation());
}

if ($node->prefix) {
throw new CompileException('Did you mean <form n:name=...> ?');
}

$name = $node->tokenizer->fetchWord();
if ($name == null) { // null or false
throw new CompileException('Missing form name in ' . $node->getNotation());
}

$node->tokenizer->reset();
return $writer->write(
'$form = $this->global->formsStack[] = '
Expand All @@ -105,10 +111,12 @@ public function macroFormContainer(MacroNode $node, PhpWriter $writer)
if ($node->modifiers) {
throw new CompileException('Modifiers are not allowed in ' . $node->getNotation());
}

$name = $node->tokenizer->fetchWord();
if ($name == null) { // null or false
throw new CompileException('Missing name in ' . $node->getNotation());
}

$node->tokenizer->reset();
return $writer->write(
'$this->global->formsStack[] = $formContainer = '
Expand All @@ -127,10 +135,12 @@ public function macroLabel(MacroNode $node, PhpWriter $writer)
if ($node->modifiers) {
throw new CompileException('Modifiers are not allowed in ' . $node->getNotation());
}

$words = $node->tokenizer->fetchWords();
if (!$words) {
throw new CompileException('Missing name in ' . $node->getNotation());
}

$node->replaced = true;
$name = array_shift($words);
return $writer->write(
Expand Down Expand Up @@ -166,10 +176,12 @@ public function macroInput(MacroNode $node, PhpWriter $writer)
if ($node->modifiers) {
throw new CompileException('Modifiers are not allowed in ' . $node->getNotation());
}

$words = $node->tokenizer->fetchWords();
if (!$words) {
throw new CompileException('Missing name in ' . $node->getNotation());
}

$node->replaced = true;
$name = array_shift($words);
return $writer->write(
Expand All @@ -192,6 +204,7 @@ public function macroNameAttr(MacroNode $node, PhpWriter $writer)
if (!$words) {
throw new CompileException('Missing name in ' . $node->getNotation());
}

$name = array_shift($words);
$tagName = strtolower($node->htmlNode->name);
$node->empty = $tagName === 'input';
Expand Down Expand Up @@ -265,6 +278,7 @@ public function macroInputError(MacroNode $node, PhpWriter $writer)
if ($node->modifiers) {
throw new CompileException('Modifiers are not allowed in ' . $node->getNotation());
}

$name = $node->tokenizer->fetchWord();
$node->replaced = true;
if (!$name) {
Expand All @@ -291,6 +305,7 @@ public function macroFormPrint(MacroNode $node, PhpWriter $writer)
if ($name == null) { // null or false
throw new CompileException('Missing form name in ' . $node->getNotation());
}

$node->tokenizer->reset();
return $writer->write(
'Nette\Bridges\FormsLatte\Runtime::render' . $node->name . '('
Expand Down
3 changes: 3 additions & 0 deletions src/Bridges/FormsLatte/Runtime.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,14 @@ public static function renderFormBegin(Form $form, array $attrs, bool $withTags
foreach ($form->getControls() as $control) {
$control->setOption('rendered', false);
}

$el = $form->getElementPrototype();
$el->action = (string) $el->action;
$el = clone $el;
if ($form->isMethod('get')) {
$el->action = preg_replace('~\?[^#]*~', '', $el->action, 1);
}

$el->addAttributes($attrs);
return $withTags ? $el->startTag() : $el->attributes();
}
Expand Down Expand Up @@ -101,6 +103,7 @@ public static function renderFormClassPrint(Form $form): void
$generator->propertyPromotion = true;
$blueprint->printCode($generator->generateCode($form));
}

echo $end;
}
}
13 changes: 12 additions & 1 deletion src/Forms/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public function setDefaults($data, bool $erase = false)
if (!$form || !$form->isAnchored() || !$form->isSubmitted()) {
$this->setValues($data, $erase);
}

return $this;
}

Expand Down Expand Up @@ -89,7 +90,6 @@ public function setValues($data, bool $erase = false)
} elseif ($erase) {
$control->setValue(null);
}

} elseif ($control instanceof self) {
if (array_key_exists($name, $values)) {
$control->setValues($values[$name], $erase);
Expand All @@ -99,6 +99,7 @@ public function setValues($data, bool $erase = false)
}
}
}

return $this;
}

Expand All @@ -119,10 +120,12 @@ public function getValues($returnType = null, array $controls = null)
} elseif (!$this->isValid()) {
trigger_error(__METHOD__ . "() invoked but the form is not valid (form '{$this->getName()}').", E_USER_WARNING);
}

if ($controls === null && $submitter instanceof SubmitterControl) {
$controls = $submitter->getValidationScope();
}
}

$returnType = $returnType === true ? self::ARRAY : $returnType;
return $this->getUnsafeValues($returnType, $controls);
}
Expand Down Expand Up @@ -202,8 +205,10 @@ public function isValid(): bool
if ($this->getErrors()) {
return false;
}

$this->validate();
}

return !$this->getErrors();
}

Expand All @@ -220,6 +225,7 @@ public function validate(array $controls = null): void
$control->validate();
}
}

$this->validated = true;

foreach ($this->onValidate as $handler) {
Expand All @@ -242,6 +248,7 @@ public function getErrors(): array
foreach ($this->getControls() as $control) {
$errors = array_merge($errors, $control->getErrors());
}

return array_unique($errors);
}

Expand Down Expand Up @@ -277,6 +284,7 @@ public function addComponent(Nette\ComponentModel\IComponent $component, ?string
if ($this->currentGroup !== null) {
$this->currentGroup->add($component);
}

return $this;
}

Expand Down Expand Up @@ -498,6 +506,7 @@ public function addContainer($name): self
if ($this->currentGroup !== null) {
$this->currentGroup->add($control);
}

return $this[$name] = $control;
}

Expand All @@ -510,6 +519,7 @@ public function __call(string $name, array $args)
if (isset(self::$extMethods[$name])) {
return (self::$extMethods[$name])($this, ...$args);
}

return parent::__call($name, $args);
}

Expand All @@ -519,6 +529,7 @@ public static function extensionMethod(string $name, /*callable*/ $callback): vo
if (strpos($name, '::') !== false) { // back compatibility
[, $name] = explode('::', $name);
}

self::$extMethods[$name] = $callback;
}

Expand Down
2 changes: 2 additions & 0 deletions src/Forms/ControlGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public function add(...$items)
throw new Nette\InvalidArgumentException("Control or Container items expected, $type given.");
}
}

return $this;
}

Expand Down Expand Up @@ -97,6 +98,7 @@ public function setOption(string $key, $value)
} else {
$this->options[$key] = $value;
}

return $this;
}

Expand Down
11 changes: 11 additions & 0 deletions src/Forms/Controls/BaseControl.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ public function setDefaultValue($value)
if ($this->isDisabled() || !$form || !$form->isAnchored() || !$form->isSubmitted()) {
$this->setValue($value);
}

return $this;
}

Expand All @@ -210,6 +211,7 @@ public function setDisabled($value = true)
} elseif (($form = $this->getForm(false)) && $form->isAnchored() && $form->isSubmitted()) {
$this->loadHttpData();
}

return $this;
}

Expand Down Expand Up @@ -335,6 +337,7 @@ public function getHtmlId()
: $form->getName() . '-';
$this->control->id = sprintf(self::$idMask, $prefix . $this->lookupPath());
}

return $this->control->id;
}

Expand All @@ -355,6 +358,7 @@ public function setHtmlAttribute(string $name, $value = true)
) {
$this->loadHttpData();
}

return $this;
}

Expand Down Expand Up @@ -393,6 +397,7 @@ public function getTranslator(): ?Nette\Localization\Translator
? $this->getForm()->getTranslator()
: null;
}

return $this->translator;
}

Expand All @@ -411,6 +416,7 @@ public function translate($value, ...$parameters)
}
}
}

return $value;
}

Expand Down Expand Up @@ -495,6 +501,7 @@ public function validate(): void
if ($this->isDisabled()) {
return;
}

$this->cleanErrors();
$this->rules->validate();
}
Expand Down Expand Up @@ -554,6 +561,7 @@ public function setOption($key, $value)
} else {
$this->options[$key] = $value;
}

return $this;
}

Expand Down Expand Up @@ -587,8 +595,10 @@ public function __call(string $name, array $args)
if (isset(self::$extMethods[$name][$class])) {
return (self::$extMethods[$name][$class])($this, ...$args);
}

$class = get_parent_class($class);
} while ($class);

return parent::__call($name, $args);
}

Expand All @@ -598,6 +608,7 @@ public static function extensionMethod(string $name, /*callable*/ $callback): vo
if (strpos($name, '::') !== false) { // back compatibility
[, $name] = explode('::', $name);
}

self::$extMethods[$name][static::class] = $callback;
}
}
1 change: 1 addition & 0 deletions src/Forms/Controls/Button.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ public function getControl($caption = null): Html
} else {
$el->value = $caption;
}

return $el;
}
}
1 change: 1 addition & 0 deletions src/Forms/Controls/Checkbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public function setValue($value)
if (!is_scalar($value) && $value !== null) {
throw new Nette\InvalidArgumentException(sprintf("Value must be scalar or null, %s given in field '%s'.", gettype($value), $this->name));
}

$this->value = (bool) $value;
return $this;
}
Expand Down
2 changes: 2 additions & 0 deletions src/Forms/Controls/ChoiceControl.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public function setValue($value)
$set = Nette\Utils\Strings::truncate(implode(', ', array_map(function ($s) { return var_export($s, true); }, array_keys($this->items))), 70, '...');
throw new Nette\InvalidArgumentException("Value '$value' is out of allowed set [$set] in field '{$this->name}'.");
}

$this->value = $value === null ? null : key([(string) $value => null]);
return $this;
}
Expand Down Expand Up @@ -142,6 +143,7 @@ public function setDisabled($value = true)
if (isset($this->disabled[$this->value])) {
$this->value = null;
}

return $this;
}

Expand Down
3 changes: 3 additions & 0 deletions src/Forms/Controls/CsrfProtection.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,12 @@ public function getToken(): string
if (!$this->session) {
throw new Nette\InvalidStateException('Session initialization error');
}

$session = $this->session->getSection(self::class);
if (!isset($session->token)) {
$session->token = Nette\Utils\Random::generate();
}

return $session->token ^ $this->session->getId();
}

Expand All @@ -84,6 +86,7 @@ private function generateToken(string $random = null): string
if ($random === null) {
$random = Nette\Utils\Random::generate(10);
}

return $random . base64_encode(sha1($this->getToken() . $random, true));
}

Expand Down
2 changes: 2 additions & 0 deletions src/Forms/Controls/HiddenField.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,11 @@ public function setValue($value)
} elseif (!is_scalar($value) && !(is_object($value) && method_exists($value, '__toString'))) {
throw new Nette\InvalidArgumentException(sprintf("Value must be scalar or null, %s given in field '%s'.", gettype($value), $this->name));
}

if (!$this->persistValue) {
$this->value = $value;
}

return $this;
}

Expand Down
Loading

0 comments on commit fd785f8

Please sign in to comment.