-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* De-couple [iet:3634834], base-classes [iet:3634867] * PSR-2 [iet:3633574], unit-tests [iet:3699309]
- Loading branch information
Showing
9 changed files
with
179 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php namespace IET_OU\Open_Media_Player; | ||
|
||
/** | ||
* OffsetIterator utility class. | ||
* | ||
* @copyright 2015 The Open University. | ||
* @author N.D.Freear, 23 May 2015. | ||
* @link https://gist.github.com/nfreear/72a3a62b8ac810ea4c49 | ||
* @link http://php.net/manual/en/class.iterator.php | ||
*/ | ||
|
||
class OffsetIterator implements \Iterator | ||
{ | ||
|
||
private $offset; | ||
private $position; | ||
private $array = array(); | ||
|
||
public function __construct($array, $offset = 0) | ||
{ | ||
$this->position = $this->offset = $offset; | ||
$this->array = $array; | ||
} | ||
|
||
public function rewind() | ||
{ | ||
$this->position = $this->offset; | ||
} | ||
|
||
public function current() | ||
{ | ||
#var_dump(__METHOD__, $this->position); | ||
return $this->array[ $this->position ]; | ||
} | ||
|
||
public function key() | ||
{ | ||
return $this->position; | ||
} | ||
|
||
public function next() | ||
{ | ||
++$this->position; | ||
} | ||
|
||
public function valid() | ||
{ | ||
return isset($this->array[ $this->position ]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php namespace IET_OU\Open_Media_Player; | ||
|
||
/** | ||
* SubClasses class. | ||
* | ||
* @copyright 2015 The Open University. | ||
* @author N.D.Freear, 23 May 2015. | ||
* @link https://gist.github.com/nfreear/72a3a62b8ac810ea4c49 | ||
*/ | ||
|
||
use \IET_OU\Open_Media_Player\OffsetIterator; | ||
|
||
class SubClasses extends OffsetIterator | ||
{ | ||
/** | ||
* How many PHP "core" classes should we skip? (performance) | ||
* | ||
* $ php -r 'echo count(get_declared_classes());' | ||
* Result: 139, Mac/PHP 5.4.38; 125, Ar**s/PHP 5.3.3; 120, Pan**s/RHE 6/PHP 5.5.26; | ||
*/ | ||
const PHP_CORE_OFFSET = 120; | ||
|
||
const ON_ADD_FN = 'onAddClass'; | ||
|
||
public function __construct($offset = self::PHP_CORE_OFFSET) | ||
{ | ||
parent::__construct(get_declared_classes(), $offset); | ||
} | ||
|
||
/** | ||
* @param string $base_class A parent class or interface. | ||
* @param string $callback Optional static function to call on the class, providing the key in the results. | ||
* @return array Array of result classes, optionally keyed. | ||
*/ | ||
public function match($base_class, $callback = null, $test_instantiable = true) | ||
{ | ||
$results = array(); | ||
foreach ($this as $class) { | ||
if (is_subclass_of($class, $base_class)) { | ||
//or $base_class == $class) { | ||
$reflect = new \ReflectionClass($class); | ||
if ($reflect->isInstantiable()) { | ||
if ($callback) { | ||
$obj = new $class; | ||
$obj->{ $callback }($results); | ||
//Was: $results[ $class::{ $callback }() ] = $class; | ||
} else { | ||
$results[] = $class; | ||
} | ||
} | ||
} | ||
} | ||
return $results; | ||
} | ||
|
||
|
||
public function get_oembed_providers() | ||
{ | ||
return $this->match('IET_OU\Open_Media_Player\Oembed_Provider', self::ON_ADD_FN); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
<?php namespace IET_OU\Open_Media_Player; | ||
|
||
/** | ||
* OU Media Player/ OU Podcast oEmbed service provider. | ||
* | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php namespace IET_OU\Open_Media_Player\Test; | ||
|
||
use \IET_OU\Open_Media_Player\SubClasses; | ||
use \IET_OU\Open_Media_Player\Oembed_Provider; | ||
|
||
class Mock_Ex_Provider extends Oembed_Provider { | ||
|
||
protected static $hosts = array( 'example.org', 'example.com' ); | ||
|
||
public function call($url, $regex_matches) { | ||
} | ||
} | ||
|
||
class SubClasses_Test extends \PHPUnit_Framework_TestCase | ||
{ | ||
// ... | ||
|
||
public function setup() | ||
{ | ||
\IET_OU\Open_Media_Player\Base::$throw_no_framework = false; | ||
} | ||
|
||
public function testOembedProviders() | ||
{ | ||
// Arrange | ||
$sub = new SubClasses(); | ||
|
||
// Act | ||
$providers = $sub->get_oembed_providers(); | ||
|
||
var_dump($providers); | ||
|
||
// Assert | ||
$this->assertEquals(2, count($providers)); | ||
} | ||
|
||
} | ||
|