Sons of PHP
Source Code
  • 🏠Home
  • Contributor Covenant Code of Conduct
  • Getting Help
  • Reporting Issues
  • Security Policy
  • 🪈Bard
    • Overview
    • Commands
  • Symfony Bundles
    • Feature Toggle
  • Contracts
    • Contracts Overview
    • Common
    • Cookie
    • CQRS
    • Filesystem
    • Mailer
    • Pager
    • Registry
    • State Machine
  • 📦Components
    • Assert
    • Cache
      • Adapters
      • Marshallers
    • Clock
    • Container
    • Cookie
    • CQRS
    • Event Dispatcher
    • Event Sourcing
      • Aggregates
        • Aggregate Repository
      • Event Messages
        • Using the Serializable Event Message
        • Message Enrichers
        • Message Serializers
        • Message Repository
        • Message Upcasters
    • Feature Toggle
    • Filesystem
      • Adapters
    • Http Factory
    • Http Handler
    • Http Message
    • JSON
    • Link
    • Logger
      • Handlers
      • Enrichers
      • Filters
      • Formatters
    • Mailer
      • Transports
    • Money
      • Currency Providers
      • Operators
      • Queries
    • Pager
      • Adapters
    • Registry
    • State Machine
    • Version
  • 💁Contributing
    • Contributing Overview
    • Contributing Code
    • Discussions
    • Documentation
Powered by GitBook
On this page
  • Installation
  • Usage
  • Assertions
  • Magic Assertions

Was this helpful?

Edit on GitHub
  1. Components

Assert

Installation

composer require sonsofphp/assert

Usage

<?php

use SonsOfPHP\Component\Assert\Assert;
use SonsOfPHP\Component\Assert\InvalidArgumentException;

$data = 'test';
Assert::string($data);

// @throws InvalidArgumentException
Assert::int($data);

// Disable exceptions
Assert::disable();

// @return bool
Assert::int($data);

// Re-enable exceptions
Assert::enable();

Assertions

Assert::array($value);
Assert::bool($value);
Assert::callable($value);
Assert::empty($value);
Assert::eq($value, $value2);
Assert::false($value);
Assert::float($value);
Assert::int($value);
Assert::null($value);
Assert::numeric($value);
Assert::object($value);
Assert::resource($value);
Assert::same($value, $value2);
Assert::scalar($value);
Assert::string($value);
Assert::true($value);

Magic Assertions

// "all"
// Throws exception if any value in $values is not the correct type
Assert::allString($values);
Assert::allString(['one', 'two', 3]); // Throws Exception

// "not"
// Throws exception if $value is the type
Assert::notString($value);
Assert::notString('opps'); // Throws Exception

// "nullOr"
// Throws exception if $value is not null or not type
Assert::nullOrString($value);
Assert::nullOrString(42); // Throws Exception
PreviousState MachineNextCache

Last updated 8 months ago

Was this helpful?

📦