-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #315 from uzulla/refactoring-2021-05-23
コードリファクタリング、型関連の厳格化
- Loading branch information
Showing
53 changed files
with
448 additions
and
465 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
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,69 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Fc2blog\Model; | ||
|
||
use ArrayIterator; | ||
use Iterator; | ||
use LogicException; | ||
use ReflectionClass; | ||
use ReflectionException; | ||
|
||
trait ArrayIterableTrait | ||
{ | ||
public static function factory(array $list): ?self | ||
{ | ||
$self = new static(); | ||
$props = (new ReflectionClass(static::class))->getProperties(); | ||
foreach ($props as $prop) { | ||
$name = $prop->getName(); | ||
$self->{$name} = $list[$name]; | ||
} | ||
return $self; | ||
} | ||
|
||
public function asArray(): array | ||
{ | ||
return (array)$this; | ||
} | ||
|
||
// ==== for array access ==== | ||
public function offsetExists($offset): bool | ||
{ | ||
return isset($this->{$offset}); | ||
} | ||
|
||
public function offsetGet($offset) | ||
{ | ||
return $this->offsetExists($offset) ? $this->{$offset} : null; | ||
} | ||
|
||
public function offsetSet($offset, $value) | ||
{ | ||
$r = new ReflectionClass(static::class); | ||
try { | ||
$prop = $r->getProperty($offset); | ||
if ($prop->isPublic()) { | ||
$this->{$prop->getName()} = $value; | ||
} | ||
} catch (ReflectionException $e) { | ||
throw new LogicException("touch missing property " . $e->getMessage()); | ||
} | ||
} | ||
|
||
public function offsetUnset($offset) | ||
{ | ||
throw new LogicException("un-settable."); | ||
} | ||
|
||
public function getIterator(): Iterator | ||
{ | ||
return new ArrayIterator((array)$this); | ||
} | ||
|
||
public function count(): int | ||
{ | ||
$r = new ReflectionClass(static::class); | ||
return count($r->getProperties()); | ||
} | ||
} |
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,30 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Fc2blog\Model; | ||
|
||
use ArrayAccess; | ||
use Countable; | ||
use IteratorAggregate; | ||
|
||
class Blog implements ArrayAccess, IteratorAggregate, Countable | ||
{ | ||
use ArrayIterableTrait; | ||
|
||
public $id; | ||
public $user_id; | ||
public $name; | ||
public $nickname; | ||
public $introduction; | ||
public $template_pc_id; | ||
public $template_sp_id; | ||
public $timezone; | ||
public $open_status; | ||
public $ssl_enable; | ||
public $redirect_status_code; | ||
public $blog_password; | ||
public $trip_salt; | ||
public $last_posted_at; | ||
public $created_at; | ||
public $updated_at; | ||
} |
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
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
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
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.