-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add options factory and formatter options
- Loading branch information
Showing
3 changed files
with
123 additions
and
29 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,79 @@ | ||
<?php | ||
|
||
namespace HeimrichHannot\UtilsBundle\Options; | ||
|
||
use BadMethodCallException; | ||
|
||
class OptionsFactory | ||
{ | ||
protected array $options = []; | ||
|
||
public function get(string $key, mixed $default = null): mixed | ||
{ | ||
return $this->$key ?? $this->options[$key] ?? $default; | ||
} | ||
|
||
public function set(string $key, mixed $value): static | ||
{ | ||
if (property_exists($this, $key)) { | ||
$this->$key = $value; | ||
} | ||
$this->options[$key] = $value; | ||
return $this; | ||
} | ||
|
||
public function has(string $key): bool | ||
{ | ||
return isset($this->$key) || isset($this->options[$key]); | ||
} | ||
|
||
public function del(string $key): static | ||
{ | ||
unset($this->options[$key]); | ||
return $this; | ||
} | ||
|
||
protected function methodNameToOptionKey(string $name): string | ||
{ | ||
return lcfirst(substr($name, 3)); | ||
} | ||
|
||
public function __call(string $name, array $arguments): mixed | ||
{ | ||
if (strlen($name) < 4) { | ||
throw new BadMethodCallException(sprintf('Method %s does not exist', $name)); | ||
} | ||
|
||
$numArgs = count($arguments); | ||
if ($numArgs < 1 || $numArgs > 1) { | ||
throw new BadMethodCallException(sprintf('Method %s expects exactly one argument', $name)); | ||
} | ||
|
||
$key = static::methodNameToOptionKey($name); | ||
$prefix = substr($name, 0, 3); | ||
|
||
return match ($prefix) | ||
{ | ||
'set' => $this->set($key, $arguments[0]), | ||
'get' => $this->get($key, $arguments[0] ?? null), | ||
'has' => $this->has($key), | ||
'del' => $this->del($key), | ||
default => throw new BadMethodCallException(sprintf('Method %s does not exist', $name)), | ||
}; | ||
} | ||
|
||
public function __get(string $name) | ||
{ | ||
return $this->get($name); | ||
} | ||
|
||
public function __set(string $name, mixed $value) | ||
{ | ||
$this->set($name, $value); | ||
} | ||
|
||
public static function create(): static | ||
{ | ||
return new static(); | ||
} | ||
} |
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
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,21 @@ | ||
<?php | ||
|
||
namespace HeimrichHannot\UtilsBundle\Util\FormatterUtil; | ||
|
||
use HeimrichHannot\UtilsBundle\Options\OptionsFactory; | ||
|
||
/** | ||
* @method $this setPreserveEmptyArrayValues(bool $value) | ||
* @method $this setLocalize(bool $value) | ||
* @method $this setLoadDca(bool $value) | ||
* @method $this setCacheOptions(bool $value) | ||
* @method $this setReplaceInsertTags(bool $value) | ||
*/ | ||
class FormatDcaFieldValueOptions extends OptionsFactory | ||
{ | ||
public bool $preserveEmptyArrayValues = false; | ||
public bool $localize = true; | ||
public bool $loadDca = true; | ||
public bool $cacheOptions = true; | ||
public bool $replaceInsertTags = true; | ||
} |