Skip to content

Latest commit

 

History

History
79 lines (58 loc) · 2.12 KB

File metadata and controls

79 lines (58 loc) · 2.12 KB

Options

A modern package for WordPress option management with validation, immutable value objects, and hexagonal architecture.

Basic Usage

use Pollora\Option\Application\Service\OptionService;
use Pollora\Option\Adapter\Out\WordPress\WordPressOptionRepository;
use Pollora\Option\Domain\Service\OptionValidationService;

$service = new OptionService(
    new WordPressOptionRepository,
    new OptionValidationService
);

$value = $service->get('site_title', 'Default');
$service->set('site_title', 'New Title');
$service->update('site_title', 'Updated Title');
$service->delete('old_option');
$exists = $service->exists('site_title');

Working with Option Objects

The Option value object is immutable — use withValue() and withAutoload() to derive new instances:

use Pollora\Option\Domain\Model\Option;

$option = new Option('api_credentials', [
    'key' => 'abc123',
    'secret' => 'xyz789'
], autoload: false);

$updated = $option->withValue(['key' => 'new123', 'secret' => 'new789']);

Validation

Keys and values are validated automatically by OptionValidationService:

  • Keys: must be 1–191 characters, no null bytes
  • Values: must be serializable (no resources, no closures)
use Pollora\Option\Domain\Exception\InvalidOptionException;

try {
    $service->set('', 'value'); // throws: key cannot be empty
} catch (InvalidOptionException $e) {
    // handle
}

Error Handling

use Pollora\Option\Domain\Exception\OptionNotFoundException;
use Pollora\Option\Domain\Exception\InvalidOptionException;

try {
    $value = $service->get('required_key');
    if ($value === null) {
        throw new OptionNotFoundException('required_key');
    }
} catch (OptionNotFoundException $e) {
    // "Option 'required_key' not found"
}

Architecture

This package follows Hexagonal Architecture (Ports & Adapters):

  • DomainOption value object, OptionValidationService, exceptions
  • ContractOptionRepositoryInterface port
  • ApplicationOptionService orchestration
  • AdapterWordPressOptionRepository (WordPress get_option() / update_option())