Skip to content

Commit

Permalink
Merge pull request #70 from justcoded/feature/field_wrapper_methods
Browse files Browse the repository at this point in the history
Added methods to work with wrapper
  • Loading branch information
stevep authored Sep 14, 2018
2 parents 43d1b68 + 5379bfb commit 68e3705
Show file tree
Hide file tree
Showing 2 changed files with 204 additions and 0 deletions.
94 changes: 94 additions & 0 deletions src/FieldBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,100 @@ public function conditional($name, $operator, $value)
return $conditionalBuilder;
}

/**
* Set Wrapper container tag attributes
*
* @param array $config
*
* @return FieldBuilder
*/
public function setWrapper($config)
{
return $this->setConfig('wrapper', $config);
}

/**
* Get Wrapper container tag attributes
*
* @return array|mixed
*/
public function getWrapper()
{
if (isset($this->config['wrapper'])) {
return $this->config['wrapper'];
}

return [];
}

/**
* Set width of a Wrapper container
*
* @param string $width Width of a container in % or px.
*
* @return FieldBuilder
*/
public function setWidth($width)
{
$wrapper = $this->getWrapper();
$wrapper['width'] = $width;

return $this->setWrapper($wrapper);
}

/**
* Set specified Attr of a Wrapper container
*
* @param string $name Attribute name, ex. 'class'.
* @param string|null $value Attribute value, ex. 'my-class'.
*
* @return FieldBuilder
*/
public function setAttr($name, $value = null)
{
$wrapper = $this->getWrapper();

// set attribute.
$wrapper[$name] = $value;

return $this->setWrapper($wrapper);
}

/**
* Set Class and/or ID attribute of a Wrapper container
* use CSS-like selector string to specify css or id
* example: #my-id.foo-class.bar-class
*
* @param string $css_selector
*
* @return FieldBuilder
*/
public function setSelector($css_selector)
{
// if # is the first sign - we start with ID.
if (0 === strpos($css_selector, '#')) {
$css_selector .= '.'; // prevent empty second part.
list($id, $class) = explode('.', $css_selector, 2);
} else {
$css_selector .= '#'; // prevent empty second part.
list($class, $id) = explode('#', $css_selector, 2);
}

$id = trim($id, '#');
$class = trim($class, '.');

if (! empty($id)) {
$this->setAttr('id', $id);
}

if (! empty($class)) {
$class = str_replace('.', ' ', $class);
$this->setAttr('class', $class);
}

return $this;
}

/**
* Build the field configuration array
* @return array Field configuration array
Expand Down
110 changes: 110 additions & 0 deletions tests/FieldBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,116 @@ public function testSetDefaultValue()
], $subject->build());
}

public function testGetWrapper()
{
$wrapper = [
'class' => 'foo',
'id' => 'bar',
];
$subject = new FieldBuilder('my_field', 'text', ['wrapper' => $wrapper]);
$this->assertSame($subject, $subject->setConfig('prepend', '@'));
$this->assertArraySubset($wrapper, $subject->getWrapper());
}

public function testSetWrapper()
{
$subject = new FieldBuilder('my_field', 'text');
$this->assertSame($subject, $subject->setWrapper(['class' => 'foo', 'id' => 'bar']));
$this->assertArraySubset([
'key' => 'field_my_field',
'name' => 'my_field',
'label' => 'My Field',
'type' => 'text',
'wrapper' => [
'class' => 'foo',
'id' => 'bar',
],
], $subject->build());
}

public function testSetWidth()
{
$subject = new FieldBuilder('my_field', 'text');
$this->assertSame($subject, $subject->setWidth('50%'));
$this->assertArraySubset([
'key' => 'field_my_field',
'name' => 'my_field',
'label' => 'My Field',
'type' => 'text',
'wrapper' => [
'width' => '50%',
],
], $subject->build());
}

public function testSetAttr()
{
$subject = new FieldBuilder('my_field', 'text');
$this->assertSame($subject, $subject->setAttr('data-my_attr', 'My Attr'));
$this->assertArraySubset([
'key' => 'field_my_field',
'name' => 'my_field',
'label' => 'My Field',
'type' => 'text',
'wrapper' => [
'data-my_attr' => 'My Attr',
],
], $subject->build());
}

public function testSetSelector()
{
$subject = new FieldBuilder('my_field', 'text');
// returns FieldBuilder.
$this->assertSame($subject, $subject->setSelector('.my-class'));

// only id.
$subject->setSelector('#my-id');
$this->assertArraySubset([
'id' => 'my-id',
], $subject->getWrapper());

// only class.
$subject->setSelector('.my-class');
$this->assertArraySubset([
'class' => 'my-class',
], $subject->getWrapper());

// only class multiple.
$subject->setSelector('.class1.class2');
$this->assertArraySubset([
'class' => 'class1 class2',
], $subject->getWrapper());

// id / class.
$subject->setSelector('#my-id.my-class');
$this->assertArraySubset([
'id' => 'my-id',
'class' => 'my-class',
], $subject->getWrapper());

// id / class multiple.
$subject->setSelector('#my-id.my-class.more-class');
$this->assertArraySubset([
'id' => 'my-id',
'class' => 'my-class more-class',
], $subject->getWrapper());

// class /id.
$subject->setSelector('.my-class#my-id');
$this->assertArraySubset([
'id' => 'my-id',
'class' => 'my-class',
], $subject->getWrapper());

// class multiple /id.
$subject->setSelector('.my-class.more-class#my-id');
$this->assertArraySubset([
'id' => 'my-id',
'class' => 'my-class more-class',
], $subject->getWrapper());
}

public function testConditional()
{
$subject = new FieldBuilder('my_field', 'text', ['prepend' => '$']);
Expand Down

0 comments on commit 68e3705

Please sign in to comment.