diff --git a/src/Configuration.php b/src/Configuration.php index 9eba89f..d43d5ae 100644 --- a/src/Configuration.php +++ b/src/Configuration.php @@ -2,10 +2,10 @@ namespace PHRETS; +use PHRETS\Enums\RETSVersion; use PHRETS\Exceptions\InvalidConfiguration; use PHRETS\Strategies\SimpleStrategy; use PHRETS\Strategies\Strategy; -use PHRETS\Versions\RETSVersion; class Configuration { @@ -17,22 +17,21 @@ class Configuration protected ?string $login_url = null; protected string $user_agent = 'PHRETS/2.6.4'; protected ?string $user_agent_password = null; - protected RETSVersion $rets_version; + protected readonly RETSVersion $rets_version; protected readonly Strategy $strategy; protected string $http_authentication = 'digest'; /** @var array */ protected array $options = []; - public function __construct(?Strategy $strategy = null) - { - if ($strategy === null) { - $strategy = new SimpleStrategy(); - } + public function __construct( + ?Strategy $strategy = null, + ?RETSVersion $version = null + ) { + $this->rets_version = $version ?? RETSVersion::VERSION_1_5; + $this->strategy = $strategy ?? new SimpleStrategy(); - $this->rets_version = (new RETSVersion())->setVersion('1.5'); - $this->strategy = $strategy; - $strategy->initialize($this); + $this->strategy->initialize($this); } public function getLoginUrl(): ?string @@ -66,18 +65,6 @@ public function getRetsVersion(): RETSVersion return $this->rets_version; } - /** - * @param string $rets_version - * - * @return $this - */ - public function setRetsVersion(string $rets_version) - { - $this->rets_version = (new RETSVersion())->setVersion($rets_version); - - return $this; - } - public function getUserAgent(): string { return $this->user_agent; @@ -140,11 +127,22 @@ public static function load(array $configuration = []): self 'login_url' => 'LoginUrl', 'user_agent' => 'UserAgent', 'user_agent_password' => 'UserAgentPassword', - 'rets_version' => 'RetsVersion', 'http_authentication' => 'HttpAuthenticationMethod', ]; - $me = new self(); + $version = null; + $retsVersion = $configuration['rets_version'] ?? null; + if ($retsVersion !== null && $retsVersion !== '') { + if (str_starts_with($retsVersion, 'RETS/')) { + $retsVersion = substr($retsVersion, strlen('RETS/')); + } + $version = RETSVersion::tryFrom($retsVersion); + if ($version === null) { + throw new InvalidConfiguration('Invalid RETS version: ' . $retsVersion); + } + } + + $me = new self(version: $version); foreach ($variables as $k => $m) { if (array_key_exists($k, $configuration)) { @@ -169,7 +167,7 @@ public function valid(): bool /** */ - public function getStrategy(): \PHRETS\Strategies\Strategy + public function getStrategy(): Strategy { return $this->strategy; } @@ -181,17 +179,12 @@ public function userAgentDigestHash(Session $session): string $ua_a1 = md5($this->getUserAgent() . ':' . $this->getUserAgentPassword()); return md5( - trim((string) $ua_a1) . '::' . trim((string) $session->getRetsSessionId()) . - ':' . trim((string) $this->getRetsVersion()->asHeader()) + trim($ua_a1) . '::' . trim((string) $session->getRetsSessionId()) . + ':' . trim($this->getRetsVersion()->asHeader()) ); } - /** - * @param $auth_method - * - * @return $this - */ - public function setHttpAuthenticationMethod(string $auth_method) + public function setHttpAuthenticationMethod(string $auth_method): self { if (!in_array($auth_method, [self::AUTH_BASIC, self::AUTH_DIGEST])) { throw new \InvalidArgumentException("Given authentication method is invalid. Must be 'basic' or 'digest'"); @@ -201,8 +194,6 @@ public function setHttpAuthenticationMethod(string $auth_method) return $this; } - /** - */ public function getHttpAuthenticationMethod(): string { return $this->http_authentication; diff --git a/src/Enums/RETSVersion.php b/src/Enums/RETSVersion.php new file mode 100644 index 0000000..8906228 --- /dev/null +++ b/src/Enums/RETSVersion.php @@ -0,0 +1,23 @@ +value; + } + + public function isAtLeast(self $version): bool + { + return version_compare($this->value, $version->value) >= 0; + } +} diff --git a/src/Exceptions/InvalidRETSVersion.php b/src/Exceptions/InvalidRETSVersion.php deleted file mode 100644 index 19f7895..0000000 --- a/src/Exceptions/InvalidRETSVersion.php +++ /dev/null @@ -1,7 +0,0 @@ -getConfiguration(); - if ($configuration->getRetsVersion()->is1_5()) { + if ($configuration->getRetsVersion() === RETSVersion::VERSION_1_5) { if (property_exists($base->System, 'SystemID') && $base->System->SystemID !== null) { $metadata->setSystemID((string) $base->System->SystemID); } diff --git a/src/Session.php b/src/Session.php index 937164b..ca7c3c8 100644 --- a/src/Session.php +++ b/src/Session.php @@ -97,8 +97,12 @@ public function Login(): Bulletin * @param int $location * */ - public function GetPreferredObject(string $resource, string $type, string $content_id, int $location = 0): ?BaseObject - { + public function GetPreferredObject( + string $resource, + string $type, + string $content_id, + int $location = 0 + ): ?BaseObject { $collection = $this->GetObject($resource, $type, $content_id, '0', $location); return $collection[0] ?? null; @@ -115,8 +119,13 @@ public function GetPreferredObject(string $resource, string $type, string $conte * * @throws \PHRETS\Exceptions\CapabilityUnavailable */ - public function GetObject(string $resource, string $type, string $content_ids, string|int $object_ids = '*', $location = 0): array - { + public function GetObject( + string $resource, + string $type, + string $content_ids, + string|int $object_ids = '*', + int $location = 0 + ): array { $request_id = GetObject::ids($content_ids, $object_ids); $response = $this->request( @@ -476,7 +485,10 @@ protected function request(string $capability, array $options = [], bool $is_ret 'headers' => $options['headers'], 'body' => $options['body'], ]); - } elseif ($this->configuration->readOption('use_post_method') || array_key_exists('form_params', $options)) { + } elseif ( + $this->configuration->readOption('use_post_method') || + array_key_exists('form_params', $options) + ) { if (array_key_exists('form_params', $options)) { $this->debug('Using POST method per form_params option'); $query = $options['form_params']; @@ -669,11 +681,6 @@ public function getLastResponse(): string return (string) $this->last_response?->getBody(); } - public function getClient(): ClientInterface - { - return $this->client; - } - public function getRetsSessionId(): ?string { return $this->rets_session_id; diff --git a/src/Strategies/SimpleStrategy.php b/src/Strategies/SimpleStrategy.php index 5b665f1..32f3c78 100644 --- a/src/Strategies/SimpleStrategy.php +++ b/src/Strategies/SimpleStrategy.php @@ -3,6 +3,7 @@ namespace PHRETS\Strategies; use PHRETS\Configuration; +use PHRETS\Enums\RETSVersion; use PHRETS\Exceptions\ParserNotFound; use PHRETS\Parsers\ParserType; @@ -57,7 +58,7 @@ public function provide(ParserType $parser): mixed */ public function initialize(Configuration $configuration): void { - if ($configuration->getRetsVersion()->isAtLeast1_8()) { + if ($configuration->getRetsVersion()->isAtLeast(RETSVersion::VERSION_1_8)) { $this->classes[ParserType::LOGIN->value] = \PHRETS\Parsers\Login\OneEight::class; $this->classes[ParserType::OBJECT_POST->value] = \PHRETS\Parsers\PostObject\OneEight::class; $this->classes[ParserType::UPDATE->value] = \PHRETS\Parsers\Update\OneEight::class; diff --git a/src/Versions/RETSVersion.php b/src/Versions/RETSVersion.php deleted file mode 100644 index 7652bf4..0000000 --- a/src/Versions/RETSVersion.php +++ /dev/null @@ -1,110 +0,0 @@ -number = str_replace('RETS/', '', $version); - if (!in_array($this->number, self::VALID_VERSIONS)) { - throw new InvalidRETSVersion("RETS version '{$version}' given is not understood"); - } - - return $this; - } - - public function getVersion(): string - { - return $this->number; - } - - public function asHeader(): string - { - return 'RETS/' . $this->number; - } - - public function is1_5(): bool - { - return $this->number == self::VERSION_1_5; - } - - public function is1_7(): bool - { - return $this->number == self::VERSION_1_7; - } - - public function is1_7_2(): bool - { - return $this->number == self::VERSION_1_7_2; - } - - public function is1_8(): bool - { - return $this->number == self::VERSION_1_8; - } - - /** - * @param string $version - */ - public function isAtLeast(string $version): bool - { - return version_compare($this->number, $version) >= 0; - } - - public function isAtLeast1_5(): bool - { - return $this->isAtLeast(self::VERSION_1_5); - } - - public function isAtLeast1_7(): bool - { - return $this->isAtLeast(self::VERSION_1_7); - } - - public function isAtLeast1_7_2(): bool - { - return $this->isAtLeast(self::VERSION_1_7_2); - } - - public function isAtLeast1_8(): bool - { - return $this->isAtLeast(self::VERSION_1_8); - } - - /** - * @return list - */ - public function getValidVersions(): array - { - return self::VALID_VERSIONS; - } - - public function __toString(): string - { - return $this->asHeader(); - } -} diff --git a/tests/ConfigurationTest.php b/tests/ConfigurationTest.php index 65a9b6e..75b3e5d 100644 --- a/tests/ConfigurationTest.php +++ b/tests/ConfigurationTest.php @@ -5,6 +5,7 @@ use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\TestCase; use PHRETS\Configuration; +use PHRETS\Enums\RETSVersion; use PHRETS\Session; use PHRETS\Strategies\SimpleStrategy; @@ -48,15 +49,14 @@ public function itComplainsAboutBadConfig(): void public function itLoadsDefaultRetsVersion(): void { $config = new Configuration(); - $this->assertTrue($config->getRetsVersion()->is1_5()); + $this->assertSame(RETSVersion::VERSION_1_5, $config->getRetsVersion()); } #[Test] public function itHandlesVersionsCorrectly(): void { - $config = new Configuration(); - $config->setRetsVersion('1.7.2'); - $this->assertTrue($config->getRetsVersion()->is1_7_2()); + $config = new Configuration(version: RETSVersion::VERSION_1_7_2); + $this->assertSame(RETSVersion::VERSION_1_7_2, $config->getRetsVersion()); } #[Test] @@ -103,11 +103,10 @@ public function itAllowsOverridingTheStrategy(): void #[Test] public function itGeneratesUserAgentAuthHashesCorrectly(): void { - $c = new Configuration(); + $c = new Configuration(version: RETSVersion::VERSION_1_7_2); $c->setLoginUrl('http://www.reso.org/login') ->setUserAgent('PHRETS/2.0') - ->setUserAgentPassword('12345') - ->setRetsVersion('1.7.2'); + ->setUserAgentPassword('12345'); $s = new Session($c); $this->assertSame('123c96e02e514da469db6bc61ab998dc', $c->userAgentDigestHash($s)); diff --git a/tests/Integration/BaseIntegration.php b/tests/Integration/BaseIntegration.php index 207c110..39e22ed 100644 --- a/tests/Integration/BaseIntegration.php +++ b/tests/Integration/BaseIntegration.php @@ -6,6 +6,7 @@ use GuzzleHttp\HandlerStack; use PHPUnit\Framework\TestCase; use PHRETS\Configuration; +use PHRETS\Enums\RETSVersion; use PHRETS\Session; use Psr\Http\Message\RequestInterface; @@ -31,11 +32,10 @@ class BaseIntegration extends TestCase public function setUp(): void { $this->path = __DIR__ . '/Fixtures/Http'; - $config = new Configuration(); + $config = new Configuration(version: RETSVersion::VERSION_1_7_2); $config->setLoginUrl('http://retsgw.flexmls.com/rets2_1/Login') ->setUsername(getenv('PHRETS_TESTING_USERNAME')) - ->setPassword(getenv('PHRETS_TESTING_PASSWORD')) - ->setRetsVersion('1.7.2'); + ->setPassword(getenv('PHRETS_TESTING_PASSWORD')); $this->session = $this->createSession($config); $this->session->Login(); diff --git a/tests/Integration/GetMetadataIntegrationTest.php b/tests/Integration/GetMetadataIntegrationTest.php index d80cef9..d0b0195 100644 --- a/tests/Integration/GetMetadataIntegrationTest.php +++ b/tests/Integration/GetMetadataIntegrationTest.php @@ -3,6 +3,8 @@ use PHPUnit\Framework\Attributes\Test; use PHRETS\Arr; +use PHRETS\Configuration; +use PHRETS\Enums\RETSVersion; class GetMetadataIntegrationTest extends BaseIntegration { @@ -20,11 +22,10 @@ public function itGetsSystemData(): void #[Test] public function itGetsSystemDataFor15(): void { - $config = new \PHRETS\Configuration(); + $config = new Configuration(version: RETSVersion::VERSION_1_5); $config->setLoginUrl('http://retsgw.flexmls.com/rets2_1/Login') ->setUsername(getenv('PHRETS_TESTING_USERNAME')) - ->setPassword(getenv('PHRETS_TESTING_PASSWORD')) - ->setRetsVersion('1.5'); + ->setPassword(getenv('PHRETS_TESTING_PASSWORD')); $session = $this->createSession($config); $session->Login(); @@ -228,11 +229,10 @@ public function itGetsRelatedLookupValues(): void #[Test] public function itRecoversFromBadLookuptypeTag(): void { - $config = new \PHRETS\Configuration(); + $config = new Configuration(version: RETSVersion::VERSION_1_5); $config->setLoginUrl('http://retsgw.flexmls.com/lookup/rets2_1/Login') ->setUsername(getenv('PHRETS_TESTING_USERNAME')) - ->setPassword(getenv('PHRETS_TESTING_PASSWORD')) - ->setRetsVersion('1.5'); + ->setPassword(getenv('PHRETS_TESTING_PASSWORD')); $session = $this->createSession($config); $session->Login(); @@ -244,11 +244,10 @@ public function itRecoversFromBadLookuptypeTag(): void #[Test] public function itHandlesIncompleteObjectMetadataCorrectly(): void { - $config = new \PHRETS\Configuration(); + $config = new \PHRETS\Configuration(version: RETSVersion::VERSION_1_5); $config->setLoginUrl('http://retsgw.flexmls.com/rets2_1/Login') ->setUsername(getenv('PHRETS_TESTING_USERNAME')) - ->setPassword(getenv('PHRETS_TESTING_PASSWORD')) - ->setRetsVersion('1.5'); + ->setPassword(getenv('PHRETS_TESTING_PASSWORD')); $session = $this->createSession($config); $session->Login(); diff --git a/tests/Integration/SessionIntegrationTest.php b/tests/Integration/SessionIntegrationTest.php index c48e5de..72e3a55 100644 --- a/tests/Integration/SessionIntegrationTest.php +++ b/tests/Integration/SessionIntegrationTest.php @@ -5,6 +5,7 @@ use GuzzleHttp\Middleware; use PHPUnit\Framework\Attributes\Test; use PHRETS\Configuration; +use PHRETS\Enums\RETSVersion; use PHRETS\Session; class SessionIntegrationTest extends BaseIntegration @@ -53,13 +54,12 @@ public function itDisconnects(): void #[Test] public function itRequestsTheServersActionTransaction(): void { - $config = new \PHRETS\Configuration(); + $config = new \PHRETS\Configuration(version: RETSVersion::VERSION_1_7_2); // this endpoint doesn't actually exist, but the response is mocked, so... $config->setLoginUrl('http://retsgw.flexmls.com/action/rets2_1/Login') ->setUsername(getenv('PHRETS_TESTING_USERNAME')) - ->setPassword(getenv('PHRETS_TESTING_PASSWORD')) - ->setRetsVersion('1.7.2'); + ->setPassword(getenv('PHRETS_TESTING_PASSWORD')); $session = $this->createSession($config); $bulletin = $session->Login(); @@ -70,13 +70,12 @@ public function itRequestsTheServersActionTransaction(): void #[Test] public function itUsesHttpPostMethodWhenDesired(): void { - $config = new \PHRETS\Configuration(); + $config = new \PHRETS\Configuration(version: RETSVersion::VERSION_1_7_2); // this endpoint doesn't actually exist, but the response is mocked, so... $config->setLoginUrl('http://retsgw.flexmls.com/rets2_1/Login') ->setUsername(getenv('PHRETS_TESTING_USERNAME')) ->setPassword(getenv('PHRETS_TESTING_PASSWORD')) - ->setRetsVersion('1.7.2') ->setOption('use_post_method', true); $session = $this->createSession($config); @@ -103,15 +102,13 @@ public function itTracksAGivenSessionId(): void #[Test] public function itDetectsWhenToUseUserAgentAuthentication(): void { - $config = new Configuration(); + $config = new Configuration(version: RETSVersion::VERSION_1_7_2); $config->setLoginUrl('http://retsgw.flexmls.com/rets2_1/Login') ->setUsername(getenv('PHRETS_TESTING_USERNAME')) ->setPassword(getenv('PHRETS_TESTING_PASSWORD')) ->setUserAgent('PHRETS/2.0') - ->setUserAgentPassword('bogus_password') - ->setRetsVersion('1.7.2'); - + ->setUserAgentPassword('bogus_password'); $handler = $this->createHandler(); $session = new Session($config, new Client(['handler' => $handler])); @@ -136,13 +133,12 @@ public function itDetectsWhenToUseUserAgentAuthentication(): void public function itDoesntAllowRequestsToUnsupportedCapabilities(): void { $this->expectException(\PHRETS\Exceptions\CapabilityUnavailable::class); - $config = new \PHRETS\Configuration(); + $config = new Configuration(version: RETSVersion::VERSION_1_7_2); // fake, mocked endpoint $config->setLoginUrl('http://retsgw.flexmls.com/limited/rets2_1/Login') ->setUsername(getenv('PHRETS_TESTING_USERNAME')) - ->setPassword(getenv('PHRETS_TESTING_PASSWORD')) - ->setRetsVersion('1.7.2'); + ->setPassword(getenv('PHRETS_TESTING_PASSWORD')); $session = $this->createSession($config); $session->Login(); diff --git a/tests/Strategies/SimpleStrategyTest.php b/tests/Strategies/SimpleStrategyTest.php index a875537..59183b1 100644 --- a/tests/Strategies/SimpleStrategyTest.php +++ b/tests/Strategies/SimpleStrategyTest.php @@ -4,6 +4,7 @@ use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\TestCase; use PHRETS\Configuration; +use PHRETS\Enums\RETSVersion; use PHRETS\Parsers\ParserType; use PHRETS\Strategies\SimpleStrategy; @@ -16,18 +17,17 @@ public function itProvidesDefaults(): void $strategy = new SimpleStrategy(); $strategy->initialize($config); - $this->assertInstanceOf('\PHRETS\Parsers\Login\OneFive', $strategy->provide(ParserType::LOGIN)); + $this->assertInstanceOf(\PHRETS\Parsers\Login\OneFive::class, $strategy->provide(ParserType::LOGIN)); } #[Test] public function itProvidesA18LoginParser(): void { - $config = new Configuration(); - $config->setRetsVersion('1.8'); + $config = new Configuration(version: RETSVersion::VERSION_1_8); $strategy = new SimpleStrategy(); $strategy->initialize($config); - $this->assertInstanceOf('\PHRETS\Parsers\Login\OneEight', $strategy->provide(ParserType::LOGIN)); + $this->assertInstanceOf(\PHRETS\Parsers\Login\OneEight::class, $strategy->provide(ParserType::LOGIN)); } #[Test] diff --git a/tests/Versions/RETSVersionTest.php b/tests/Versions/RETSVersionTest.php index 678cb60..62f7a5e 100644 --- a/tests/Versions/RETSVersionTest.php +++ b/tests/Versions/RETSVersionTest.php @@ -3,105 +3,55 @@ use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\TestCase; -use PHRETS\Versions\RETSVersion; +use PHRETS\Enums\RETSVersion; class RETSVersionTest extends TestCase { #[Test] public function itLoads(): void { - $this->assertSame('1.7.2', (new RETSVersion())->setVersion('1.7.2')->getVersion()); - } - - #[Test] - public function itCleans(): void - { - $this->assertSame('1.7.2', (new RETSVersion())->setVersion('RETS/1.7.2')->getVersion()); + $this->assertSame('1.7.2', RETSVersion::VERSION_1_7_2->value); } #[Test] public function itMakesTheHeader(): void { - $this->assertSame('RETS/1.7.2', (new RETSVersion())->setVersion('1.7.2')->asHeader()); + $this->assertSame('RETS/1.7.2', RETSVersion::VERSION_1_7_2->asHeader()); } #[Test] public function itIs15(): void { - $v = new RETSVersion(); - $v->setVersion('RETS/1.5'); - - $this->assertTrue($v->is1_5()); - $this->assertTrue($v->isAtLeast1_5()); + $this->assertTrue(RETSVersion::VERSION_1_5->isAtLeast(RETSVersion::VERSION_1_5)); } #[Test] public function itIs17(): void { - $v = new RETSVersion(); - $v->setVersion('RETS/1.7'); + $v = RETSVersion::VERSION_1_7; - $this->assertTrue($v->is1_7()); - $this->assertFalse($v->is1_5()); - $this->assertFalse($v->is1_7_2()); - $this->assertTrue($v->isAtLeast1_7()); - $this->assertFalse($v->isAtLeast1_7_2()); + $this->assertTrue($v->isAtLeast(RETSVersion::VERSION_1_7)); + $this->assertFalse($v->isAtLeast(RETSVersion::VERSION_1_7_2)); } #[Test] public function itIs172(): void { - $v = new RETSVersion(); - $v->setVersion('RETS/1.7.2'); + $v = RETSVersion::VERSION_1_7_2; - $this->assertFalse($v->is1_7()); - $this->assertFalse($v->is1_5()); - $this->assertTrue($v->is1_7_2()); - $this->assertTrue($v->isAtLeast1_7()); - $this->assertTrue($v->isAtLeast1_7_2()); - $this->assertFalse($v->isAtLeast1_8()); + $this->assertTrue($v->isAtLeast(RETSVersion::VERSION_1_7)); + $this->assertTrue($v->isAtLeast(RETSVersion::VERSION_1_7_2)); + $this->assertFalse($v->isAtLeast(RETSVersion::VERSION_1_8)); } #[Test] public function itIs18(): void { - $v = new RETSVersion(); - $v->setVersion('RETS/1.8'); - - $this->assertTrue($v->is1_8()); - $this->assertFalse($v->is1_7()); - $this->assertFalse($v->is1_5()); - $this->assertFalse($v->is1_7_2()); - $this->assertTrue($v->isAtLeast1_7()); - $this->assertTrue($v->isAtLeast1_7_2()); - $this->assertTrue($v->isAtLeast1_8()); - } - - #[Test] - public function itCompares(): void - { - $v = new RETSVersion(); - $v->setVersion('RETS/1.8'); - - $this->assertTrue($v->isAtLeast('1.5')); - $this->assertTrue($v->isAtLeast('1.7')); - $this->assertTrue($v->isAtLeast('1.7.2')); - } - - #[Test] - public function itFailsBadVersions(): void - { - $this->expectException(\PHRETS\Exceptions\InvalidRETSVersion::class); - $v = new RETSVersion(); - $v->setVersion('2.0'); - } - - #[Test] - public function itConvertsToString(): void - { - $v = new RETSVersion(); - $v->setVersion('1.7.2'); + $v = RETSVersion::VERSION_1_8; - $this->assertSame('RETS/1.7.2', (string) $v); + $this->assertTrue($v->isAtLeast(RETSVersion::VERSION_1_5)); + $this->assertTrue($v->isAtLeast(RETSVersion::VERSION_1_7)); + $this->assertTrue($v->isAtLeast(RETSVersion::VERSION_1_7_2)); + $this->assertTrue($v->isAtLeast(RETSVersion::VERSION_1_8)); } }