You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
61 lines
1.6 KiB
61 lines
1.6 KiB
6 years ago
|
<?php
|
||
|
/** @license MIT
|
||
|
* Copyright 2018 J. King et al.
|
||
|
* See LICENSE and AUTHORS files for details */
|
||
|
|
||
|
declare(strict_types=1);
|
||
6 years ago
|
namespace JKingWeb\Lax\Person;
|
||
6 years ago
|
|
||
6 years ago
|
class Collection extends \JKingWeb\Lax\Collection {
|
||
6 years ago
|
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;
|
||
|
}
|
||
|
}
|