/** * Returns events by filter name, namespace or callback. * @return EventRegister[] */ public function filter() { Parameter::organize(func_get_args())->expects(1)->add($filterNames, 'string')->add($filterCallback, 'callable'); if (!$filterNames && !$filterCallback) { return []; } // Filters data. $filterNamespace = null; $filterName = null; // Match name/namespace filters. if ($filterNames) { EventRegister::splitNames($filterNames, $filterName, $filterNamespace); } // Collect registers. $registers = []; foreach ($this->registers as $register) { if ($filterNamespace !== null && $filterNamespace !== $register->namespace) { continue; } if ($filterName !== null && $filterName !== $register->name && $filterName !== '*') { continue; } if ($filterCallback !== null && $filterCallback !== $register->callback) { continue; } $registers[] = $register; } return $registers; }
/** * Test basic methods. * @covers Rentalhost\VanillaParameter\Parameter::__construct * @covers Rentalhost\VanillaParameter\Parameter::organize * @covers Rentalhost\VanillaParameter\ParameterOrganizer::__construct * @covers Rentalhost\VanillaParameter\ParameterOrganizer::add * @covers Rentalhost\VanillaParameter\ParameterOrganizer::expects * @covers Rentalhost\VanillaParameter\ParameterOrganizer::validate * @covers Rentalhost\VanillaParameter\ParameterOrganizer::defaultValue */ public function testBasic() { static::assertTrue(Parameter::organize([])->validate()); // Simple validation. static::assertTrue(Parameter::organize([1])->validate()); static::assertTrue(Parameter::organize([1, 2, 3])->validate()); static::assertTrue(Parameter::organize(['a'])->validate()); static::assertTrue(Parameter::organize(['a', 'b', 'c'])->validate()); static::assertTrue(Parameter::organize([true])->validate()); static::assertTrue(Parameter::organize([false])->validate()); static::assertTrue(Parameter::organize([1, 'b', false])->validate()); // Expects validation. static::assertTrue(Parameter::organize([1, 2, 3])->expects(2)->validate()); static::assertTrue(Parameter::organize(['a', 'b', 'c'])->expects(2)->validate()); static::assertTrue(Parameter::organize([1, 'b', false])->expects(2)->validate()); static::assertTrue(Parameter::organize([1, 0])->expects(2)->validate()); static::assertFalse(Parameter::organize([1])->expects(2)->validate()); static::assertFalse(Parameter::organize(['a'])->expects(2)->validate()); static::assertFalse(Parameter::organize([true])->expects(2)->validate()); static::assertFalse(Parameter::organize([false])->expects(2)->validate()); // No parameters validation. $organizer = Parameter::organize([])->add($parameter1)->add($parameter2)->add($parameter3); static::assertNull($parameter1); static::assertNull($parameter2); static::assertNull($parameter3); static::assertTrue($organizer->validate()); unset($parameter1, $parameter2, $parameter3); // Single parameter validation. $organizer = Parameter::organize([false])->add($parameter1, null, true); static::assertSame(false, $parameter1); static::assertTrue($organizer->validate()); unset($parameter1); // Required parameters validation. $organizer = Parameter::organize([1, 'a', false])->add($parameter1, null, true)->add($parameter2, null, true)->add($parameter3, null, true); static::assertSame(1, $parameter1); static::assertSame('a', $parameter2); static::assertSame(false, $parameter3); static::assertTrue($organizer->validate()); unset($parameter1, $parameter2, $parameter3); // Required parameters validation (fail). $organizer = Parameter::organize([1, 'a'])->add($parameter1, null, true)->add($parameter2, null, true)->add($parameter3, null, true); static::assertSame(1, $parameter1); static::assertSame('a', $parameter2); static::assertSame(null, $parameter3); static::assertFalse($organizer->validate()); unset($parameter1, $parameter2, $parameter3); // Default value set (even if required fails). $organizer = Parameter::organize([false, false])->add($parameter1, null, true)->add($parameter2, null, true)->defaultValue(true)->add($parameter3, null, true)->defaultValue(true); static::assertSame(false, $parameter1); static::assertSame(false, $parameter2); static::assertSame(true, $parameter3); static::assertFalse($organizer->validate()); unset($parameter1, $parameter2, $parameter3); // Organize to second parameter. $organizer = Parameter::organize(['abc'])->add($parameter1, 'int')->defaultValue(123)->add($parameter2)->defaultValue('def'); static::assertSame(123, $parameter1); static::assertSame('abc', $parameter2); static::assertTrue($organizer->validate()); unset($parameter1, $parameter2); // Organize, even if required fails. $organizer = Parameter::organize(['abc'])->add($parameter1, 'int', true)->defaultValue(123)->add($parameter2)->defaultValue('def'); static::assertSame(123, $parameter1); static::assertSame('abc', $parameter2); static::assertFalse($organizer->validate()); unset($parameter1, $parameter2); // Class tests. $instance1 = new stdClass(); $organizer = Parameter::organize([$instance1])->add($parameter1, stdClass::class, true); static::assertSame($instance1, $parameter1); static::assertTrue($organizer->validate()); unset($parameter1); // Class tests. $instance1 = new stdClass(); $organizer = Parameter::organize([$instance1])->add($parameter1, 'int')->add($parameter2, stdClass::class, true); static::assertNull($parameter1); static::assertSame($instance1, $parameter2); static::assertTrue($organizer->validate()); unset($parameter1, $parameter2); // Instance of. $instance1 = new Test\B(); $organizer = Parameter::organize([$instance1])->add($parameter1, Test\A::class)->add($parameter2, Test\A::class, true); static::assertSame($instance1, $parameter1); static::assertNull($parameter2); static::assertFalse($organizer->validate()); unset($parameter1, $parameter2); // Instance of (should fail). $organizer = Parameter::organize([new Test\B()])->add($parameter1, Test\C::class); static::assertNull($parameter1); static::assertTrue($organizer->validate()); unset($parameter1); }