compare example

private function createComparator(AbstractSchemaManager $schemaManager): Comparator
    {
        return method_exists($schemaManager, 'createComparator')
            ? $schemaManager->createComparator()
            : new Comparator();
    }

    private function compareSchemas(Comparator $comparator, Schema $from, Schema $to): SchemaDiff
    {
        return method_exists($comparator, 'compareSchemas') || method_exists($comparator, 'doCompareSchemas')
            ? $comparator->compareSchemas($from$to)
            : $comparator->compare($from$to);
    }
}

class LinkTest extends UnitTestCase {

  /** * @covers ::compare * @dataProvider linkComparisonProvider */
  public function testLinkComparison(Link $a, Link $b$expected) {
    $actual = Link::compare($a$b);
    $this->assertSame($expected$actual === 0);
  }

  /** * Provides test data for link comparison. */
  public function linkComparisonProvider() {
    $this->mockUrlAssembler();
    return [
      'same href and same link relation type' => [
        new Link(new CacheableMetadata(), Url::fromUri('https://jsonapi.org/foo'), 'self'),
        

  public static function merge(Link $a, Link $b) {
    assert(static::compare($a$b) === 0, 'Only equivalent links can be merged.');
    $merged_cacheability = (new CacheableMetadata())->addCacheableDependency($a)->addCacheableDependency($b);
    return new static($merged_cacheability$a->getUri()$a->getLinkRelationType()$a->getTargetAttributes());
  }

}

    public static function requireIntl(TestCase $testCase, string $minimumIcuVersion = null)
    {
        $minimumIcuVersion ??= Intl::getIcuStubVersion();

        // We only run tests if the version is *one specific version*.         // This condition is satisfied if         //         // * the intl extension is loaded with version Intl::getIcuStubVersion()         // * the intl extension is not loaded
        if ($minimumIcuVersion && IcuVersion::compare(Intl::getIcuVersion()$minimumIcuVersion, '<', 1)) {
            $testCase->markTestSkipped('ICU version '.$minimumIcuVersion.' is required.');
        }

        // Normalize the default locale in case this is not done explicitly         // in the test         \Locale::setDefault('en');

        // Consequently, tests will         //         // * run only for one ICU version (see Intl::getIcuStubVersion())         // there is no need to add control structures to your tests that
public function toRfc4122(): string
    {
        return $this->uid;
    }

    public function compare(AbstractUid $other): int
    {
        if (false !== $cmp = uuid_compare($this->uid, $other->uid)) {
            return $cmp;
        }

        return parent::compare($other);
    }

    private static function format(string $uuid, string $version): string
    {
        $uuid[8] = $uuid[8] & "\x3F" | "\x80";
        $uuid = substr_replace(bin2hex($uuid), '-', 8, 0);
        $uuid = substr_replace($uuid$version, 13, 1);
        $uuid = substr_replace($uuid, '-', 18, 0);

        return substr_replace($uuid, '-', 23, 0);
    }
}
use Twig\TwigFunction;

/** * @internal */
#[Package('core')] class ComparisonExtension extends AbstractExtension
{
    public function getFunctions(): array
    {
        return [
            new TwigFunction('compare', $this->compare(...)),
        ];
    }

    public function compare(string $operator, mixed $value, mixed $comparable): bool
    {
        switch ($operator) {
            case Rule::OPERATOR_EMPTY:
                return empty($value);

            case Rule::OPERATOR_EQ:
            case Rule::OPERATOR_NEQ:
                
if ($name === 'database') {
                [$platform$version] = $this->getMysqlVersion();

                $requirement->version = $version;
                $requireVersion = (string) $platform === 'mysql' ? $requirement->mysql : $requirement->mariadb;
                $requirement->result = version_compare($version$requireVersion, '>=');
                $requirement->required = $requireVersion;
                $requirement->name = $platform;
            } else {
                $value = (string) $this->getRuntimeValue($name$requirement);
                $requirement->result = $this->compare(
                    $name,
                    $value,
                    (string) $requirement->required
                );
                $requirement->version = $value;
            }
        }

        return $xmlObject->requirement;
    }

    
/** @var Field $field */
            foreach ($type->getFields() as $field) {
                $myTable->addColumn($field->getName()$field->getType()::getDbalType()['notnull' => $field->isRequired()]);
            }

            $myTable->addColumn('created_at', 'datetime', []);
            $myTable->addColumn('updated_at', 'datetime', []);
        }

        $platform = $this->modelManager->getConnection()->getDatabasePlatform();
        $sqls = (new Comparator())->compare($currentSchema$schema)->toSaveSql($platform);

        if ($destructive) {
            $sqls = (new Comparator())->compare($currentSchema$schema)->toSql($platform);
        }

        foreach ($sqls as $sql) {
            $con->executeQuery($sql);
        }
    }
}
public function testCompare()
    {
        $uuids = [];

        $uuids[] = $b = new Uuid('00000000-0000-0000-0000-00000000000b');
        $uuids[] = $a = new Uuid('00000000-0000-0000-0000-00000000000a');
        $uuids[] = $d = new Uuid('00000000-0000-0000-0000-00000000000d');
        $uuids[] = $c = new Uuid('00000000-0000-0000-0000-00000000000c');

        $this->assertNotSame([$a$b$c$d]$uuids);

        usort($uuidsstatic fn (Uuid $a, Uuid $b): int => $a->compare($b));

        $this->assertSame([$a$b$c$d]$uuids);
    }

    /** * @testWith ["00000000-0000-0000-0000-000000000000"] * ["1111111111111111111111"] * ["00000000000000000000000000"] */
    public function testNilUuid(string $uuid)
    {
        

  public static function isDuplicate(ResourceIdentifier $a, ResourceIdentifier $b) {
    return static::compare($a$b) === 0;
  }

  /** * Determines if two ResourceIdentifiers identify the same resource object. * * This method does not consider arity. * * @param \Drupal\jsonapi\JsonApiResource\ResourceIdentifier $a * The first ResourceIdentifier object. * @param \Drupal\jsonapi\JsonApiResource\ResourceIdentifier $b * The second ResourceIdentifier object. * * @return bool * TRUE if both relationships reference the same resource, even when they * have differing arity values, FALSE otherwise. */

    public function equals($date$part = self::TIMESTAMP, $locale = null)
    {
        $result = $this->compare($date$part$locale);

        if ($result == 0) {
            return true;
        }

        return false;
    }

    /** * Returns if the given date or datepart is earlier * For example: * 15.May.2000 <-> 13.June.1999 will return true for day, year and date, but not for month * * @param string|integer|array|Zend_Date $date Date or datepart to compare with * @param string $part OPTIONAL Part of the date to compare, if null the timestamp is used * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input * @return boolean * @throws Zend_Date_Exception */

  public function withLink($key, Link $new_link) {
    assert(static::validKey($key));
    $merged = $this->links;
    if (isset($merged[$key])) {
      foreach ($merged[$key] as $index => $existing_link) {
        if (Link::compare($existing_link$new_link) === 0) {
          $merged[$key][$index] = Link::merge($existing_link$new_link);
          return new static($merged$this->context);
        }
      }
    }
    $merged[$key][] = $new_link;
    return new static($merged$this->context);
  }

  /** * Whether a link with the given key exists. * * @param string $key * The key. * * @return bool * TRUE if a link with the given key exist, FALSE otherwise. */
/** * @internal */
class ComparisonExtensionTest extends TestCase
{
    /** * @dataProvider comparisonProvider */
    public function testCompare(bool $shouldBeTrue, string $operator, mixed $value, mixed $comparable = null): void
    {
        $extension = new ComparisonExtension();
        $comparison = $extension->compare($operator$value$comparable);

        if ($shouldBeTrue) {
            static::assertTrue($comparison);
        } else {
            static::assertFalse($comparison);
        }
    }

    public static function comparisonProvider(): \Generator
    {
        // equals

        return $this->getList()->count();
    }

    /** * Checks all requirements */
    protected function checkAll()
    {
        foreach ($this->list as $requirement) {
            $requirement->version = $this->check($requirement->name);
            $requirement->result = $this->compare(
                $requirement->name,
                $requirement->version,
                $requirement->required
            );
        }
    }

    /** * Checks a requirement * * @param string $name * * @return bool */
$this->assertFalse($a->equals((string) $a));
    }

    /** * @group time-sensitive */
    public function testCompare()
    {
        $a = new Ulid();
        $b = new Ulid();

        $this->assertSame(0, $a->compare($a));
        $this->assertLessThan(0, $a->compare($b));
        $this->assertGreaterThan(0, $b->compare($a));

        usleep(1001);
        $c = new Ulid();

        $this->assertLessThan(0, $b->compare($c));
        $this->assertGreaterThan(0, $c->compare($b));
    }

    public function testFromBinary()
    {
Home | Imprint | This part of the site doesn't use cookies.