diff --git a/lib/Category/Collection.php b/lib/Category/Collection.php index 0b0564c..a64a63b 100644 --- a/lib/Category/Collection.php +++ b/lib/Category/Collection.php @@ -7,13 +7,6 @@ declare(strict_types=1); namespace MensBeam\Lax\Category; class Collection extends \MensBeam\Lax\Collection { - protected static $ranks = [ - 'webmaster' => 10, - 'editor' => 20, - 'contributor' => 30, - 'author' => 40, - ]; - /** Returns the collection formatted as an array of strings * * The $humanFriendly parameter controls whether or not an effort is made to return human-friendly category names. Only Atom categories have this distinction diff --git a/lib/Collection.php b/lib/Collection.php index aa05c24..b4cb8b1 100644 --- a/lib/Collection.php +++ b/lib/Collection.php @@ -52,7 +52,7 @@ abstract class Collection implements \IteratorAggregate, \ArrayAccess, \Countabl * * The returned collection is the original instance, modified */ - public function merge(Collection ...$coll): self { + public function merge(self ...$coll): self { foreach ($coll as $c) { foreach ($c as $p) { $this[] = $p; diff --git a/lib/Enclosure/Enclosure.php b/lib/Enclosure/Enclosure.php index d3e8628..a12abae 100644 --- a/lib/Enclosure/Enclosure.php +++ b/lib/Enclosure/Enclosure.php @@ -6,14 +6,99 @@ declare(strict_types=1); namespace MensBeam\Lax\Enclosure; -class Enclosure { - public $url; - public $type; - public $title; - public $height; - public $width; - public $duration; - public $bitrate; - public $size; +/** + * @property \MensBeam\Lax\Url $url + * @property string $type + * @property string $title + * @property int $height + * @property int $width + * @property int $duration + * @property int $bitrate + * @property int $size + * @property bool $preferred + */ +class Enclosure implements \IteratorAggregate, \ArrayAccess, \Countable { public $preferred; + protected $data = []; + private $url; + private $type; + private $title; + private $height; + private $width; + private $duration; + private $bitrate; + private $size; + + public function __construct(Enclosure ...$enc) { + $this->data = $enc ?? []; + } + + public function getIterator(): \Traversable { + return new \ArrayIterator($this->data); + } + + public function count(): int { + return count($this->data); + } + + public function offsetExists($offset): bool { + return isset($this->data[$offset]); + } + + public function offsetGet($offset) { + return $this->data[$offset]; + } + + public function offsetSet($offset, $value): void { + if (is_null($offset)) { + $this->data[] = $value; + } else { + $this->data[$offset] = $value; + } + } + + public function offsetUnset($offset): void { + unset($this->data[$offset]); + } + + public function __set(string $name, $value): void { + if ($this->data) { + $this->default()->__set($name, $value); + } else { + $this->$name = $value; + } + } + + public function __get(string $name) { + if ($this->data) { + return $this->default()->__get($name); + } else { + return $this->$name; + } + } + + public function __isset(string $name): bool { + if ($this->data) { + return $this->default()->__isset($name); + } else { + return isset($this->$name); + } + } + + public function __unset(string $name): void { + if ($this->data) { + $this->default()->__unset($name); + } else { + unset($this->$name); + } + } + + protected function default(): self { + foreach ($this->data as $m) { + if ($m->preferred) { + return $m; + } + } + return $this->data[array_keys($this->data)[0]]; + } } diff --git a/tests/cases/Util/EnclosureTest.php b/tests/cases/Util/EnclosureTest.php new file mode 100644 index 0000000..a12d9c3 --- /dev/null +++ b/tests/cases/Util/EnclosureTest.php @@ -0,0 +1,96 @@ +preferred = true; + $enc->url = $url; + $this->assertTrue($enc->preferred); + $this->assertSame($url, $enc->url); + } + + public function testCreateAnEnclosureSet(): void { + // create an empty enclosure + $enc = new Enclosure; + $this->assertSame(0, count($enc)); + $this->assertFalse(isset($enc->title)); + // create two more enclosures; the second is preferred + $enc1 = new Enclosure; + $enc1->title = "Ook"; + $enc2 = new Enclosure; + $enc2->title = "Eek"; + $enc2->preferred = true; + // add the first sub-enclosure + $enc[] = $enc1; + $this->assertTrue(isset($enc->title)); + $this->assertSame(1, count($enc)); + $this->assertSame("Ook", $enc->title); + // add the second sub-enclosure + $enc[] = $enc2; + $this->assertSame(2, count($enc)); + $this->assertSame("Eek", $enc->title); + // make the second enclosure non-preferred + $enc2->preferred = false; + $this->assertSame("Ook", $enc->title); + } + + public function testIterateOverAnEnclosureSet(): void { + // create four enclosures + $enc1 = new Enclosure; + $enc2 = new Enclosure; + $enc3 = new Enclosure; + $enc4 = new Enclosure; + // create a set with two of the enclosures + $enc = new Enclosure($enc1, $enc2); + // add the other two with arbitrary keys + $enc['ook'] = $enc3; + $enc[] = $enc4; + // iterate over the sub-enclosures + $act = []; + foreach ($enc as $k => $v) { + $act[$k] = $v; + } + $exp = [0 => $enc1, 1 => $enc2, 'ook' => $enc3, 2 => $enc4]; + $this->assertSame($exp, $act); + } + + public function testManipulateMagicEnclosureProperties(): void { + // create two enclosures + $enc1 = new Enclosure; + $enc2 = new Enclosure; + $enc1->title = "Ook"; + $enc2->title = "Eek"; + // create a set with the two enclosures + $enc = new Enclosure($enc1, $enc2); + // check titles + $this->assertTrue(isset($enc[0])); + $this->assertTrue(isset($enc[1])); + $this->assertSame("Ook", $enc[0]->title); + $this->assertSame("Eek", $enc[1]->title); + $this->assertSame("Ook", $enc->title); + // unset the title of the first sub-enclosure via the parent + unset($enc->title); + $this->assertFalse(isset($enc[0]->title)); + $this->assertFalse(isset($enc->title)); + // remove the first sub-enclosure + unset($enc[0]); + $this->assertFalse(isset($enc[0])); + $this->assertTrue(isset($enc[1])); + $this->assertSame("Eek", $enc->title); + // set the title of the second enclosure via the parent + $enc->title = "Ack"; + $this->assertSame("Ack", $enc[1]->title); + $this->assertSame("Ack", $enc->title); + } +}