| 1: | <?php |
| 2: | |
| 3: | trait Searchable { |
| 4: | /** |
| 5: | * Return items matching $match in column $column. |
| 6: | * |
| 7: | * @param mixed $match |
| 8: | * @param string $column |
| 9: | * @param bool $returnFirst |
| 10: | * @return array|null |
| 11: | */ |
| 12: | public static function getBy($match, $column = 'id', $returnFirst = false) { |
| 13: | $results = []; |
| 14: | foreach (static::getDataset() as $item) { |
| 15: | if (isset($item[$column]) && $item[$column] === $match) { |
| 16: | if ($returnFirst) { |
| 17: | return $item; |
| 18: | } |
| 19: | $results[] = $item; |
| 20: | } |
| 21: | } |
| 22: | return $returnFirst ? null : $results; |
| 23: | } |
| 24: | |
| 25: | // Each class must implement this |
| 26: | abstract protected static function getDataset(); |
| 27: | } |
| 28: |