-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* style: alphabetically sort and indenting * feat: add video block * chore: remove unused imports * chore: reorder named arguments * feat: add valid url property rule
- Loading branch information
1 parent
04bddbc
commit ff33581
Showing
7 changed files
with
254 additions
and
38 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,129 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SlackPhp\BlockKit\Blocks; | ||
|
||
use SlackPhp\BlockKit\Parts\PlainText; | ||
use SlackPhp\BlockKit\Property; | ||
use SlackPhp\BlockKit\Validation\RequiresAllOf; | ||
use SlackPhp\BlockKit\Validation\ValidString; | ||
use SlackPhp\BlockKit\Validation\ValidUrl; | ||
|
||
#[RequiresAllOf('alt_text', 'title', 'thumbnail_url', 'video_url')] | ||
class Video extends Block | ||
{ | ||
#[Property, ValidString(200)] | ||
public ?PlainText $title; | ||
|
||
#[Property('video_url'), ValidUrl] | ||
public ?string $videoUrl; | ||
|
||
#[Property('thumbnail_url'), ValidUrl] | ||
public ?string $thumbnailUrl; | ||
|
||
#[Property('alt_text'), ValidString] | ||
public ?string $altText; | ||
|
||
#[Property, ValidString(200)] | ||
public ?PlainText $description; | ||
|
||
#[Property('author_name'), ValidString(50)] | ||
public ?string $authorName; | ||
|
||
#[Property('title_url'), ValidUrl] | ||
public ?string $titleUrl; | ||
|
||
#[Property('provider_name'), ValidString] | ||
public ?string $providerName; | ||
|
||
#[Property('provider_icon_url'), ValidUrl] | ||
public ?string $providerIconUrl; | ||
|
||
public function __construct( | ||
PlainText|string|null $title = null, | ||
?string $videoUrl = null, | ||
?string $thumbnailUrl = null, | ||
?string $altText = null, | ||
PlainText|string|null $description = null, | ||
?string $authorName = null, | ||
?string $titleUrl = null, | ||
?string $providerName = null, | ||
?string $providerIconUrl = null, | ||
?string $blockId = null, | ||
) { | ||
parent::__construct($blockId); | ||
$this->title($title); | ||
$this->videoUrl($videoUrl); | ||
$this->thumbnailUrl($thumbnailUrl); | ||
$this->altText($altText); | ||
$this->description($description); | ||
$this->authorName($authorName); | ||
$this->titleUrl($titleUrl); | ||
$this->providerName($providerName); | ||
$this->providerIconUrl($providerIconUrl); | ||
} | ||
|
||
public function title(PlainText|string|null $title): self | ||
{ | ||
$this->title = PlainText::wrap($title); | ||
|
||
return $this; | ||
} | ||
|
||
public function videoUrl(?string $videoUrl): self | ||
{ | ||
$this->videoUrl = $videoUrl; | ||
|
||
return $this; | ||
} | ||
|
||
public function thumbnailUrl(?string $thumbnailUrl): self | ||
{ | ||
$this->thumbnailUrl = $thumbnailUrl; | ||
|
||
return $this; | ||
} | ||
|
||
public function altText(?string $altText): self | ||
{ | ||
$this->altText = $altText; | ||
|
||
return $this; | ||
} | ||
|
||
public function description(PlainText|string|null $description): self | ||
{ | ||
$this->description = PlainText::wrap($description); | ||
|
||
return $this; | ||
} | ||
|
||
public function authorName(?string $authorName): self | ||
{ | ||
$this->authorName = $authorName; | ||
|
||
return $this; | ||
} | ||
|
||
public function titleUrl(?string $titleUrl): self | ||
{ | ||
$this->titleUrl = $titleUrl; | ||
|
||
return $this; | ||
} | ||
|
||
public function providerName(?string $providerName): self | ||
{ | ||
$this->providerName = $providerName; | ||
|
||
return $this; | ||
} | ||
|
||
public function providerIconUrl(?string $providerIconUrl): self | ||
{ | ||
$this->providerIconUrl = $providerIconUrl; | ||
|
||
return $this; | ||
} | ||
} |
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
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,26 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SlackPhp\BlockKit\Validation; | ||
|
||
use Attribute; | ||
use SlackPhp\BlockKit\Component; | ||
|
||
#[Attribute(Attribute::TARGET_PROPERTY)] | ||
class ValidUrl implements PropertyRule | ||
{ | ||
public function check(Component $component, string $field, mixed $value): void | ||
{ | ||
if ($value === null) { | ||
return; | ||
} | ||
|
||
if (! \is_string($value) || filter_var($value, FILTER_VALIDATE_URL) === false) { | ||
throw new ValidationException( | ||
'The "%s" field of a valid "%s" component must be a valid URL', | ||
[$field, $component->type->value], | ||
); | ||
} | ||
} | ||
} |
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,32 @@ | ||
<?php | ||
|
||
namespace SlackPhp\BlockKit\Tests\Functional; | ||
|
||
use SlackPhp\BlockKit\Blocks\Block; | ||
use SlackPhp\BlockKit\Blocks\Video; | ||
|
||
class BlockTest extends TestCase | ||
{ | ||
/** | ||
* @param class-string<Block> $class | ||
* @dataProvider providesJsonFiles | ||
*/ | ||
public function testMatchesJsonFromReferenceDocumentation(string $file, string $class): void | ||
{ | ||
$json = $this->loadAssetJson($file); | ||
|
||
$hydrate = [$class, 'fromJson'](...); | ||
$block = $hydrate($json); | ||
$block->validate(); | ||
$newJson = $block->toJson(); | ||
|
||
$this->assertJsonStringEqualsJsonString($json, $newJson); | ||
} | ||
|
||
public static function providesJsonFiles(): array | ||
{ | ||
return [ | ||
['blocks/video', Video::class], | ||
]; | ||
} | ||
} |
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
Oops, something went wrong.