Skip to content

Commit

Permalink
Add docs Meta::class. (#277)
Browse files Browse the repository at this point in the history
  • Loading branch information
terabytesoftw committed Feb 7, 2024
1 parent 0218b7a commit d57e50e
Show file tree
Hide file tree
Showing 10 changed files with 248 additions and 92 deletions.
1 change: 1 addition & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ The following tags are currently supported:
- [img](/docs/tag/Img.md)
- [label](/docs/tag/Label.md)
- [li](/docs/tag/Li.md)
- [meta](/docs/tag/Meta.md)

## Input Tags

Expand Down
103 changes: 103 additions & 0 deletions docs/tag/Meta.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
# Meta

The `<meta>` `HTML` element represents metadata that cannot be represented by other HTML meta-related elements, like
`<base>`, `<link>`, `<script>`, `<style>`, or `<title>`.

## Basic Usage

Instantiate the `Meta` class using `Meta::widget()`.

```php
$meta = Meta::widget();
```

## Setting Attributes

Use the provided methods to set specific attributes for the a element.

```php
// setting class attribute
$meta->class('external');
```

Or, use the `attributes` method to set multiple attributes at once.

```php
$meta->attributes(['class' => 'external', 'id' => 'MyId']);
```

## Adding Content

If you want to include content within the `meta` tag, use the `content` method.

The first parameter is the `name` attribute and the second parameter is the `content` value.

```php
$meta->content('csrf', 'value');
```

### Adding HTTP-Equiv

If you want to include `http-equiv` within the `meta` tag, use the `httpEquiv` method.

The first parameter is the `http-equiv` attribute and the second parameter is the `content` value.

```php
$meta->httpEquiv('refresh', '30');
```

## Rendering

Generate the `HTML` output using the `render` method.

```php
$html = $meta->render();
```

Or, use the magic `__toString` method.

```php
$html = (string) $meta;
```

## Common Use Cases

Below are examples of common use cases:

```php
// adding multiple attributes
$meta->class('external')->content('viewport', 'width=device-width, initial-scale=1');
```

Explore additional methods for setting various attributes such as `httpEquiv`, `id`, `lang` and more.

## Attributes

Refer to the [Attribute Tests](https://github.com/php-forge/html/blob/main/tests/Meta/AttributeTest.php) for comprehensive
examples.

The following methods are available for setting attributes:

| Method | Description |
| ----------------- | ------------------------------------------------------------------------------------------------ |
| `attributes()` | Set multiple `attributes` at once. |
| `charset()` | Set the `charset` attribute. |
| `class()` | Set the `class` attribute. |
| `content()` | Set the `content` within the `meta` element. |
| `httpEquiv()` | Set the `http-equiv` attribute. |
| `id()` | Set the `id` attribute. |
| `lang()` | Set the `lang` attribute. |
| `name()` | Set the `name` attribute. |
| `style()` | Set the `style` attribute. |

## Custom methods

Refer to the [Custom Method Test](https://github.com/php-forge/html/blob/main/tests/Meta/CustomMethodTest.php) for
comprehensive examples.

The following methods are available for customizing the `HTML` output:

| Method | Description |
| ---------------------------- | ------------------------------------------------------------------------------------- |
| `render()` | Generates the `HTML` output. |
| `widget()` | Instantiates the `Meta::class`. |
30 changes: 0 additions & 30 deletions src/Attribute/Tag/HasProperty.php

This file was deleted.

4 changes: 2 additions & 2 deletions src/Base/AbstractMeta.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ abstract class AbstractMeta extends Element
use Attribute\HasClass;
use Attribute\HasId;
use Attribute\HasLang;
use Attribute\HasStyle;
use Attribute\Tag\HasCharset;
use Attribute\Tag\HasHttpEquiv;
use Attribute\Tag\HasProperty;

protected array $attributes = [];

Expand Down Expand Up @@ -48,6 +48,6 @@ public function content(string $value, string $content): static
*/
protected function run(): string
{
return Tag::widget()->attributes($this->attributes)->tagName('meta')->render();
return Tag::widget()->attributes($this->attributes)->id($this->id)->tagName('meta')->render();
}
}
5 changes: 1 addition & 4 deletions src/Meta.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,4 @@
*
* @link https://html.spec.whatwg.org/multipage/semantics.html#the-meta-element
*/
final class Meta extends Base\AbstractMeta
{
protected string $tagName = 'meta';
}
final class Meta extends Base\AbstractMeta {}
22 changes: 0 additions & 22 deletions tests/Attribute/Tag/HasPropertyTest.php

This file was deleted.

95 changes: 95 additions & 0 deletions tests/Meta/AttributeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php

declare(strict_types=1);

namespace PHPForge\Html\Tests\Meta;

use PHPForge\Html\Meta;
use PHPForge\Support\Assert;
use PHPUnit\Framework\TestCase;

/**
* @psalm-suppress PropertyNotSetInConstructor
*/
final class AttributeTest extends TestCase
{
public function testAttributes(): void
{
Assert::equalsWithoutLE(
<<<HTML
<meta class="value">
HTML,
Meta::widget()->attributes(['class' => 'value'])->render()
);
}

public function testCharset(): void
{
Assert::equalsWithoutLE(
<<<HTML
<meta charset="value">
HTML,
Meta::widget()->charset('value')->render()
);
}

public function testClass(): void
{
Assert::equalsWithoutLE(
<<<HTML
<meta class="value">
HTML,
Meta::widget()->attributes(['class' => 'value'])->render()
);
}

public function testContent(): void
{
Assert::equalsWithoutLE(
<<<HTML
<meta name="name" content="value">
HTML,
Meta::widget()->content('name', 'value')->render()
);
}

public function testHttpEquiv(): void
{
Assert::equalsWithoutLE(
<<<HTML
<meta http-equiv="name" content="value">
HTML,
Meta::widget()->httpEquiv('name', 'value')->render()
);
}

public function testId(): void
{
Assert::equalsWithoutLE(
<<<HTML
<meta id="value">
HTML,
Meta::widget()->id('value')->render()
);
}

public function testLang(): void
{
Assert::equalsWithoutLE(
<<<HTML
<meta lang="value">
HTML,
Meta::widget()->lang('value')->render()
);
}

public function testStyle(): void
{
Assert::equalsWithoutLE(
<<<HTML
<meta style="value">
HTML,
Meta::widget()->style('value')->render()
);
}
}
25 changes: 25 additions & 0 deletions tests/Meta/CustomMethodTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace PHPForge\Html\Tests\Meta;

use PHPForge\Html\Meta;
use PHPForge\Support\Assert;
use PHPUnit\Framework\TestCase;

/**
* @psalm-suppress PropertyNotSetInConstructor
*/
final class CustomMethodTest extends TestCase
{
public function testRender(): void
{
Assert::equalsWithoutLE(
<<<HTML
<meta>
HTML,
Meta::widget()->render()
);
}
}
21 changes: 21 additions & 0 deletions tests/Meta/ImmutableTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace PHPForge\Html\Tests\Meta;

use PHPForge\Html\Meta;
use PHPUnit\Framework\TestCase;

/**
* @psalm-suppress PropertyNotSetInConstructor
*/
final class ImmutableTest extends TestCase
{
public function testImmutable(): void
{
$meta = Meta::widget();

$this->assertNotSame($meta, $meta->content('', ''));
}
}
34 changes: 0 additions & 34 deletions tests/Meta/RenderTest.php

This file was deleted.

0 comments on commit d57e50e

Please sign in to comment.