0 follower

Final Class Yiisoft\Yii\DataView\GridView\Column\Base\Cell

InheritanceYiisoft\Yii\DataView\GridView\Column\Base\Cell

Cell represents a single grid cell with content and attributes.

Method Details

Hide inherited methods

__construct() public method

Creates a new cell instance.

public mixed __construct ( array $attributes = [], boolean|null $encode null, string|\Stringable $content )
$attributes array

HTML attributes for the cell as name-value pairs.

$encode boolean|null

Whether to encode content. See {@see \Yiisoft\Yii\DataView\GridView\Column\Base\encode()} for details.

$content string|\Stringable

Cell content items. Multiple items can be provided and will be concatenated when rendered.

                public function __construct(
    private array $attributes = [],
    private ?bool $encode = null,
    string|Stringable ...$content,
) {
    $this->content = $content;
}

            
addAttributes() public method

Add attributes to existing cell attributes.

This method merges new attributes with existing ones. If an attribute already exists, it will be replaced with the new value.

public self addAttributes ( array $attributes )
$attributes array

HTML attributes as name-value pairs.

return self

New instance with merged attributes.

                public function addAttributes(array $attributes): self
{
    $new = clone $this;
    $new->attributes = array_merge($new->attributes, $attributes);
    return $new;
}

            
addClass() public method

Add a CSS class to the cell.

This method safely adds a CSS class to the cell's class attribute, maintaining any existing classes.

public self addClass ( string|string[]|null $class )
$class string|string[]|null

The CSS class(es) to be added. Null values will be ignored.

return self

New instance with added CSS class.

                public function addClass(string|array|null $class): self
{
    $new = clone $this;
    Html::addCssClass($new->attributes, $class);
    return $new;
}

            
attribute() public method

Set a single attribute value.

This is a convenience method for setting a single attribute without affecting other existing attributes.

public self attribute ( string $name, mixed $value )
$name string

Attribute name.

$value mixed

Attribute value.

return self

New instance with updated attribute.

                public function attribute(string $name, mixed $value): self
{
    $new = clone $this;
    $new->attributes[$name] = $value;
    return $new;
}

            
attributes() public method

Replace all attributes with a new set.

Unlike {@see \Yiisoft\Yii\DataView\GridView\Column\Base\addAttributes()}, this method completely replaces all existing attributes with the new set.

public self attributes ( array $attributes )
$attributes array

HTML attributes as name-value pairs.

return self

New instance with replaced attributes.

                public function attributes(array $attributes): self
{
    $new = clone $this;
    $new->attributes = $attributes;
    return $new;
}

            
content() public method

Set the cell content.

Multiple content parts can be provided and will be concatenated when rendered. Each part can be either a string or an object implementing {@see \Stringable}.

public self content ( string|\Stringable $parts )
$parts string|\Stringable

Cell content parts.

return self

New instance with updated content.

                public function content(string|Stringable ...$parts): self
{
    $new = clone $this;
    $new->content = $parts;
    return $new;
}

            
doubleEncode() public method

Set whether to double-encode HTML entities in content.

This is useful when the content might already contain encoded entities, and you need to control whether they should be encoded again.

public self doubleEncode ( boolean $enabled )
$enabled boolean

Whether to double encode HTML entities. Set to false if content already contains encoded entities.

return self

New instance with updated double encode setting.

                public function doubleEncode(bool $enabled): self
{
    $new = clone $this;
    $new->doubleEncode = $enabled;
    return $new;
}

            
encode() public method

Set content encoding behavior.

This method controls how the cell content is encoded to prevent XSS attacks while allowing intentional HTML content when needed.

public self encode ( boolean|null $enabled )
$enabled boolean|null

Whether to encode tag content. Supported values:

  • null: stringable objects implementing {@see \Yiisoft\Html\NoEncodeStringableInterface} aren't encoded, everything else is encoded (default behavior)
  • true: any content is encoded, regardless of type
  • false: nothing is encoded, use with caution and only for trusted content
return self

New instance with updated encoding setting.

                public function encode(?bool $enabled): self
{
    $new = clone $this;
    $new->encode = $enabled;
    return $new;
}

            
getAttributes() public method

Get cell HTML attributes.

public array getAttributes ( )
return array

HTML attributes as name-value pairs.

                public function getAttributes(): array
{
    return $this->attributes;
}

            
getContent() public method

Get cell content parts.

public array getContent ( )
return array

Array of content parts.

                public function getContent(): array
{
    return $this->content;
}

            
isEmptyContent() public method

Check if cell content is empty.

A cell is considered empty if all its content parts are empty strings when converted to string representation.

public boolean isEmptyContent ( )
return boolean

Whether all content parts are empty strings.

                public function isEmptyContent(): bool
{
    foreach ($this->content as $part) {
        if ((string) $part !== '') {
            return false;
        }
    }
    return true;
}

            
shouldDoubleEncode() public method

Get double encode setting.

public boolean shouldDoubleEncode ( )
return boolean

Whether HTML entities in content should be double encoded.

                public function shouldDoubleEncode(): bool
{
    return $this->doubleEncode;
}

            
shouldEncode() public method

Get content encoding setting.

public boolean|null shouldEncode ( )
return boolean|null

Current encoding setting:

  • null: default encoding behavior
  • true: force encoding
  • false: disable encoding

                public function shouldEncode(): ?bool
{
    return $this->encode;
}