Skip to content

Commit

Permalink
Merge pull request #4 from SocialEngine/feature/get-set-metaprops
Browse files Browse the repository at this point in the history
Add a way to get/set meta properties
  • Loading branch information
Danno040 committed Aug 31, 2015
2 parents d9ca1da + 82d2b03 commit 983e50e
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 17 deletions.
16 changes: 12 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
SOCIALENGINE_STRD="`pwd`/vendor/socialengine/sniffer-rules/src/Socialengine/SnifferRules/Standard/"
PHPCS=php ./vendor/bin/phpcs
INSTALLED=$(shell ${PHPCS} -i)

test:
php ./vendor/bin/phpunit

test-cover:
php ./vendor/bin/phpunit --coverage-clover=coverage.clover

sniff:
php ./vendor/bin/phpcs --colors --standard=SocialEngine --runtime-set allowSnakeCaseMethodName '[{"classSuffix":"Test","methodPrefix":["test"]}]' src tests
sniff: check-standard
${PHPCS} --colors --standard=SocialEngine --runtime-set allowSnakeCaseMethodName '[{"classSuffix":"Test","methodPrefix":["test"]}]' src tests

sniff-fix:
php ./vendor/bin/phpcbf --colors --standard='./vendor/socialengine/sniffer-rules/src/Socialengine/SnifferRules/Standard/SocialEngine' --runtime-set allowSnakeCaseMethodName '[{"classSuffix":"Test","methodPrefix":["test"]}]' src tests
sniff-fix: check-standard
php ./vendor/bin/phpcbf --colors --standard=SocialEngine --runtime-set allowSnakeCaseMethodName '[{"classSuffix":"Test","methodPrefix":["test"]}]' src tests

check-standard:
ifeq (,$(findstring SocialEngine, $(INSTALLED)))
php ./vendor/bin/phpcs --config-set installed_paths ${SOCIALENGINE_STRD}
endif
61 changes: 61 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,67 @@ array(0) {
*/
```

### Meta Attributes

You can set and get "meta" attributes - ones that do not have attributes directly attached to them.

This is good if you have an entity that stores a `category_id` but does not store the category name

```php
<?php

require('vendor/autoload.php');

class MyEntity extends \SocialEngine\Unum\Entity
{
protected $category_id;
protected $first_name;
protected $last_name;

protected function getCategoryName()
{
$categoryNamesMap = [
1 => 'Awesome'
];

return $categoryNamesMap[$this->getProp('category_id')];
}

/**
* You can also set meta properties
*/
protected function setName($name)
{
list($firstName, $lastName) = explode(' ', $name, 2);

$this->fromArray([
'first_name' => $firstName,
'last_name' => $lastName
]);
}
}

$entity = new MyEntity(['category_id' => 1]);
var_dump($entity->categoryName);
/*
string(7) "Awesome"
*/

$entity['name'] = 'Duke Orcino';
var_dump($entity->toArray(true));
/*
array(2) {
'first_name' =>
string(4) "Duke"
'last_name' =>
string(6) "Orcino"
}
*/
```

The entity above will let you access Meta attributes via the normal syntax. Use build in `setProp` and `getProp` so
that you can leverage some of the validation and attribute checking code **Unum** offers you.

## Code Style

Please follow the following guides and code standards:
Expand Down
31 changes: 18 additions & 13 deletions src/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,6 @@ public function isLoaded()
public function fromArray(array $data)
{
foreach ($data as $k => $v) {
if (!property_exists($this, $k) || in_array($k, $this->propertyBlacklist)) {
throw new InvalidArgumentException(sprintf(
'$data key: "%s" was not listed in the allowed properties',
$k
));
}
$this->setProp($k, $v);
}

Expand Down Expand Up @@ -188,7 +182,7 @@ public function isClean()
return empty($this->propertyDirty);
}

/**
/**
* @param $key
* @param $value
*/
Expand All @@ -197,18 +191,28 @@ protected function setProp($key, $value)
if (!is_string($key)) {
throw new InvalidArgumentException('$key must be a string');
}
if (!property_exists($this, $key) || in_array($key, $this->propertyBlacklist)) {

$methodName = $this->normalizeAccessor($key);
$hasAccessor = method_exists($this, $methodName);

if ((!property_exists($this, $key) || in_array($key, $this->propertyBlacklist))) {
if ($hasAccessor) {
// let meta property set the actual prop.
$this->$methodName($value);
return;
}

throw new InvalidArgumentException(sprintf(
'Key: "%s" is not an allowed property',
$key
));
}


$this->propertyLoaded[] = $key;

if ($this->getProp($key) !== $value) {
$methodName = $this->normalizeAccessor($key);
if (method_exists($this, $methodName)) {
if ($hasAccessor) {
$this->$methodName($value);
} else {
$this->$key = $value;
Expand All @@ -223,15 +227,16 @@ protected function setProp($key, $value)
*/
protected function getProp($key)
{
if (!in_array($key, $this->propertyLoaded)) {
$methodName = $this->normalizeAccessor($key, false);
$hasAccessor = method_exists($this, $methodName);
if (!in_array($key, $this->propertyLoaded) && !$hasAccessor) {
throw new InvalidArgumentException(sprintf(
'Key: "%s" was not loaded',
$key
));
}
$methodName = $this->normalizeAccessor($key, false);

if (method_exists($this, $methodName)) {
if ($hasAccessor) {
return $this->$methodName();
} else {
return $this->$key;
Expand Down
16 changes: 16 additions & 0 deletions tests/EntityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,22 @@ public function test_assignment_to_nonexistant_property()
$entity->fake = 1;
}

public function test_meta_property_get()
{
$entity = new TestEntity();
$this->assertEquals($entity::META_PROPERTY, $entity->customMethodProp);
}

public function test_meta_property_set()
{
$entity = new TestEntity();
$entity->uppercaseTestProp = 'lowercase_test';
$this->assertEquals(strtoupper('lowercase_test'), $entity->testProp);

$entity2 = new TestEntity(['uppercaseTestProp' => 'lowercase_test']);
$this->assertEquals(strtoupper('lowercase_test'), $entity->testProp);
}

public function test_isset()
{
$entity = new TestEntity(['testProp' => 'test1']);
Expand Down
17 changes: 17 additions & 0 deletions tests/Fixtures/TestEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ class TestEntity extends Entity
protected $test_prop;
// @codingStandardsIgnoreEnd

/**
* For accessing from tests
*
* @var string
*/
const META_PROPERTY = 'This is a meta property';

protected function getGetMethodProp()
{
return $this->getMethodProp . ' From Get';
Expand All @@ -26,4 +33,14 @@ protected function setSetMethodProp($value)
{
$this->setMethodProp = $value . ' From Set';
}

protected function getCustomMethodProp()
{
return self::META_PROPERTY;
}

public function setUppercaseTestProp($value)
{
$this->setProp('testProp', strtoupper($value));
}
}

0 comments on commit 983e50e

Please sign in to comment.