Skip to content

Commit

Permalink
Add php dataclasses for regmem data
Browse files Browse the repository at this point in the history
This is a stucture to let us add custom methods and properties
to parts of the regmem information.
  • Loading branch information
ajparsons committed Feb 17, 2025
1 parent 414cf6d commit eb4063f
Show file tree
Hide file tree
Showing 17 changed files with 357 additions and 6 deletions.
19 changes: 19 additions & 0 deletions classes/DataClass/BaseCollection.php
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);
}
}
17 changes: 17 additions & 0 deletions classes/DataClass/BaseInterface.php
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;
}
}
}
10 changes: 10 additions & 0 deletions classes/DataClass/BaseModel.php
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;

}
15 changes: 15 additions & 0 deletions classes/DataClass/Regmem/Annotation.php
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;
}
16 changes: 16 additions & 0 deletions classes/DataClass/Regmem/AnnotationList.php
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;
}
}
18 changes: 18 additions & 0 deletions classes/DataClass/Regmem/Category.php
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;

}
16 changes: 16 additions & 0 deletions classes/DataClass/Regmem/CategoryList.php
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;
}
}
40 changes: 40 additions & 0 deletions classes/DataClass/Regmem/Detail.php
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;
}

}
16 changes: 16 additions & 0 deletions classes/DataClass/Regmem/DetailGroup.php
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;
}
}
16 changes: 16 additions & 0 deletions classes/DataClass/Regmem/DetailGroupList.php
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;
}
}
16 changes: 16 additions & 0 deletions classes/DataClass/Regmem/EntryList.php
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;
}
}
24 changes: 24 additions & 0 deletions classes/DataClass/Regmem/InfoEntry.php
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;

}
47 changes: 47 additions & 0 deletions classes/DataClass/Regmem/Person.php
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");
}
}
16 changes: 16 additions & 0 deletions classes/DataClass/Regmem/PersonList.php
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;
}
}
16 changes: 16 additions & 0 deletions classes/DataClass/Regmem/Register.php
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;
}
7 changes: 4 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,23 @@
}
},
"require": {
"php": ">=7.3",
"php": ">=7.4",
"filp/whoops": "2.*",
"ircmaxell/password-compat": "1.0.4",
"facebook/graph-sdk": "^5.6",
"stripe/stripe-php": "^14.9",
"predis/predis": "^1.1",
"volnix/csrf": "^1.2",
"phpmailer/phpmailer": "^6.5",
"erusev/parsedown": "^1.7"
"erusev/parsedown": "^1.7",
"rutek/dataclass": "^0.1.2"
},
"require-dev": {
"phpunit/phpunit": "9.*"
},
"config": {
"platform": {
"php": "7.3"
"php": "7.4"
},
"allow-plugins": {
"symfony/flex": true
Expand Down
Loading

0 comments on commit eb4063f

Please sign in to comment.