Skip to content

Conditional Attributes

Complete reference of all 18 conditional attributes for dynamic visibility.

SimpleDTO provides 18 conditional attributes organized into categories:

  • Core Conditional (9) - Framework-agnostic conditions
  • Laravel-Specific (4) - Authentication & authorization
  • Symfony-Specific (2) - Security & roles
  • Context-Based (3) - Custom context conditions
AttributeDescription
#[WhenCallback(callable)]Custom logic
#[WhenValue(string, mixed)]Property equals value
#[WhenNull(string)]Property is null
#[WhenNotNull(string)]Property not null
#[WhenTrue(string)]Property is true
#[WhenFalse(string)]Property is false
#[WhenEquals(string, mixed)]Property equals
#[WhenIn(string, array)]Property in array
#[WhenNotIn(string, array)]Property not in array
#[WhenAuth]User authenticated (Laravel)
#[WhenGuest]User is guest (Laravel)
#[WhenCan(string)]User has permission (Laravel)
#[WhenRole(string)]User has role (Laravel)
#[WhenGranted(string)]User granted (Symfony)
#[WhenSymfonyRole(string)]User has role (Symfony)
#[WhenContext(string)]Context is truthy
#[WhenContextEquals(string, mixed)]Context equals
#[WhenContextIn(string, array)]Context in array
// Custom logic
#[WhenCallback(fn($value, $dto) => strlen($value) > 0)]
public readonly ?string $bio = null;
// Property equals value
#[WhenValue('status', 'published')]
public readonly ?string $publishedAt = null;
// Property is null
#[WhenNull('deletedAt')]
public readonly ?string $activeStatus = null;
// Property not null
#[WhenNotNull('verifiedAt')]
public readonly ?string $verifiedBadge = null;
// Property is true
#[WhenTrue('isPublished')]
public readonly ?string $publishedUrl = null;
// Property is false
#[WhenFalse('isActive')]
public readonly ?string $inactiveReason = null;
// Property equals (alias for WhenValue)
#[WhenEquals('role', 'admin')]
public readonly ?array $adminPanel = null;
// Property in array
#[WhenIn('role', ['admin', 'moderator'])]
public readonly ?array $moderationTools = null;
// Property not in array
#[WhenNotIn('role', ['guest', 'banned'])]
public readonly ?array $premiumFeatures = null;
// User authenticated
#[WhenAuth]
public readonly ?string $email = null;
// User is guest
#[WhenGuest]
public readonly ?string $registerPrompt = null;
// User has permission
#[WhenCan('edit-posts')]
public readonly ?string $editUrl = null;
// User has role
#[WhenRole('admin')]
public readonly ?array $adminPanel = null;
// Multiple roles (OR logic)
#[WhenRole(['admin', 'moderator'])]
public readonly ?array $moderationPanel = null;
// User granted
#[WhenGranted('ROLE_ADMIN')]
public readonly ?array $adminData = null;
// User has role
#[WhenSymfonyRole('ROLE_ADMIN')]
public readonly ?array $adminPanel = null;
// Context is truthy
#[WhenContext('debug')]
public readonly ?array $debugInfo = null;
// Use with context
$dto->withContext(['debug' => true]);
// Context equals value
#[WhenContextEquals('environment', 'development')]
public readonly ?array $debugData = null;
// Context in array
#[WhenContextIn('environment', ['development', 'staging'])]
public readonly ?array $debugInfo = null;
class UserProfileDTO extends SimpleDTO
{
public function __construct(
public readonly string $name,
#[WhenAuth]
public readonly ?string $email = null,
#[WhenRole('admin')]
public readonly ?string $ipAddress = null,
) {}
}
class DashboardDTO extends SimpleDTO
{
public function __construct(
public readonly string $title,
#[WhenRole('admin')]
public readonly ?array $userManagement = null,
#[WhenCan('view-logs')]
public readonly ?array $systemLogs = null,
) {}
}
// ✅ Good
#[WhenAuth]
public readonly ?string $email;
// ❌ Bad
#[WhenCallback(fn() => auth()->check())]
public readonly ?string $email;
// Multiple conditions (AND logic)
#[WhenAuth, WhenRole('admin')]
public readonly ?array $adminData = null;