Skip to content

Commit

Permalink
Merge abea769 into c3d4381
Browse files Browse the repository at this point in the history
  • Loading branch information
rozsival committed Oct 30, 2020
2 parents c3d4381 + abea769 commit d426033
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 23 deletions.
30 changes: 15 additions & 15 deletions src/PropsControl/Helpers/ClassName.php
Expand Up @@ -17,14 +17,16 @@ class ClassName

public const PROP = 'className';

private const BLOCK_DELIMITER = '-';

private const ELEMENT_DELIMITER = '__';

private const MODIFIER_DELIMITER = '--';

private const SUB_BLOCK_DELIMITER = '-';

private string $baseClass;

private string $blockDelimiter;

private string $elementDelimiter;

private string $modifierDelimiter;
Expand All @@ -34,15 +36,13 @@ class ClassName
*/
private $modifiersCallback;

private string $subBlockDelimiter;

public function __construct(string $baseClass, ?callable $modifiersCallback = null)
{
$this->baseClass = $baseClass;
$this->blockDelimiter = self::BLOCK_DELIMITER;
$this->elementDelimiter = self::ELEMENT_DELIMITER;
$this->modifierDelimiter = self::MODIFIER_DELIMITER;
$this->modifiersCallback = $modifiersCallback;
$this->subBlockDelimiter = self::SUB_BLOCK_DELIMITER;
}

public function block(?string ...$modifiers): string
Expand All @@ -53,10 +53,10 @@ public function block(?string ...$modifiers): string
return $this->composeClassNames($this->baseClass, $modifiers);
}

public function create(string $baseClass, bool $subBlock = true, bool $excludeModifiers = false): self
public function create(string $baseClass, bool $block = true, bool $excludeModifiers = false): self
{
if ($subBlock) {
$baseClass = $this->baseClass . $this->subBlockDelimiter . $baseClass;
if ($block) {
$baseClass = $this->baseClass . $this->blockDelimiter . $baseClass;
}
return new self($baseClass, $excludeModifiers ? null : $this->modifiersCallback);
}
Expand All @@ -69,7 +69,7 @@ public function element(string $className, ?string ...$modifiers): string
public function extra(string $className, string $prefix = ''): string
{
if ($prefix !== '') {
return $prefix . $this->subBlockDelimiter . $className;
return $prefix . $this->blockDelimiter . $className;
}
return $className;
}
Expand All @@ -79,21 +79,21 @@ public function getBaseClass(): string
return $this->baseClass;
}

public function setElementDelimiter(string $elementDelimiter): self
public function setBlockDelimiter(string $blockDelimiter): self
{
$this->elementDelimiter = $elementDelimiter;
$this->blockDelimiter = $blockDelimiter;
return $this;
}

public function setModifierDelimiter(string $modifierDelimiter): self
public function setElementDelimiter(string $elementDelimiter): self
{
$this->modifierDelimiter = $modifierDelimiter;
$this->elementDelimiter = $elementDelimiter;
return $this;
}

public function setSubBlockDelimiter(string $subBlockDelimiter): self
public function setModifierDelimiter(string $modifierDelimiter): self
{
$this->subBlockDelimiter = $subBlockDelimiter;
$this->modifierDelimiter = $modifierDelimiter;
return $this;
}

Expand Down
4 changes: 1 addition & 3 deletions src/PropsControl/Helpers/PropTypes.php
Expand Up @@ -24,9 +24,7 @@ public static function booleans(string ...$props): array
/** @var array<string, Schema> $booleans */
$booleans = Arrays::mapKeysFromValues(
$props,
function (string $prop): array {
return [$prop, Expect::bool(false)];
}
fn(string $prop): array => [$prop, Expect::bool(false)]
);
return $booleans;
}
Expand Down
6 changes: 6 additions & 0 deletions src/PropsControl/PropsControl.php
Expand Up @@ -28,6 +28,8 @@ abstract class PropsControl extends BaseControl

private const CLASS_NAME = ClassName::PROP;

private const DEFINITION = 'definition';

private const MODIFIERS = 'modifiers';

private const PROPS = 'props';
Expand All @@ -50,6 +52,7 @@ public function __construct()
function (Template $template): void {
$template->setParameters(
[
self::DEFINITION => $this->getProps(),
self::CLASS_NAME => $this->createClassName(),
self::STYLE => new Style(),
]
Expand Down Expand Up @@ -135,6 +138,9 @@ final public function getProps(): Props
return $this->createProps($props ? (array)$props : []);
}

/**
* @return class-string<Props>
*/
abstract protected function getPropsClass(): string;

protected function beforeMapPropsToTemplate(ValidProps $props): void
Expand Down
2 changes: 2 additions & 0 deletions src/PropsControl/PropsControlTemplate.php
Expand Up @@ -11,6 +11,8 @@ class PropsControlTemplate extends Template

public ClassName $className;

public Props $definition;

/**
* @var string[]
*/
Expand Down
2 changes: 1 addition & 1 deletion tests/PropsControlTests/Components/InvalidComponent.php
Expand Up @@ -9,7 +9,7 @@ class InvalidComponent extends PropsControl

protected function getPropsClass(): string
{
return '';
return ''; // @phpstan-ignore-line
}

}
Expand Up @@ -65,6 +65,9 @@ protected function beforeRender(ValidProps $props): void
// do stuff before component is rendered
}

/**
* @return class-string<TestComponentProps>
*/
protected function getPropsClass(): string
{
return TestComponentProps::class;
Expand Down
@@ -1,9 +1,11 @@
{templateType Wavevision\PropsControl\PropsControlTemplate}
{varType Wavevision\PropsControlTests\Components\TestComponent\TestComponentProps $definition}
<div n:class="$className->block()" n:attr="style => $style">
<div n:class="$className->element(first-child)">
{$props->string}
{$props->get($definition::STRING)}
</div>
<div n:class="$className->element(collection)" n:inner-foreach="$props->collection as $item">
<p>{$item->one} / {$item->two}</p>
<div n:class="$className->element(collection)" n:inner-foreach="$props->get($definition::COLLECTION) as $item">
<p>{$item->{$definition::TYPE_ONE}} / {$item->{$definition::TYPE_TWO}}</p>
</div>
{var $partClassName = $className->create(part)}
<div n:class="$partClassName->block()" n:inner-foreach="[1,2,3,4,5] as $part">
Expand Down
2 changes: 1 addition & 1 deletion tests/PropsControlTests/Helpers/ClassNameTest.php
Expand Up @@ -30,7 +30,7 @@ public function testCreate(): void
$this->assertInstanceOf(ClassName::class, $sub2);
$this->assertInstanceOf(ClassName::class, $sub1->setElementDelimiter(''));
$this->assertInstanceOf(ClassName::class, $sub1->setModifierDelimiter(''));
$this->assertInstanceOf(ClassName::class, $sub1->setSubBlockDelimiter(''));
$this->assertInstanceOf(ClassName::class, $sub1->setBlockDelimiter(''));
}

public function testElement(): void
Expand Down

0 comments on commit d426033

Please sign in to comment.