PluginProbe
Flexible Subscriptions / trunk
Flexible Subscriptions vtrunk
1.8.4 1.8.3 1.8.2 1.8.1 1.8.0 1.7.19 1.7.18 1.7.17 1.7.16 1.7.15 1.7.14 1.7.13 1.7.12 1.7.11 trunk 1.0.0 1.0.1 1.1.0 1.1.1 1.1.2 1.1.3 1.2.0 1.2.1 1.2.2 1.3.0 All 61 releases
flexible-subscriptions / src / Compatibility / Caster.php

Caster.php in Flexible Subscriptions trunk, at src/Compatibility/Caster.php

41 lines 1.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 declare(strict_types=1);
4
5 namespace WPDesk\FlexibleSubscriptions\Compatibility;
6
7 use WPDesk\FlexibleSubscriptions\Subscription\Subscription;
8
9 class Caster {
10
11 /**
12 * @template T of object
13 * @param class-string<T> $class_name
14 * @param \WC_Data $source
15 *
16 * @return T
17 */
18 public function cast_to( string $class_name, object $source ): object {
19 if ( ! class_exists( $class_name ) ) {
20 throw new \InvalidArgumentException( sprintf( 'Cannot cast to %s. Class does not exist.', $class_name ) );
21 }
22
23 if ( $source instanceof Subscription && is_subclass_of( $class_name, \WC_Order::class, true ) ) {
24 return $this->cast_subscription( $class_name, $source );
25 }
26
27 throw new \InvalidArgumentException( sprintf( 'Cannot cast to %s.', $class_name ) );
28 }
29
30 /**
31 * @template T of object
32 * @param class-string<T> $class_name
33 * @param \WC_Data $source
34 *
35 * @return T
36 */
37 private function cast_subscription( string $class_name, object $source ): object {
38 return new $class_name( $source->get_id() );
39 }
40 }
41