Skip to content

Instantly share code, notes, and snippets.

@jeremyworboys
Created March 22, 2014 06:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeremyworboys/9702192 to your computer and use it in GitHub Desktop.
Save jeremyworboys/9702192 to your computer and use it in GitHub Desktop.
Annotated Entity Trait

I just threw this together quickly to play with the Reflection API.

There are a bunch of optimisations that could be made if you actually want to use this in a project (which I don't really recommend anyway).

The MIT License (MIT)

Copyright (c) 2014 Jeremy Worboys

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
<?php
namespace Domain\Entities\Traits;
/**
* Annotated entity trait
*/
trait AnnotatedEntityTrait
{
/** @var array */
private $annotated_properties;
/**
* @param string $name
* @param mixed $params
*/
public function __call($name, $params)
{
if (preg_match('/^set(.+)/', $name, $matches)) {
$property = snake_case($matches[1]);
if ($this->isSettable($property)) {
$this->$property = $params[0];
return;
}
} elseif (preg_match('/^is(.+)/', $name, $matches)) {
$property = snake_case($matches[1]);
if ($this->isBooleanGettable($property)) {
return (bool)$this->$property;
}
} elseif (preg_match('/^get(.+)/', $name, $matches)) {
$property = snake_case($matches[1]);
if ($this->isGettable($property)) {
return $this->$property;
}
}
throw new \BadFunctionCallException;
}
/**
* Determine if property is settable
*
* @param string $property
* @return boolean
*/
private function isSettable($property)
{
return in_array($property, $this->getSettableProperties());
}
/**
* Determine if property is a boolean and gettable
*
* @param string $property
* @return boolean
*/
private function isBooleanGettable($property)
{
return (in_array($property, $this->getGettableProperties())
&& in_array($property, $this->getBooleanProperties()));
}
/**
* Determine if property is settable
*
* @param string $property
* @return boolean
*/
private function isGettable($property)
{
return (in_array($property, $this->getGettableProperties())
&& !in_array($property, $this->getBooleanProperties()));
}
/**
* Retrieve properties marked as settable
*
* @return array
*/
private function getSettableProperties()
{
$properties = [];
$class = new \ReflectionClass($this);
foreach ($class->getProperties() as $prop) {
if (preg_match('/@settable/', $prop->getDocComment())) {
$properties[] = $prop->getName();
}
}
return $properties;
}
/**
* Retrieve properties marked as gettable
*
* @return array
*/
private function getGettableProperties()
{
$properties = [];
$class = new \ReflectionClass($this);
foreach ($class->getProperties() as $prop) {
if (preg_match('/@gettable/', $prop->getDocComment())) {
$properties[] = $prop->getName();
}
}
return $properties;
}
/**
* Retrieve properties marked as boolean
*
* @return array
*/
private function getBooleanProperties()
{
$properties = [];
$class = new \ReflectionClass($this);
foreach ($class->getProperties() as $prop) {
if (preg_match('/@var bool(?:ean)?/', $prop->getDocComment())) {
$properties[] = $prop->getName();
}
}
return $properties;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment