J. King
6 years ago
9 changed files with 274 additions and 1 deletions
@ -0,0 +1,42 @@ |
|||
<?php |
|||
/** @license MIT |
|||
* Copyright 2018 J. King et al. |
|||
* See LICENSE and AUTHORS files for details */ |
|||
|
|||
declare(strict_types=1); |
|||
namespace JKingWeb\Lax; |
|||
|
|||
abstract class Collection implements \IteratorAggregate, \ArrayAccess, \Countable { |
|||
|
|||
protected $data = []; |
|||
|
|||
abstract public function primary(); |
|||
|
|||
public function getIterator(): \Traversable { |
|||
return ($this->data instanceof \Traversable) ? $this->data : new \ArrayIterator((array) $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) { |
|||
if (is_null($offset)) { |
|||
$this->data[] = $value; |
|||
} else { |
|||
$this->data[$offset] = $value; |
|||
} |
|||
} |
|||
|
|||
public function offsetUnset($offset) { |
|||
unset($this->data[$offset]); |
|||
} |
|||
} |
@ -0,0 +1,18 @@ |
|||
<?php |
|||
/** @license MIT |
|||
* Copyright 2018 J. King et al. |
|||
* See LICENSE and AUTHORS files for details */ |
|||
|
|||
declare(strict_types=1); |
|||
namespace JKingWeb\Lax; |
|||
|
|||
class Person { |
|||
public $name = ""; |
|||
public $mail = ""; |
|||
public $url = ""; |
|||
public $role = ""; |
|||
|
|||
public function __toString() { |
|||
return strlen($this->mail) ? $this->name."<".$this->mail.">" : $this->name; |
|||
} |
|||
} |
@ -0,0 +1,60 @@ |
|||
<?php |
|||
/** @license MIT |
|||
* Copyright 2018 J. King et al. |
|||
* See LICENSE and AUTHORS files for details */ |
|||
|
|||
declare(strict_types=1); |
|||
namespace JKingWeb\Lax; |
|||
|
|||
class PersonCollection extends Collection { |
|||
protected static $ranks = [ |
|||
'webmaster' => 10, |
|||
'editor' => 20, |
|||
'contributor' => 30, |
|||
'author' => 40, |
|||
]; |
|||
|
|||
/** Returns the primary person of the collection |
|||
* |
|||
* The primary is the first member of the highest-weight role |
|||
* |
|||
* Roles are ranked thus: |
|||
* author > contributor > editor > webmaster > (anything else) |
|||
* |
|||
*/ |
|||
public function primary() { |
|||
$out = null; |
|||
foreach ($this as $p) { |
|||
if (!$out) { |
|||
$out = $p; |
|||
} elseif (!isset(static::ranks[$p->role])) { |
|||
continue; |
|||
} elseif (static::ranks[$p->role] > static::ranks[$out->role]) { |
|||
$out = $p; |
|||
} |
|||
} |
|||
return $out; |
|||
} |
|||
|
|||
/** Returns a collection filtered to include only the specified roles */ |
|||
public function filterRole(string ...$role): self { |
|||
$out = new static; |
|||
foreach ($this as $p) { |
|||
if (in_array($p->role, $role)) { |
|||
$out[] = $p; |
|||
} |
|||
} |
|||
return $out; |
|||
} |
|||
|
|||
/** Returns a collection filtered to exclude the specified roles */ |
|||
public function filterOutRole(string ...$role): self { |
|||
$out = new static; |
|||
foreach ($this as $p) { |
|||
if (!in_array($p->role, $role)) { |
|||
$out[] = $p; |
|||
} |
|||
} |
|||
return $out; |
|||
} |
|||
} |
@ -0,0 +1,36 @@ |
|||
<?php |
|||
/** @license MIT |
|||
* Copyright 2018 J. King et al. |
|||
* See LICENSE and AUTHORS files for details */ |
|||
|
|||
declare(strict_types=1); |
|||
namespace JKingWeb\Lax; |
|||
|
|||
trait XMLEntryPrimitives { |
|||
|
|||
/** Primitive to fetch a collection of people associated with an RSS entry |
|||
* |
|||
* For RSS 2.0 this includes both native metadata and Dublin Core |
|||
*/ |
|||
protected function getPeopleRss2() { |
|||
$nodes = $this->fetchElements("./author|./dc:creator|./dc:contributor"); |
|||
if (!$nodes->length) { |
|||
return null; |
|||
} |
|||
$out = new PersonCollection; |
|||
$roles = [ |
|||
'author' => "author", |
|||
'creator' => "author", |
|||
'contributor' => "contributor", |
|||
]; |
|||
foreach ($nodes as $node) { |
|||
$text = $this->trimText($node->textContent); |
|||
if (strlen($text)) { |
|||
$p = $this->parsePersonText($text); |
|||
$p->role = $roles[$node->localName]; |
|||
$out[] = $p; |
|||
} |
|||
} |
|||
return $out; |
|||
} |
|||
} |
Loading…
Reference in new issue