Skip to content

Instantly share code, notes, and snippets.

@rogerlos
Created July 8, 2018 04:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rogerlos/fbfc04a2abb104fd3a5556270532fedd to your computer and use it in GitHub Desktop.
Save rogerlos/fbfc04a2abb104fd3a5556270532fedd to your computer and use it in GitHub Desktop.
Untested factory idea
<?php
class Puc_Factory {
/**
* Current version entered by default, but can be changed by ::version()
*/
protected static $major = 4;
protected static $minor = 3;
/**
* Private latest version
*/
private static $current_major = 4;
private static $current_minor = 3;
/**
* Array of supported versions
*/
private static $supported = [
[
'major' => 4,
'minor' => [
'low' => 0,
'high' => 3
]
]
];
/**
* Changes version number being used. Checks if single parameter contains full version number
*
* Puc_Factory::version( 4.3 ) is same as Puc_Factory::version( 4,3 )
*
* @param int|string $maj
* @param int|string|null $minor
*/
public static function version( $maj, $minor = null ) {
$single = explode( '.', (string) $maj );
$mj = intval( $single[0] );
$mn = ! empty( $single[1] ) ? intval( $single[1] ) :
( $minor !== null ? intval( $minor ) : self::$current_minor );
$check = self::supports( $mj, $mn );
self::$major = $check ? $mj : self::$current_major;
self::$minor = $check ? $mn : self::$current_minor;
}
/**
* Checks if version asked for is supported, using self::$supported array
*
* @param int $major
* @param int $minor
*
* @return bool
*/
protected static function supports( $major, $minor ) {
$maj = $min = false;
foreach( self::$supported as $sup ) {
if ( $sup['major'] == $major ) {
$maj = $major;
$min = $minor >= $sup['minor'][ 'low' ] && $minor <= $sup['minor'][ 'high' ] ? $minor : false;
break;
}
}
return $maj && $min;
}
/**
* Magical! Note, this will default to current version number, you'd have to call "version" first to change
* version being called.
*
* @param $name
* @param $args
*
* @return mixed
*/
public static function __callStatic( $name, $args ) {
$call = 'Puc_v' . self::$major . 'p' . self::$minor . '_Factory';
if ( class_exists( $call ) && method_exists( $call, $name ) ) {
return $call::$name( $args );
}
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment