-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This is a stucture to let us add custom methods and properties to parts of the regmem information.
- Loading branch information
Showing
17 changed files
with
357 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace MySociety\TheyWorkForYou\DataClass; | ||
|
||
use Rutek\Dataclass\Collection; | ||
|
||
/** | ||
* @template T | ||
* @extends Collection<T> | ||
*/ | ||
class BaseCollection extends Collection { | ||
use BaseInterface; | ||
|
||
public function isEmpty(): bool { | ||
return empty($this->items); | ||
} | ||
} |
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,17 @@ | ||
<?php | ||
|
||
namespace MySociety\TheyWorkForYou\DataClass; | ||
|
||
use function Rutek\Dataclass\transform; | ||
|
||
trait BaseInterface { | ||
public static function fromJson(string $json): self { | ||
$data = json_decode($json, true); | ||
try { | ||
return transform(static::class, $data); | ||
} catch (\Exception $transformException) { | ||
echo json_encode($transformException, JSON_PRETTY_PRINT); | ||
throw $transformException; | ||
} | ||
} | ||
} |
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,10 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace MySociety\TheyWorkForYou\DataClass; | ||
|
||
class BaseModel { | ||
use BaseInterface; | ||
|
||
} |
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,15 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace MySociety\TheyWorkForYou\DataClass\Regmem; | ||
|
||
use MySociety\TheyWorkForYou\DataClass\BaseModel; | ||
|
||
class Annotation extends BaseModel { | ||
public string $author; | ||
public string $type; | ||
public string $content; | ||
public string $date_added; | ||
public string $content_format; | ||
} |
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,16 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace MySociety\TheyWorkForYou\DataClass\Regmem; | ||
|
||
use MySociety\TheyWorkForYou\DataClass\BaseCollection; | ||
|
||
/** | ||
* @extends BaseCollection<Annotation> | ||
*/ | ||
class AnnotationList extends BaseCollection { | ||
public function __construct(Annotation ...$annotations) { | ||
$this->items = $annotations; | ||
} | ||
} |
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,18 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace MySociety\TheyWorkForYou\DataClass\Regmem; | ||
|
||
use MySociety\TheyWorkForYou\DataClass\BaseModel; | ||
|
||
class Category extends BaseModel { | ||
public string $category_id; | ||
public string $category_name; | ||
public ?string $category_description; | ||
public ?string $legislation_or_rule_name; | ||
public ?string $legislation_or_rule_url; | ||
public EntryList $summaries; | ||
public EntryList $entries; | ||
|
||
} |
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,16 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace MySociety\TheyWorkForYou\DataClass\Regmem; | ||
|
||
use MySociety\TheyWorkForYou\DataClass\BaseCollection; | ||
|
||
/** | ||
* @extends BaseCollection<Category> | ||
*/ | ||
class CategoryList extends BaseCollection { | ||
public function __construct(Category ...$categories) { | ||
$this->items = $categories; | ||
} | ||
} |
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,40 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace MySociety\TheyWorkForYou\DataClass\Regmem; | ||
|
||
use MySociety\TheyWorkForYou\DataClass\BaseModel; | ||
|
||
class Detail { | ||
public string $source; | ||
public ?string $slug = null; | ||
public ?string $display_as = null; | ||
public ?string $common_key = null; | ||
public ?string $description = null; | ||
public ?string $type = null; | ||
public $value = null; | ||
public AnnotationList $annotations; | ||
|
||
/** | ||
* @return \Iterator<Detail>| | ||
*/ | ||
public function sub_details(): \Iterator { | ||
|
||
|
||
$items = new \ArrayIterator(); | ||
|
||
if (!$this->value instanceof DetailGroup) { | ||
return $items; | ||
} | ||
|
||
foreach ($this->value as $detail_group) { | ||
foreach ($detail_group as $detail) { | ||
$items[] = $detail; | ||
} | ||
} | ||
|
||
return $items; | ||
} | ||
|
||
} |
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,16 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace MySociety\TheyWorkForYou\DataClass\Regmem; | ||
|
||
use MySociety\TheyWorkForYou\DataClass\BaseCollection; | ||
|
||
/** | ||
* @extends BaseCollection<Detail> | ||
*/ | ||
class DetailGroup extends BaseCollection { | ||
public function __construct(Detail ...$names) { | ||
$this->items = $names; | ||
} | ||
} |
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,16 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace MySociety\TheyWorkForYou\DataClass\Regmem; | ||
|
||
use MySociety\TheyWorkForYou\DataClass\BaseCollection; | ||
|
||
/** | ||
* @extends BaseCollection<DetailGroup> | ||
*/ | ||
class DetailGroupList extends BaseCollection { | ||
public function __construct(DetailGroup ...$groups) { | ||
$this->items = $groups; | ||
} | ||
} |
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,16 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace MySociety\TheyWorkForYou\DataClass\Regmem; | ||
|
||
use MySociety\TheyWorkForYou\DataClass\BaseCollection; | ||
|
||
/** | ||
* @extends BaseCollection<InfoEntry> | ||
*/ | ||
class EntryList extends BaseCollection { | ||
public function __construct(InfoEntry ...$entries) { | ||
$this->items = $entries; | ||
} | ||
} |
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,24 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace MySociety\TheyWorkForYou\DataClass\Regmem; | ||
|
||
use MySociety\TheyWorkForYou\DataClass\BaseModel; | ||
|
||
class InfoEntry extends BaseModel { | ||
public ?string $id = null; | ||
public ?string $comparable_id = null; | ||
public string $item_hash; | ||
public string $content; | ||
public string $content_format; | ||
public string $info_type; | ||
public ?string $date_registered = null; | ||
public ?string $date_published = null; | ||
public ?string $date_updated = null; | ||
public ?string $date_received = null; | ||
public AnnotationList $annotations; | ||
public DetailGroup $details; | ||
public EntryList $sub_entries; | ||
|
||
} |
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,47 @@ | ||
<?php | ||
/** | ||
* Mirrors pydantic model for deseralisation in a PHP context. | ||
* For adding display related helper functions. | ||
* @package TheyWorkForYou | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace MySociety\TheyWorkForYou\DataClass\Regmem; | ||
|
||
use MySociety\TheyWorkForYou\DataClass\BaseModel; | ||
|
||
use InvalidArgumentException; | ||
|
||
class Person extends BaseModel { | ||
public string $chamber; | ||
public string $language; | ||
public string $person_id; | ||
public string $person_name; | ||
public string $published_date; | ||
public CategoryList $categories; | ||
|
||
public function displayChamber(): string { | ||
switch ($this->chamber) { | ||
case 'house-of-commons': | ||
return 'House of Commons'; | ||
case 'welsh-parliament': | ||
return 'Senedd'; | ||
case 'scottish-parliament': | ||
return 'Scottish Parliament'; | ||
case 'northern-ireland-assembly': | ||
return 'Northern Ireland Assembly'; | ||
default: | ||
return 'Unknown Chamber'; | ||
} | ||
} | ||
|
||
public function getCategoryFromId(string $categoryId): Category { | ||
foreach ($this->categories as $category) { | ||
if ($category->category_id === $categoryId) { | ||
return $category; | ||
} | ||
} | ||
throw new InvalidArgumentException("Category $categoryId not found in register"); | ||
} | ||
} |
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,16 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace MySociety\TheyWorkForYou\DataClass\Regmem; | ||
|
||
use MySociety\TheyWorkForYou\DataClass\BaseCollection; | ||
|
||
/** | ||
* @extends BaseCollection<Person> | ||
*/ | ||
class PersonList extends BaseCollection { | ||
public function __construct(Person ...$persons) { | ||
$this->items = $persons; | ||
} | ||
} |
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,16 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace MySociety\TheyWorkForYou\DataClass\Regmem; | ||
|
||
use MySociety\TheyWorkForYou\DataClass\BaseModel; | ||
|
||
class Register extends BaseModel { | ||
public string $chamber; | ||
public string $language; | ||
public string $published_date; | ||
public AnnotationList $annotations; | ||
public EntryList $summaries; | ||
public PersonList $persons; | ||
} |
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
Oops, something went wrong.