# Printers
**Printers** are specials converters that transform any value into a string.
- A printer is a specific <@\MMO\core\classes\aExporter> that uses property's attributes to export an object's properties.
- A printer's behavior can be overridden in a class using a specific interface method.
The method MUST take an optional parameter `array $options = []`
## Options
How a value is printed (escaped, indented, how deep...) is set by options, given typed with
<@\MMO\core\printers\PrintOptions> or as an array:
```php
use MMO\core\printers\{CsvPrinter, HtmlPrinter, PrintOptions, TextPrinter};
echo HtmlPrinter::Print('user input', new PrintOptions(escape: true)); // <b>user input</b>
echo TextPrinter::Print(['a' => [1, 2]], new PrintOptions(indent: 2, maxLevel: 3));
echo CsvPrinter::Encode([['name' => 'Ada', 'age' => 36]], new PrintOptions(headers: ['Name', 'Age'], separator: ','));
```
With `PrintOptions`, a misspelled option is an error ("Unknown named parameter $escpae"); in an array, it is silently
ignored. What takes an array only (<@\MMO\core\interfaces\iRenderable::render>, a printer's `__invoke()`) takes
`->toArray()`.
## <@\MMO\core\printers\JsonPrinter>
```php
class MyObject {
protected int $x = 66;
}
$x = new MyObject();
//you can pass flags for the encoding process
echo \MMO\core\printers\JsonPrinter::Encode($x, [], \JSON_BIGINT_AS_STRING);
//{x:66}
```
- Override method: <@\JsonSerializable::jsonSerialize>.
```php
class MyObject implements \JsonSerializable {
protected int $x = 66;
public function jsonSerialize(array $opts = []): mixed {
//Be very carrefull here not to call JsonPrinter::Export($this). It will create an infinite loop
return ['data' => \MMO\core\printers\JsonPrinter::ExportProperties($this, $opts)];
}
}
$x = new MyObject();
//you can pass flags for the encoding process
echo \MMO\core\printers\JsonPrinter::Encode($x, [], \JSON_BIGINT_AS_STRING);
//{data: {x:66}}
```
## <@\MMO\core\printers\TextPrinter>
- Override method: <\MMO\core\interfaces\iRenderable::render>.
- An entity is printed as its `toArray()`, as <@\MMO\core\printers\JsonPrinter> exports it: a hidden property (`@hidden`,
a password's hash) is left out, even named in `fields`. So do <@\MMO\core\printers\HtmlPrinter>,
<@\MMO\core\printers\XmlPrinter> and <@\MMO\core\printers\ShellPrinter>, which extend it. Dumped, an entity shows its
properties, protected ones included, but a hidden one by its name only:
```php
echo HtmlPrinter::Print($user); // the same as HtmlPrinter::Print($user->toArray()): no passhash
\MMO\dump($user); // ... passhash: [hidden] ...
```
## <@\MMO\core\printers\CsvPrinter>
- Override method: not implemented.
## References
### See also
- [converters](Converters.md)
## Tags
<#coercer>, <#printer>, <#converter>, <#attribute>