mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-08-06 15:06:31 +02:00
On to PHP7^!
This commit is contained in:
@@ -8,44 +8,44 @@ namespace DesignPatterns\Structural\Proxy;
|
||||
class Record
|
||||
{
|
||||
/**
|
||||
* @var array|null
|
||||
* @var string[]
|
||||
*/
|
||||
protected $data;
|
||||
private $data;
|
||||
|
||||
/**
|
||||
* @param null $data
|
||||
* @param string[] $data
|
||||
*/
|
||||
public function __construct($data = null)
|
||||
public function __construct(array $data = [])
|
||||
{
|
||||
$this->data = (array) $data;
|
||||
$this->data = $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* magic setter.
|
||||
* magic setter
|
||||
*
|
||||
* @param string $name
|
||||
* @param mixed $value
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __set($name, $value)
|
||||
public function __set(string $name, string $value)
|
||||
{
|
||||
$this->data[(string) $name] = $value;
|
||||
$this->data[$name] = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* magic getter.
|
||||
* magic getter
|
||||
*
|
||||
* @param string $name
|
||||
*
|
||||
* @return mixed|null
|
||||
* @return string|null
|
||||
*/
|
||||
public function __get($name)
|
||||
public function __get(string $name): string
|
||||
{
|
||||
if (array_key_exists($name, $this->data)) {
|
||||
return $this->data[(string) $name];
|
||||
if (isset($this->data[$name])) {
|
||||
return $this->data[$name];
|
||||
} else {
|
||||
return;
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -2,25 +2,22 @@
|
||||
|
||||
namespace DesignPatterns\Structural\Proxy;
|
||||
|
||||
/**
|
||||
* Class RecordProxy.
|
||||
*/
|
||||
class RecordProxy extends Record
|
||||
{
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
protected $isDirty = false;
|
||||
private $isDirty = false;
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
protected $isInitialized = false;
|
||||
private $isInitialized = false;
|
||||
|
||||
/**
|
||||
* @param array $data
|
||||
*/
|
||||
public function __construct($data)
|
||||
public function __construct(array $data)
|
||||
{
|
||||
parent::__construct($data);
|
||||
|
||||
@@ -28,23 +25,30 @@ class RecordProxy extends Record
|
||||
// since Record will hold our business logic, we don't want to
|
||||
// implement this behaviour there, but instead in a new proxy class
|
||||
// that extends the Record class
|
||||
if (null !== $data) {
|
||||
if (count($data) > 0) {
|
||||
$this->isInitialized = true;
|
||||
$this->isDirty = true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* magic setter.
|
||||
* magic setter
|
||||
*
|
||||
* @param string $name
|
||||
* @param mixed $value
|
||||
*
|
||||
* @return void
|
||||
* @param string $value
|
||||
*/
|
||||
public function __set($name, $value)
|
||||
public function __set(string $name, string $value)
|
||||
{
|
||||
$this->isDirty = true;
|
||||
|
||||
parent::__set($name, $value);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function isDirty(): bool
|
||||
{
|
||||
return $this->isDirty;
|
||||
}
|
||||
}
|
||||
|
25
Structural/Proxy/Tests/ProxyTest.php
Normal file
25
Structural/Proxy/Tests/ProxyTest.php
Normal file
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
namespace DesignPatterns\Structural\Proxy\Tests;
|
||||
|
||||
use DesignPatterns\Structural\Decorator;
|
||||
use DesignPatterns\Structural\Proxy\RecordProxy;
|
||||
|
||||
class ProxyTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
public function testWillSetDirtyFlagInProxy()
|
||||
{
|
||||
$recordProxy = new RecordProxy([]);
|
||||
$recordProxy->foobar = 'baz';
|
||||
|
||||
$this->assertTrue($recordProxy->isDirty());
|
||||
}
|
||||
|
||||
public function testProxyIsInstanceOfRecord()
|
||||
{
|
||||
$recordProxy = new RecordProxy([]);
|
||||
$recordProxy->foobar = 'baz';
|
||||
|
||||
$this->assertInstanceOf('DesignPatterns\Structural\Proxy\Record', $recordProxy);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user