tobento / service-support
Interfaces and classes for supporting applications.
Requires
- php: >=8.4
- psr/http-message: ^2.0
Requires (Dev)
- phpunit/phpunit: ^12.3
- vimeo/psalm: ^6.13
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
The Support Service provides interfaces and classes supporting applications.
Table of Contents
Getting started
Add the latest version of the support service running this command.
composer require tobento/service-support
Requirements
- PHP 8.4 or greater
Documentation
Interfaces
Arrayable
use Tobento\Service\Support\Arrayable; interface Arrayable { /** * Get the object as an array. * * @return array */ public function toArray(): array; }
Htmlable
use Tobento\Service\Support\Htmlable; interface Htmlable { /** * Get content as a string of HTML. * * @return string */ public function toHtml(): string; }
Jsonable
use Tobento\Service\Support\Jsonable; interface Jsonable { /** * Get the object as a JSON string. * * @param int $options * @return string */ public function toJson(int $options = 0): string; }
Renderable
use Tobento\Service\Support\Renderable; interface Renderable { /** * Get the evaluated contents of the object. * * @return string */ public function render(): string; }
Responsable
use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use Tobento\Service\Support\Responsable; interface Responsable { /** * Create a HTTP response that represents the object. * * @param ServerRequestInterface $request * @return ResponseInterface */ public function toResponse(ServerRequestInterface $request): ResponseInterface; }
Content Disposition
The ContentDisposition::class creates a safe Content-Disposition header value for any UTF-8 filename. Browsers display the original filename, while older clients use an ASCII-only fallback.
use Tobento\Service\Support\ContentDisposition; $value = ContentDisposition::header('Rechnung Müller.pdf'); // attachment; filename="rechnung-mueller.pdf"; filename*=UTF-8''Rechnung%20M%C3%BCller.pdf // Display the file in the browser instead of downloading it: $value = ContentDisposition::header('Rechnung Müller.pdf', inline: true); // inline; filename="rechnung-mueller.pdf"; filename*=UTF-8''Rechnung%20M%C3%BCller.pdf // Use it with any PSR-7 response: $response = $response->withHeader('Content-Disposition', $value);
Html String
The HtmlString::class can be useful in certain situations where a string should not be escaped by another function.
use Tobento\Service\Support\HtmlString; $htmlString = new HtmlString('<h1>foo</h1>'); // you may check if the string is empty or not: var_dump($htmlString->isEmpty()); // bool(false)
Str
The Str::class provides static string helper methods.
Esc
Escapes a string with htmlspecialchars. Objects implementing the Htmlable interface are not escaped.
use Tobento\Service\Support\HtmlString; use Tobento\Service\Support\Str; var_dump(Str::esc('<p>"Tom" & Jerry</p>')); // string(46) "<p>"Tom" & Jerry</p>" var_dump(Str::esc(new HtmlString('<p>foo</p>'))); // string(10) "<p>foo</p>" // Do not encode existing html entities: var_dump(Str::esc('&', double_encode: false)); // string(5) "&"
Lower
Converts a string to lower case, including UTF-8 characters.
use Tobento\Service\Support\Str; var_dump(Str::lower('ÄPFEL Und Birnen')); // string(17) "äpfel und birnen"
Slug
Converts a string to a lowercase slug containing only ASCII letters, numbers, underscores and the separator.
use Tobento\Service\Support\Str; var_dump(Str::slug('Äpfel & Birnen (frisch)')); // string(20) "aepfel-birnen-frisch" // With a custom separator: var_dump(Str::slug('Lorem Ipsum', '_')); // string(11) "lorem_ipsum"
Snake
Converts a string to snake case.
use Tobento\Service\Support\Str; var_dump(Str::snake('fooBarBaz')); // string(11) "foo_bar_baz" // With a custom delimiter: var_dump(Str::snake('fooBar', '-')); // string(7) "foo-bar"