Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added isAOf(), isAOfAny(), notAOf() #94

Closed
wants to merge 3 commits into from

Conversation

jbdelhommeau
Copy link
Contributor

Implement a is_a of php function like an asset : http://php.net/manual/en/function.is-a.php

An example of use case:

<?php
use Webmozart\Assert\Assert;

final class CreditNoteLines extends ArrayCollection
{
    private $creditNoteClass;

    public function __construct(string $creditNoteClass, array $elements = [])
    {
        Assert::isAOfAny($creditNoteClass, [
            CreditNoteLine::class,
            PurchaseCreditNoteLine::class
        ]);
        $this->creditNoteClass = $creditNoteClass;

        parent::__construct($elements);
    }

    public function add($accountingDocument): void
    {
        Assert::isInstanceOf($accountingDocument, $this->creditNoteClass);

        if (!$this->contains($accountingDocument)) {
            parent::add($accountingDocument);
        }
    }

    public function createFrom(array $elements)
    {
        return new static($this->creditNoteClass, $elements);
    }
}

@smoench
Copy link
Contributor

smoench commented Jun 7, 2019

@jbdelhommeau Could you do a rebase on the latest master?

public static function isAOfAny($value, array $classes, $message = '')
{
foreach ($classes as $class) {
if (!is_string($class) || is_a($value, $class, is_string($value))) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This statement must be changed to if (is_string($class) && is_a($value, $class, is_string($value))) { otherwise this test case array('isAOfAny', array('ArrayIterator', array(123)), true), would be passed. The correct test case would be array('isAOfAny', array('ArrayIterator', array(123)), false),

public static function isAOfAny($value, array $classes, $message = '')
{
foreach ($classes as $class) {
if (!is_string($class) || is_a($value, $class, is_string($value))) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would have the assumption if no string passed for $class an exception would be thrown. If you enabled declare(strict_types=1); and calling is_a() with a class variable contains an integer php would thrown a type error. I talked with @Nyholm and he agreed if $class is not a string a an invalid argument should be reported.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants