@@ -13,165 +13,165 @@ |
||
| 13 | 13 | abstract class Entity |
| 14 | 14 | { |
| 15 | 15 | |
| 16 | - /** |
|
| 17 | - * @var array Columns |
|
| 18 | - */ |
|
| 19 | - protected static $columns = []; |
|
| 20 | - |
|
| 21 | - /** |
|
| 22 | - * @var array Internal entity properties |
|
| 23 | - */ |
|
| 24 | - protected $properties = []; |
|
| 25 | - |
|
| 26 | - /** |
|
| 27 | - * Get entity columns |
|
| 28 | - * |
|
| 29 | - * @return array Entity columns |
|
| 30 | - */ |
|
| 31 | - public static function getColumns() |
|
| 32 | - { |
|
| 33 | - $columns = array_merge([ |
|
| 34 | - 'id' => 'integer' |
|
| 35 | - ], static::$columns); |
|
| 36 | - |
|
| 37 | - $columns = array_merge($columns, [ |
|
| 38 | - 'modified' => 'DateTime', |
|
| 39 | - 'created' => 'DateTime' |
|
| 40 | - ]); |
|
| 41 | - |
|
| 42 | - return $columns; |
|
| 43 | - } |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * Set entity property |
|
| 47 | - * |
|
| 48 | - * @param string $key Entity property name |
|
| 49 | - * @param object|integer|double|string|array|boolean|null $value Entity property value |
|
| 50 | - * |
|
| 51 | - * @throws InvalidEntityPropertyException If entity does not have that property |
|
| 52 | - * @throws InvalidValueTypeForEntityPropertyException If value is of the wrong type |
|
| 53 | - */ |
|
| 54 | - public function set($key, $value) |
|
| 55 | - { |
|
| 56 | - if (!isset(self::getColumns()[$key])) { |
|
| 57 | - throw new InvalidEntityPropertyException([get_class($this), $key]); |
|
| 58 | - } |
|
| 59 | - |
|
| 60 | - $this->properties[$key] = $this->validatePropertyForValue($key, $value); |
|
| 61 | - } |
|
| 62 | - |
|
| 63 | - /** |
|
| 64 | - * Get entity property |
|
| 65 | - * |
|
| 66 | - * @param string $key Entity property |
|
| 67 | - * |
|
| 68 | - * @return object|integer|double|string|array|boolean|null Entity property value for given key |
|
| 69 | - * |
|
| 70 | - * @throws InvalidEntityPropertyException If entity does not have that property |
|
| 71 | - */ |
|
| 72 | - public function get($key) |
|
| 73 | - { |
|
| 74 | - if (!isset(self::getColumns()[$key])) { |
|
| 75 | - throw new InvalidEntityPropertyException([get_class($this), $key]); |
|
| 76 | - } |
|
| 77 | - |
|
| 78 | - return $this->properties[$key]; |
|
| 79 | - } |
|
| 80 | - |
|
| 81 | - /** |
|
| 82 | - * Return table structur for saving |
|
| 83 | - * Example: `['table_field_name' => $this->fieldName]` |
|
| 84 | - * |
|
| 85 | - * @param bool $includeId Should the ID column be included |
|
| 86 | - * |
|
| 87 | - * @return array |
|
| 88 | - */ |
|
| 89 | - public function toArray($includeId) |
|
| 90 | - { |
|
| 91 | - $array = []; |
|
| 92 | - |
|
| 93 | - foreach (self::getColumns() as $column => $type) { |
|
| 94 | - if ($column === 'id' && !$includeId) { |
|
| 95 | - continue; |
|
| 96 | - } |
|
| 97 | - |
|
| 98 | - $property = new EntityProperty($type); |
|
| 99 | - |
|
| 100 | - $value = $this->get($column); |
|
| 101 | - $value = $property->formatValueForDatabase($value); |
|
| 102 | - |
|
| 103 | - $array[":$column"] = $value; |
|
| 104 | - } |
|
| 105 | - |
|
| 106 | - return $array; |
|
| 107 | - } |
|
| 108 | - |
|
| 109 | - /** |
|
| 110 | - * Validate property for given value |
|
| 111 | - * |
|
| 112 | - * @param string $key Entity property name |
|
| 113 | - * @param mixed $value Entity property value |
|
| 114 | - * |
|
| 115 | - * @return object|integer|double|string|array|boolean|null Value |
|
| 116 | - * |
|
| 117 | - * @throws InvalidEntityPropertyException If entity does not have that property |
|
| 118 | - * @throws InvalidValueTypeForEntityPropertyException If value is of the wrong type |
|
| 119 | - */ |
|
| 120 | - protected function validatePropertyForValue($key, $value) |
|
| 121 | - { |
|
| 122 | - if (!isset(self::getColumns()[$key])) { |
|
| 123 | - throw new InvalidEntityPropertyException([get_class($this), $key]); |
|
| 124 | - } |
|
| 125 | - |
|
| 126 | - /** |
|
| 127 | - * Allow NULL |
|
| 128 | - */ |
|
| 129 | - if (!is_null($value)) { |
|
| 130 | - $valueType = gettype($value); |
|
| 131 | - |
|
| 132 | - /** |
|
| 133 | - * Get class if object |
|
| 134 | - */ |
|
| 135 | - if ($valueType === 'object') { |
|
| 136 | - $valueType = get_class($value); |
|
| 137 | - } |
|
| 138 | - |
|
| 139 | - /** |
|
| 140 | - * Handle alias types |
|
| 141 | - */ |
|
| 142 | - $columnType = self::getColumns()[$key]; |
|
| 143 | - |
|
| 144 | - switch ($columnType) { |
|
| 145 | - case 'Date': |
|
| 146 | - $columnType = 'DateTime'; |
|
| 147 | - break; |
|
| 148 | - } |
|
| 149 | - |
|
| 150 | - /** |
|
| 151 | - * Validate type |
|
| 152 | - */ |
|
| 153 | - if ($valueType !== $columnType) { |
|
| 154 | - throw new InvalidValueTypeForEntityPropertyException([ |
|
| 155 | - get_class($this), |
|
| 156 | - $key, |
|
| 157 | - $valueType, |
|
| 158 | - $columnType |
|
| 159 | - ]); |
|
| 160 | - } |
|
| 161 | - } |
|
| 162 | - |
|
| 163 | - return $value; |
|
| 164 | - } |
|
| 165 | - |
|
| 166 | - /** |
|
| 167 | - * @param int $id |
|
| 168 | - * @param \DateTime $modified |
|
| 169 | - * @param \DateTime $created |
|
| 170 | - */ |
|
| 171 | - public function __construct($id, \DateTime $modified, \DateTime $created) |
|
| 172 | - { |
|
| 173 | - $this->set('id', $id); |
|
| 174 | - $this->set('modified', $modified); |
|
| 175 | - $this->set('created', $created); |
|
| 176 | - } |
|
| 16 | + /** |
|
| 17 | + * @var array Columns |
|
| 18 | + */ |
|
| 19 | + protected static $columns = []; |
|
| 20 | + |
|
| 21 | + /** |
|
| 22 | + * @var array Internal entity properties |
|
| 23 | + */ |
|
| 24 | + protected $properties = []; |
|
| 25 | + |
|
| 26 | + /** |
|
| 27 | + * Get entity columns |
|
| 28 | + * |
|
| 29 | + * @return array Entity columns |
|
| 30 | + */ |
|
| 31 | + public static function getColumns() |
|
| 32 | + { |
|
| 33 | + $columns = array_merge([ |
|
| 34 | + 'id' => 'integer' |
|
| 35 | + ], static::$columns); |
|
| 36 | + |
|
| 37 | + $columns = array_merge($columns, [ |
|
| 38 | + 'modified' => 'DateTime', |
|
| 39 | + 'created' => 'DateTime' |
|
| 40 | + ]); |
|
| 41 | + |
|
| 42 | + return $columns; |
|
| 43 | + } |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * Set entity property |
|
| 47 | + * |
|
| 48 | + * @param string $key Entity property name |
|
| 49 | + * @param object|integer|double|string|array|boolean|null $value Entity property value |
|
| 50 | + * |
|
| 51 | + * @throws InvalidEntityPropertyException If entity does not have that property |
|
| 52 | + * @throws InvalidValueTypeForEntityPropertyException If value is of the wrong type |
|
| 53 | + */ |
|
| 54 | + public function set($key, $value) |
|
| 55 | + { |
|
| 56 | + if (!isset(self::getColumns()[$key])) { |
|
| 57 | + throw new InvalidEntityPropertyException([get_class($this), $key]); |
|
| 58 | + } |
|
| 59 | + |
|
| 60 | + $this->properties[$key] = $this->validatePropertyForValue($key, $value); |
|
| 61 | + } |
|
| 62 | + |
|
| 63 | + /** |
|
| 64 | + * Get entity property |
|
| 65 | + * |
|
| 66 | + * @param string $key Entity property |
|
| 67 | + * |
|
| 68 | + * @return object|integer|double|string|array|boolean|null Entity property value for given key |
|
| 69 | + * |
|
| 70 | + * @throws InvalidEntityPropertyException If entity does not have that property |
|
| 71 | + */ |
|
| 72 | + public function get($key) |
|
| 73 | + { |
|
| 74 | + if (!isset(self::getColumns()[$key])) { |
|
| 75 | + throw new InvalidEntityPropertyException([get_class($this), $key]); |
|
| 76 | + } |
|
| 77 | + |
|
| 78 | + return $this->properties[$key]; |
|
| 79 | + } |
|
| 80 | + |
|
| 81 | + /** |
|
| 82 | + * Return table structur for saving |
|
| 83 | + * Example: `['table_field_name' => $this->fieldName]` |
|
| 84 | + * |
|
| 85 | + * @param bool $includeId Should the ID column be included |
|
| 86 | + * |
|
| 87 | + * @return array |
|
| 88 | + */ |
|
| 89 | + public function toArray($includeId) |
|
| 90 | + { |
|
| 91 | + $array = []; |
|
| 92 | + |
|
| 93 | + foreach (self::getColumns() as $column => $type) { |
|
| 94 | + if ($column === 'id' && !$includeId) { |
|
| 95 | + continue; |
|
| 96 | + } |
|
| 97 | + |
|
| 98 | + $property = new EntityProperty($type); |
|
| 99 | + |
|
| 100 | + $value = $this->get($column); |
|
| 101 | + $value = $property->formatValueForDatabase($value); |
|
| 102 | + |
|
| 103 | + $array[":$column"] = $value; |
|
| 104 | + } |
|
| 105 | + |
|
| 106 | + return $array; |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + /** |
|
| 110 | + * Validate property for given value |
|
| 111 | + * |
|
| 112 | + * @param string $key Entity property name |
|
| 113 | + * @param mixed $value Entity property value |
|
| 114 | + * |
|
| 115 | + * @return object|integer|double|string|array|boolean|null Value |
|
| 116 | + * |
|
| 117 | + * @throws InvalidEntityPropertyException If entity does not have that property |
|
| 118 | + * @throws InvalidValueTypeForEntityPropertyException If value is of the wrong type |
|
| 119 | + */ |
|
| 120 | + protected function validatePropertyForValue($key, $value) |
|
| 121 | + { |
|
| 122 | + if (!isset(self::getColumns()[$key])) { |
|
| 123 | + throw new InvalidEntityPropertyException([get_class($this), $key]); |
|
| 124 | + } |
|
| 125 | + |
|
| 126 | + /** |
|
| 127 | + * Allow NULL |
|
| 128 | + */ |
|
| 129 | + if (!is_null($value)) { |
|
| 130 | + $valueType = gettype($value); |
|
| 131 | + |
|
| 132 | + /** |
|
| 133 | + * Get class if object |
|
| 134 | + */ |
|
| 135 | + if ($valueType === 'object') { |
|
| 136 | + $valueType = get_class($value); |
|
| 137 | + } |
|
| 138 | + |
|
| 139 | + /** |
|
| 140 | + * Handle alias types |
|
| 141 | + */ |
|
| 142 | + $columnType = self::getColumns()[$key]; |
|
| 143 | + |
|
| 144 | + switch ($columnType) { |
|
| 145 | + case 'Date': |
|
| 146 | + $columnType = 'DateTime'; |
|
| 147 | + break; |
|
| 148 | + } |
|
| 149 | + |
|
| 150 | + /** |
|
| 151 | + * Validate type |
|
| 152 | + */ |
|
| 153 | + if ($valueType !== $columnType) { |
|
| 154 | + throw new InvalidValueTypeForEntityPropertyException([ |
|
| 155 | + get_class($this), |
|
| 156 | + $key, |
|
| 157 | + $valueType, |
|
| 158 | + $columnType |
|
| 159 | + ]); |
|
| 160 | + } |
|
| 161 | + } |
|
| 162 | + |
|
| 163 | + return $value; |
|
| 164 | + } |
|
| 165 | + |
|
| 166 | + /** |
|
| 167 | + * @param int $id |
|
| 168 | + * @param \DateTime $modified |
|
| 169 | + * @param \DateTime $created |
|
| 170 | + */ |
|
| 171 | + public function __construct($id, \DateTime $modified, \DateTime $created) |
|
| 172 | + { |
|
| 173 | + $this->set('id', $id); |
|
| 174 | + $this->set('modified', $modified); |
|
| 175 | + $this->set('created', $created); |
|
| 176 | + } |
|
| 177 | 177 | } |
@@ -10,8 +10,7 @@ discard block |
||
| 10 | 10 | * |
| 11 | 11 | * @package Zortje\MVC\Model\Table\Entity |
| 12 | 12 | */ |
| 13 | -abstract class Entity |
|
| 14 | -{ |
|
| 13 | +abstract class Entity { |
|
| 15 | 14 | |
| 16 | 15 | /** |
| 17 | 16 | * @var array Columns |
@@ -28,8 +27,7 @@ discard block |
||
| 28 | 27 | * |
| 29 | 28 | * @return array Entity columns |
| 30 | 29 | */ |
| 31 | - public static function getColumns() |
|
| 32 | - { |
|
| 30 | + public static function getColumns() { |
|
| 33 | 31 | $columns = array_merge([ |
| 34 | 32 | 'id' => 'integer' |
| 35 | 33 | ], static::$columns); |
@@ -51,8 +49,7 @@ discard block |
||
| 51 | 49 | * @throws InvalidEntityPropertyException If entity does not have that property |
| 52 | 50 | * @throws InvalidValueTypeForEntityPropertyException If value is of the wrong type |
| 53 | 51 | */ |
| 54 | - public function set($key, $value) |
|
| 55 | - { |
|
| 52 | + public function set($key, $value) { |
|
| 56 | 53 | if (!isset(self::getColumns()[$key])) { |
| 57 | 54 | throw new InvalidEntityPropertyException([get_class($this), $key]); |
| 58 | 55 | } |
@@ -69,8 +66,7 @@ discard block |
||
| 69 | 66 | * |
| 70 | 67 | * @throws InvalidEntityPropertyException If entity does not have that property |
| 71 | 68 | */ |
| 72 | - public function get($key) |
|
| 73 | - { |
|
| 69 | + public function get($key) { |
|
| 74 | 70 | if (!isset(self::getColumns()[$key])) { |
| 75 | 71 | throw new InvalidEntityPropertyException([get_class($this), $key]); |
| 76 | 72 | } |
@@ -86,8 +82,7 @@ discard block |
||
| 86 | 82 | * |
| 87 | 83 | * @return array |
| 88 | 84 | */ |
| 89 | - public function toArray($includeId) |
|
| 90 | - { |
|
| 85 | + public function toArray($includeId) { |
|
| 91 | 86 | $array = []; |
| 92 | 87 | |
| 93 | 88 | foreach (self::getColumns() as $column => $type) { |
@@ -117,8 +112,7 @@ discard block |
||
| 117 | 112 | * @throws InvalidEntityPropertyException If entity does not have that property |
| 118 | 113 | * @throws InvalidValueTypeForEntityPropertyException If value is of the wrong type |
| 119 | 114 | */ |
| 120 | - protected function validatePropertyForValue($key, $value) |
|
| 121 | - { |
|
| 115 | + protected function validatePropertyForValue($key, $value) { |
|
| 122 | 116 | if (!isset(self::getColumns()[$key])) { |
| 123 | 117 | throw new InvalidEntityPropertyException([get_class($this), $key]); |
| 124 | 118 | } |
@@ -168,8 +162,7 @@ discard block |
||
| 168 | 162 | * @param \DateTime $modified |
| 169 | 163 | * @param \DateTime $created |
| 170 | 164 | */ |
| 171 | - public function __construct($id, \DateTime $modified, \DateTime $created) |
|
| 172 | - { |
|
| 165 | + public function __construct($id, \DateTime $modified, \DateTime $created) { |
|
| 173 | 166 | $this->set('id', $id); |
| 174 | 167 | $this->set('modified', $modified); |
| 175 | 168 | $this->set('created', $created); |
@@ -10,49 +10,49 @@ |
||
| 10 | 10 | class EntityFactory |
| 11 | 11 | { |
| 12 | 12 | |
| 13 | - /** |
|
| 14 | - * @var String Entity class |
|
| 15 | - */ |
|
| 16 | - protected $entityClass; |
|
| 17 | - |
|
| 18 | - /** |
|
| 19 | - * @param array $array |
|
| 20 | - * |
|
| 21 | - * @return object |
|
| 22 | - */ |
|
| 23 | - public function createFromArray(array $array) |
|
| 24 | - { |
|
| 25 | - /** |
|
| 26 | - * @var Entity $entity |
|
| 27 | - */ |
|
| 28 | - $reflector = new \ReflectionClass($this->entityClass); |
|
| 29 | - |
|
| 30 | - $entity = $reflector->newInstanceWithoutConstructor(); |
|
| 31 | - |
|
| 32 | - $columns = $entity::getColumns(); |
|
| 33 | - unset($columns['id'], $columns['modified'], $columns['created']); |
|
| 34 | - |
|
| 35 | - $arguments = []; |
|
| 36 | - |
|
| 37 | - foreach ($columns as $column => $type) { |
|
| 38 | - $property = new EntityProperty($type); |
|
| 39 | - |
|
| 40 | - $arguments[$column] = $property->formatValueForEntity($array[$column]); |
|
| 41 | - } |
|
| 42 | - |
|
| 43 | - $entity = $reflector->newInstanceArgs($arguments); |
|
| 44 | - $entity->set('id', (int) $array['id']); |
|
| 45 | - $entity->set('modified', new \DateTime($array['modified'])); |
|
| 46 | - $entity->set('created', new \DateTime($array['created'])); |
|
| 47 | - |
|
| 48 | - return $entity; |
|
| 49 | - } |
|
| 50 | - |
|
| 51 | - /** |
|
| 52 | - * @param string $entityClass |
|
| 53 | - */ |
|
| 54 | - public function __construct($entityClass) |
|
| 55 | - { |
|
| 56 | - $this->entityClass = $entityClass; |
|
| 57 | - } |
|
| 13 | + /** |
|
| 14 | + * @var String Entity class |
|
| 15 | + */ |
|
| 16 | + protected $entityClass; |
|
| 17 | + |
|
| 18 | + /** |
|
| 19 | + * @param array $array |
|
| 20 | + * |
|
| 21 | + * @return object |
|
| 22 | + */ |
|
| 23 | + public function createFromArray(array $array) |
|
| 24 | + { |
|
| 25 | + /** |
|
| 26 | + * @var Entity $entity |
|
| 27 | + */ |
|
| 28 | + $reflector = new \ReflectionClass($this->entityClass); |
|
| 29 | + |
|
| 30 | + $entity = $reflector->newInstanceWithoutConstructor(); |
|
| 31 | + |
|
| 32 | + $columns = $entity::getColumns(); |
|
| 33 | + unset($columns['id'], $columns['modified'], $columns['created']); |
|
| 34 | + |
|
| 35 | + $arguments = []; |
|
| 36 | + |
|
| 37 | + foreach ($columns as $column => $type) { |
|
| 38 | + $property = new EntityProperty($type); |
|
| 39 | + |
|
| 40 | + $arguments[$column] = $property->formatValueForEntity($array[$column]); |
|
| 41 | + } |
|
| 42 | + |
|
| 43 | + $entity = $reflector->newInstanceArgs($arguments); |
|
| 44 | + $entity->set('id', (int) $array['id']); |
|
| 45 | + $entity->set('modified', new \DateTime($array['modified'])); |
|
| 46 | + $entity->set('created', new \DateTime($array['created'])); |
|
| 47 | + |
|
| 48 | + return $entity; |
|
| 49 | + } |
|
| 50 | + |
|
| 51 | + /** |
|
| 52 | + * @param string $entityClass |
|
| 53 | + */ |
|
| 54 | + public function __construct($entityClass) |
|
| 55 | + { |
|
| 56 | + $this->entityClass = $entityClass; |
|
| 57 | + } |
|
| 58 | 58 | } |
@@ -7,8 +7,7 @@ discard block |
||
| 7 | 7 | * |
| 8 | 8 | * @package Zortje\MVC\Model\Table\Entity |
| 9 | 9 | */ |
| 10 | -class EntityFactory |
|
| 11 | -{ |
|
| 10 | +class EntityFactory { |
|
| 12 | 11 | |
| 13 | 12 | /** |
| 14 | 13 | * @var String Entity class |
@@ -20,8 +19,7 @@ discard block |
||
| 20 | 19 | * |
| 21 | 20 | * @return object |
| 22 | 21 | */ |
| 23 | - public function createFromArray(array $array) |
|
| 24 | - { |
|
| 22 | + public function createFromArray(array $array) { |
|
| 25 | 23 | /** |
| 26 | 24 | * @var Entity $entity |
| 27 | 25 | */ |
@@ -51,8 +49,7 @@ discard block |
||
| 51 | 49 | /** |
| 52 | 50 | * @param string $entityClass |
| 53 | 51 | */ |
| 54 | - public function __construct($entityClass) |
|
| 55 | - { |
|
| 52 | + public function __construct($entityClass) { |
|
| 56 | 53 | $this->entityClass = $entityClass; |
| 57 | 54 | } |
| 58 | 55 | } |
@@ -10,75 +10,75 @@ |
||
| 10 | 10 | class EntityProperty |
| 11 | 11 | { |
| 12 | 12 | |
| 13 | - /** |
|
| 14 | - * @var string Entity property type |
|
| 15 | - */ |
|
| 16 | - protected $type; |
|
| 13 | + /** |
|
| 14 | + * @var string Entity property type |
|
| 15 | + */ |
|
| 16 | + protected $type; |
|
| 17 | 17 | |
| 18 | - /** |
|
| 19 | - * Format value according to entity property type |
|
| 20 | - * |
|
| 21 | - * @param mixed $value Value |
|
| 22 | - * |
|
| 23 | - * @return mixed Value |
|
| 24 | - */ |
|
| 25 | - public function formatValueForEntity($value) |
|
| 26 | - { |
|
| 27 | - switch ($this->type) { |
|
| 28 | - case 'string': |
|
| 29 | - $value = "$value"; |
|
| 30 | - break; |
|
| 18 | + /** |
|
| 19 | + * Format value according to entity property type |
|
| 20 | + * |
|
| 21 | + * @param mixed $value Value |
|
| 22 | + * |
|
| 23 | + * @return mixed Value |
|
| 24 | + */ |
|
| 25 | + public function formatValueForEntity($value) |
|
| 26 | + { |
|
| 27 | + switch ($this->type) { |
|
| 28 | + case 'string': |
|
| 29 | + $value = "$value"; |
|
| 30 | + break; |
|
| 31 | 31 | |
| 32 | - case 'integer': |
|
| 33 | - $value = (int) $value; |
|
| 34 | - break; |
|
| 32 | + case 'integer': |
|
| 33 | + $value = (int) $value; |
|
| 34 | + break; |
|
| 35 | 35 | |
| 36 | - case 'float': |
|
| 37 | - $value = (float) $value; |
|
| 38 | - break; |
|
| 36 | + case 'float': |
|
| 37 | + $value = (float) $value; |
|
| 38 | + break; |
|
| 39 | 39 | |
| 40 | - case 'Date': |
|
| 41 | - case 'DateTime': |
|
| 42 | - $value = new \DateTime($value); |
|
| 43 | - break; |
|
| 44 | - } |
|
| 40 | + case 'Date': |
|
| 41 | + case 'DateTime': |
|
| 42 | + $value = new \DateTime($value); |
|
| 43 | + break; |
|
| 44 | + } |
|
| 45 | 45 | |
| 46 | - return $value; |
|
| 47 | - } |
|
| 46 | + return $value; |
|
| 47 | + } |
|
| 48 | 48 | |
| 49 | - /** |
|
| 50 | - * Format value for insertion into the database |
|
| 51 | - * |
|
| 52 | - * @param mixed $value Value |
|
| 53 | - * |
|
| 54 | - * @return mixed Value |
|
| 55 | - */ |
|
| 56 | - public function formatValueForDatabase($value) |
|
| 57 | - { |
|
| 58 | - switch ($this->type) { |
|
| 59 | - case 'Date': |
|
| 60 | - /** |
|
| 61 | - * @var \DateTime $value |
|
| 62 | - */ |
|
| 63 | - $value = $value->format('Y-m-d'); |
|
| 64 | - break; |
|
| 49 | + /** |
|
| 50 | + * Format value for insertion into the database |
|
| 51 | + * |
|
| 52 | + * @param mixed $value Value |
|
| 53 | + * |
|
| 54 | + * @return mixed Value |
|
| 55 | + */ |
|
| 56 | + public function formatValueForDatabase($value) |
|
| 57 | + { |
|
| 58 | + switch ($this->type) { |
|
| 59 | + case 'Date': |
|
| 60 | + /** |
|
| 61 | + * @var \DateTime $value |
|
| 62 | + */ |
|
| 63 | + $value = $value->format('Y-m-d'); |
|
| 64 | + break; |
|
| 65 | 65 | |
| 66 | - case 'DateTime': |
|
| 67 | - /** |
|
| 68 | - * @var \DateTime $value |
|
| 69 | - */ |
|
| 70 | - $value = $value->format('Y-m-d H:i:s'); |
|
| 71 | - break; |
|
| 72 | - } |
|
| 66 | + case 'DateTime': |
|
| 67 | + /** |
|
| 68 | + * @var \DateTime $value |
|
| 69 | + */ |
|
| 70 | + $value = $value->format('Y-m-d H:i:s'); |
|
| 71 | + break; |
|
| 72 | + } |
|
| 73 | 73 | |
| 74 | - return $value; |
|
| 75 | - } |
|
| 74 | + return $value; |
|
| 75 | + } |
|
| 76 | 76 | |
| 77 | - /** |
|
| 78 | - * @param string $type |
|
| 79 | - */ |
|
| 80 | - public function __construct($type) |
|
| 81 | - { |
|
| 82 | - $this->type = $type; |
|
| 83 | - } |
|
| 77 | + /** |
|
| 78 | + * @param string $type |
|
| 79 | + */ |
|
| 80 | + public function __construct($type) |
|
| 81 | + { |
|
| 82 | + $this->type = $type; |
|
| 83 | + } |
|
| 84 | 84 | } |
@@ -7,8 +7,7 @@ discard block |
||
| 7 | 7 | * |
| 8 | 8 | * @package Zortje\MVC\Model\Table\Entity |
| 9 | 9 | */ |
| 10 | -class EntityProperty |
|
| 11 | -{ |
|
| 10 | +class EntityProperty { |
|
| 12 | 11 | |
| 13 | 12 | /** |
| 14 | 13 | * @var string Entity property type |
@@ -22,8 +21,7 @@ discard block |
||
| 22 | 21 | * |
| 23 | 22 | * @return mixed Value |
| 24 | 23 | */ |
| 25 | - public function formatValueForEntity($value) |
|
| 26 | - { |
|
| 24 | + public function formatValueForEntity($value) { |
|
| 27 | 25 | switch ($this->type) { |
| 28 | 26 | case 'string': |
| 29 | 27 | $value = "$value"; |
@@ -53,8 +51,7 @@ discard block |
||
| 53 | 51 | * |
| 54 | 52 | * @return mixed Value |
| 55 | 53 | */ |
| 56 | - public function formatValueForDatabase($value) |
|
| 57 | - { |
|
| 54 | + public function formatValueForDatabase($value) { |
|
| 58 | 55 | switch ($this->type) { |
| 59 | 56 | case 'Date': |
| 60 | 57 | /** |
@@ -77,8 +74,7 @@ discard block |
||
| 77 | 74 | /** |
| 78 | 75 | * @param string $type |
| 79 | 76 | */ |
| 80 | - public function __construct($type) |
|
| 81 | - { |
|
| 77 | + public function __construct($type) { |
|
| 82 | 78 | $this->type = $type; |
| 83 | 79 | } |
| 84 | 80 | } |
@@ -12,16 +12,16 @@ |
||
| 12 | 12 | class EntityClassNonexistentException extends Exception |
| 13 | 13 | { |
| 14 | 14 | |
| 15 | - /** |
|
| 16 | - * {@inheritdoc} |
|
| 17 | - */ |
|
| 18 | - protected $template = 'Subclass %s defined entity class %s is nonexistent'; |
|
| 15 | + /** |
|
| 16 | + * {@inheritdoc} |
|
| 17 | + */ |
|
| 18 | + protected $template = 'Subclass %s defined entity class %s is nonexistent'; |
|
| 19 | 19 | |
| 20 | - /** |
|
| 21 | - * {@inheritdoc} |
|
| 22 | - */ |
|
| 23 | - public function __construct($message) |
|
| 24 | - { |
|
| 25 | - parent::__construct($message); |
|
| 26 | - } |
|
| 20 | + /** |
|
| 21 | + * {@inheritdoc} |
|
| 22 | + */ |
|
| 23 | + public function __construct($message) |
|
| 24 | + { |
|
| 25 | + parent::__construct($message); |
|
| 26 | + } |
|
| 27 | 27 | } |
@@ -9,8 +9,7 @@ discard block |
||
| 9 | 9 | * |
| 10 | 10 | * @package Zortje\MVC\Model\Table\Entity\Exception |
| 11 | 11 | */ |
| 12 | -class EntityClassNonexistentException extends Exception |
|
| 13 | -{ |
|
| 12 | +class EntityClassNonexistentException extends Exception { |
|
| 14 | 13 | |
| 15 | 14 | /** |
| 16 | 15 | * {@inheritdoc} |
@@ -20,8 +19,7 @@ discard block |
||
| 20 | 19 | /** |
| 21 | 20 | * {@inheritdoc} |
| 22 | 21 | */ |
| 23 | - public function __construct($message) |
|
| 24 | - { |
|
| 22 | + public function __construct($message) { |
|
| 25 | 23 | parent::__construct($message); |
| 26 | 24 | } |
| 27 | 25 | } |
@@ -12,16 +12,16 @@ |
||
| 12 | 12 | class EntityClassNotDefinedException extends Exception |
| 13 | 13 | { |
| 14 | 14 | |
| 15 | - /** |
|
| 16 | - * {@inheritdoc} |
|
| 17 | - */ |
|
| 18 | - protected $template = 'Subclass %s does not have a entity class defined'; |
|
| 15 | + /** |
|
| 16 | + * {@inheritdoc} |
|
| 17 | + */ |
|
| 18 | + protected $template = 'Subclass %s does not have a entity class defined'; |
|
| 19 | 19 | |
| 20 | - /** |
|
| 21 | - * {@inheritdoc} |
|
| 22 | - */ |
|
| 23 | - public function __construct($message) |
|
| 24 | - { |
|
| 25 | - parent::__construct($message); |
|
| 26 | - } |
|
| 20 | + /** |
|
| 21 | + * {@inheritdoc} |
|
| 22 | + */ |
|
| 23 | + public function __construct($message) |
|
| 24 | + { |
|
| 25 | + parent::__construct($message); |
|
| 26 | + } |
|
| 27 | 27 | } |
@@ -9,8 +9,7 @@ discard block |
||
| 9 | 9 | * |
| 10 | 10 | * @package Zortje\MVC\Model\Table\Entity\Exception |
| 11 | 11 | */ |
| 12 | -class EntityClassNotDefinedException extends Exception |
|
| 13 | -{ |
|
| 12 | +class EntityClassNotDefinedException extends Exception { |
|
| 14 | 13 | |
| 15 | 14 | /** |
| 16 | 15 | * {@inheritdoc} |
@@ -20,8 +19,7 @@ discard block |
||
| 20 | 19 | /** |
| 21 | 20 | * {@inheritdoc} |
| 22 | 21 | */ |
| 23 | - public function __construct($message) |
|
| 24 | - { |
|
| 22 | + public function __construct($message) { |
|
| 25 | 23 | parent::__construct($message); |
| 26 | 24 | } |
| 27 | 25 | } |
@@ -12,16 +12,16 @@ |
||
| 12 | 12 | class InvalidEntityPropertyException extends Exception |
| 13 | 13 | { |
| 14 | 14 | |
| 15 | - /** |
|
| 16 | - * {@inheritdoc} |
|
| 17 | - */ |
|
| 18 | - protected $template = 'Entity %s does not have a property named %s'; |
|
| 15 | + /** |
|
| 16 | + * {@inheritdoc} |
|
| 17 | + */ |
|
| 18 | + protected $template = 'Entity %s does not have a property named %s'; |
|
| 19 | 19 | |
| 20 | - /** |
|
| 21 | - * {@inheritdoc} |
|
| 22 | - */ |
|
| 23 | - public function __construct($message) |
|
| 24 | - { |
|
| 25 | - parent::__construct($message); |
|
| 26 | - } |
|
| 20 | + /** |
|
| 21 | + * {@inheritdoc} |
|
| 22 | + */ |
|
| 23 | + public function __construct($message) |
|
| 24 | + { |
|
| 25 | + parent::__construct($message); |
|
| 26 | + } |
|
| 27 | 27 | } |
@@ -9,8 +9,7 @@ discard block |
||
| 9 | 9 | * |
| 10 | 10 | * @package Zortje\MVC\Model\Table\Entity\Exception |
| 11 | 11 | */ |
| 12 | -class InvalidEntityPropertyException extends Exception |
|
| 13 | -{ |
|
| 12 | +class InvalidEntityPropertyException extends Exception { |
|
| 14 | 13 | |
| 15 | 14 | /** |
| 16 | 15 | * {@inheritdoc} |
@@ -20,8 +19,7 @@ discard block |
||
| 20 | 19 | /** |
| 21 | 20 | * {@inheritdoc} |
| 22 | 21 | */ |
| 23 | - public function __construct($message) |
|
| 24 | - { |
|
| 22 | + public function __construct($message) { |
|
| 25 | 23 | parent::__construct($message); |
| 26 | 24 | } |
| 27 | 25 | } |
@@ -12,16 +12,16 @@ |
||
| 12 | 12 | class InvalidValueTypeForEntityPropertyException extends Exception |
| 13 | 13 | { |
| 14 | 14 | |
| 15 | - /** |
|
| 16 | - * {@inheritdoc} |
|
| 17 | - */ |
|
| 18 | - protected $template = 'Entity %s property %s is of type %s and not %s'; |
|
| 15 | + /** |
|
| 16 | + * {@inheritdoc} |
|
| 17 | + */ |
|
| 18 | + protected $template = 'Entity %s property %s is of type %s and not %s'; |
|
| 19 | 19 | |
| 20 | - /** |
|
| 21 | - * {@inheritdoc} |
|
| 22 | - */ |
|
| 23 | - public function __construct($message) |
|
| 24 | - { |
|
| 25 | - parent::__construct($message); |
|
| 26 | - } |
|
| 20 | + /** |
|
| 21 | + * {@inheritdoc} |
|
| 22 | + */ |
|
| 23 | + public function __construct($message) |
|
| 24 | + { |
|
| 25 | + parent::__construct($message); |
|
| 26 | + } |
|
| 27 | 27 | } |
@@ -9,8 +9,7 @@ discard block |
||
| 9 | 9 | * |
| 10 | 10 | * @package Zortje\MVC\Model\Table\Entity\Exception |
| 11 | 11 | */ |
| 12 | -class InvalidValueTypeForEntityPropertyException extends Exception |
|
| 13 | -{ |
|
| 12 | +class InvalidValueTypeForEntityPropertyException extends Exception { |
|
| 14 | 13 | |
| 15 | 14 | /** |
| 16 | 15 | * {@inheritdoc} |
@@ -20,8 +19,7 @@ discard block |
||
| 20 | 19 | /** |
| 21 | 20 | * {@inheritdoc} |
| 22 | 21 | */ |
| 23 | - public function __construct($message) |
|
| 24 | - { |
|
| 22 | + public function __construct($message) { |
|
| 25 | 23 | parent::__construct($message); |
| 26 | 24 | } |
| 27 | 25 | } |
@@ -18,174 +18,174 @@ |
||
| 18 | 18 | abstract class Table |
| 19 | 19 | { |
| 20 | 20 | |
| 21 | - /** |
|
| 22 | - * @var \PDO Connection |
|
| 23 | - */ |
|
| 24 | - protected $pdo; |
|
| 25 | - |
|
| 26 | - /** |
|
| 27 | - * @var string Table name |
|
| 28 | - */ |
|
| 29 | - protected $tableName; |
|
| 30 | - |
|
| 31 | - /** |
|
| 32 | - * @var String Entity class |
|
| 33 | - */ |
|
| 34 | - protected $entityClass; |
|
| 35 | - |
|
| 36 | - /** |
|
| 37 | - * @var SQLCommand SQL Command |
|
| 38 | - */ |
|
| 39 | - protected $sqlCommand; |
|
| 40 | - |
|
| 41 | - /** |
|
| 42 | - * Get table name |
|
| 43 | - * |
|
| 44 | - * @return string Table name |
|
| 45 | - */ |
|
| 46 | - public function getTableName() |
|
| 47 | - { |
|
| 48 | - return $this->tableName; |
|
| 49 | - } |
|
| 50 | - |
|
| 51 | - /** |
|
| 52 | - * Find all entities |
|
| 53 | - * |
|
| 54 | - * @return Entity[] Entities |
|
| 55 | - */ |
|
| 56 | - public function findAll() |
|
| 57 | - { |
|
| 58 | - $stmt = $this->pdo->prepare($this->sqlCommand->selectFrom()); |
|
| 59 | - $stmt->execute(); |
|
| 60 | - |
|
| 61 | - return $this->createEntitiesFromStatement($stmt); |
|
| 62 | - } |
|
| 63 | - |
|
| 64 | - /** |
|
| 65 | - * Find all entities where key is equal to the given value |
|
| 66 | - * |
|
| 67 | - * @param $key |
|
| 68 | - * @param $value |
|
| 69 | - * |
|
| 70 | - * @throws InvalidEntityPropertyException If entity does not have that property |
|
| 71 | - * |
|
| 72 | - * @return Entity[] Entities |
|
| 73 | - */ |
|
| 74 | - public function findBy($key, $value) |
|
| 75 | - { |
|
| 76 | - /** |
|
| 77 | - * Check if entity have the property |
|
| 78 | - */ |
|
| 79 | - $reflector = new \ReflectionClass($this->entityClass); |
|
| 80 | - |
|
| 81 | - $entity = $reflector->newInstanceWithoutConstructor(); |
|
| 82 | - |
|
| 83 | - if (!isset($entity::getColumns()[$key])) { |
|
| 84 | - throw new InvalidEntityPropertyException([$this->entityClass, $key]); |
|
| 85 | - } |
|
| 86 | - |
|
| 87 | - /** |
|
| 88 | - * Execute with key-value condition |
|
| 89 | - */ |
|
| 90 | - $stmt = $this->pdo->prepare($this->sqlCommand->selectFromWhere($key)); |
|
| 91 | - $stmt->execute([":$key" => $value]); |
|
| 92 | - |
|
| 93 | - return $this->createEntitiesFromStatement($stmt); |
|
| 94 | - } |
|
| 95 | - |
|
| 96 | - /** |
|
| 97 | - * Insert entity into dabase |
|
| 98 | - * |
|
| 99 | - * @param Entity $entity Entity |
|
| 100 | - * |
|
| 101 | - * @return int Inserted entity ID |
|
| 102 | - */ |
|
| 103 | - public function insert(Entity $entity) |
|
| 104 | - { |
|
| 105 | - $stmt = $this->pdo->prepare($this->sqlCommand->insertInto()); |
|
| 106 | - |
|
| 107 | - $now = new \DateTime(); |
|
| 108 | - $now = $now->format('Y-m-d H:i:s'); |
|
| 109 | - |
|
| 110 | - $array = array_merge($entity->toArray(false), [ |
|
| 111 | - 'modified' => $now, |
|
| 112 | - 'created' => $now |
|
| 113 | - ]); |
|
| 114 | - |
|
| 115 | - $stmt->execute($array); |
|
| 116 | - |
|
| 117 | - return (int) $this->pdo->lastInsertId(); |
|
| 118 | - } |
|
| 119 | - |
|
| 120 | - public function update(Entity $entity) |
|
| 121 | - { |
|
| 122 | - // @todo Implement |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - public function delete(Entity $entity) |
|
| 126 | - { |
|
| 127 | - // @todo Implement |
|
| 128 | - } |
|
| 129 | - |
|
| 130 | - /** |
|
| 131 | - * Creates an array of Entity objects from statement |
|
| 132 | - * |
|
| 133 | - * @param \PDOStatement $statement |
|
| 134 | - * |
|
| 135 | - * @return Entity[] Entities from statement |
|
| 136 | - */ |
|
| 137 | - protected function createEntitiesFromStatement(\PDOStatement $statement) |
|
| 138 | - { |
|
| 139 | - $entities = []; |
|
| 140 | - |
|
| 141 | - $entityFactory = new EntityFactory($this->entityClass); |
|
| 142 | - |
|
| 143 | - foreach ($statement as $row) { |
|
| 144 | - $entities[] = $entityFactory->createFromArray($row); |
|
| 145 | - } |
|
| 146 | - |
|
| 147 | - return $entities; |
|
| 148 | - } |
|
| 149 | - |
|
| 150 | - /** |
|
| 151 | - * Create SQLCommand for this Table with provided Entity |
|
| 152 | - * |
|
| 153 | - * @return SQLCommand |
|
| 154 | - */ |
|
| 155 | - protected function createCommand() |
|
| 156 | - { |
|
| 157 | - $reflector = new \ReflectionClass($this->entityClass); |
|
| 158 | - |
|
| 159 | - $entity = $reflector->newInstanceWithoutConstructor(); |
|
| 160 | - |
|
| 161 | - $columns = $entity::getColumns(); |
|
| 162 | - |
|
| 163 | - return new SQLCommand($this->tableName, $columns); |
|
| 164 | - } |
|
| 165 | - |
|
| 166 | - /** |
|
| 167 | - * @param \PDO $pdo |
|
| 168 | - * |
|
| 169 | - * @throws TableNameNotDefinedException If table name is not defined in subclass |
|
| 170 | - * @throws EntityClassNotDefinedException If entity class is not defined in subclass |
|
| 171 | - * @throws EntityClassNonexistentException If entity class is nonexistent |
|
| 172 | - */ |
|
| 173 | - public function __construct(\PDO $pdo) |
|
| 174 | - { |
|
| 175 | - if (is_null($this->tableName)) { |
|
| 176 | - throw new TableNameNotDefinedException([get_class($this)]); |
|
| 177 | - } |
|
| 178 | - |
|
| 179 | - if (is_null($this->entityClass)) { |
|
| 180 | - throw new EntityClassNotDefinedException([get_class($this)]); |
|
| 181 | - } elseif (!class_exists($this->entityClass)) { |
|
| 182 | - throw new EntityClassNonexistentException([get_class($this), $this->entityClass]); |
|
| 183 | - } |
|
| 184 | - |
|
| 185 | - // @todo should check if `$this->entityClass` is subclass of Entity class |
|
| 186 | - |
|
| 187 | - $this->pdo = $pdo; |
|
| 188 | - |
|
| 189 | - $this->sqlCommand = $this->createCommand(); |
|
| 190 | - } |
|
| 21 | + /** |
|
| 22 | + * @var \PDO Connection |
|
| 23 | + */ |
|
| 24 | + protected $pdo; |
|
| 25 | + |
|
| 26 | + /** |
|
| 27 | + * @var string Table name |
|
| 28 | + */ |
|
| 29 | + protected $tableName; |
|
| 30 | + |
|
| 31 | + /** |
|
| 32 | + * @var String Entity class |
|
| 33 | + */ |
|
| 34 | + protected $entityClass; |
|
| 35 | + |
|
| 36 | + /** |
|
| 37 | + * @var SQLCommand SQL Command |
|
| 38 | + */ |
|
| 39 | + protected $sqlCommand; |
|
| 40 | + |
|
| 41 | + /** |
|
| 42 | + * Get table name |
|
| 43 | + * |
|
| 44 | + * @return string Table name |
|
| 45 | + */ |
|
| 46 | + public function getTableName() |
|
| 47 | + { |
|
| 48 | + return $this->tableName; |
|
| 49 | + } |
|
| 50 | + |
|
| 51 | + /** |
|
| 52 | + * Find all entities |
|
| 53 | + * |
|
| 54 | + * @return Entity[] Entities |
|
| 55 | + */ |
|
| 56 | + public function findAll() |
|
| 57 | + { |
|
| 58 | + $stmt = $this->pdo->prepare($this->sqlCommand->selectFrom()); |
|
| 59 | + $stmt->execute(); |
|
| 60 | + |
|
| 61 | + return $this->createEntitiesFromStatement($stmt); |
|
| 62 | + } |
|
| 63 | + |
|
| 64 | + /** |
|
| 65 | + * Find all entities where key is equal to the given value |
|
| 66 | + * |
|
| 67 | + * @param $key |
|
| 68 | + * @param $value |
|
| 69 | + * |
|
| 70 | + * @throws InvalidEntityPropertyException If entity does not have that property |
|
| 71 | + * |
|
| 72 | + * @return Entity[] Entities |
|
| 73 | + */ |
|
| 74 | + public function findBy($key, $value) |
|
| 75 | + { |
|
| 76 | + /** |
|
| 77 | + * Check if entity have the property |
|
| 78 | + */ |
|
| 79 | + $reflector = new \ReflectionClass($this->entityClass); |
|
| 80 | + |
|
| 81 | + $entity = $reflector->newInstanceWithoutConstructor(); |
|
| 82 | + |
|
| 83 | + if (!isset($entity::getColumns()[$key])) { |
|
| 84 | + throw new InvalidEntityPropertyException([$this->entityClass, $key]); |
|
| 85 | + } |
|
| 86 | + |
|
| 87 | + /** |
|
| 88 | + * Execute with key-value condition |
|
| 89 | + */ |
|
| 90 | + $stmt = $this->pdo->prepare($this->sqlCommand->selectFromWhere($key)); |
|
| 91 | + $stmt->execute([":$key" => $value]); |
|
| 92 | + |
|
| 93 | + return $this->createEntitiesFromStatement($stmt); |
|
| 94 | + } |
|
| 95 | + |
|
| 96 | + /** |
|
| 97 | + * Insert entity into dabase |
|
| 98 | + * |
|
| 99 | + * @param Entity $entity Entity |
|
| 100 | + * |
|
| 101 | + * @return int Inserted entity ID |
|
| 102 | + */ |
|
| 103 | + public function insert(Entity $entity) |
|
| 104 | + { |
|
| 105 | + $stmt = $this->pdo->prepare($this->sqlCommand->insertInto()); |
|
| 106 | + |
|
| 107 | + $now = new \DateTime(); |
|
| 108 | + $now = $now->format('Y-m-d H:i:s'); |
|
| 109 | + |
|
| 110 | + $array = array_merge($entity->toArray(false), [ |
|
| 111 | + 'modified' => $now, |
|
| 112 | + 'created' => $now |
|
| 113 | + ]); |
|
| 114 | + |
|
| 115 | + $stmt->execute($array); |
|
| 116 | + |
|
| 117 | + return (int) $this->pdo->lastInsertId(); |
|
| 118 | + } |
|
| 119 | + |
|
| 120 | + public function update(Entity $entity) |
|
| 121 | + { |
|
| 122 | + // @todo Implement |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + public function delete(Entity $entity) |
|
| 126 | + { |
|
| 127 | + // @todo Implement |
|
| 128 | + } |
|
| 129 | + |
|
| 130 | + /** |
|
| 131 | + * Creates an array of Entity objects from statement |
|
| 132 | + * |
|
| 133 | + * @param \PDOStatement $statement |
|
| 134 | + * |
|
| 135 | + * @return Entity[] Entities from statement |
|
| 136 | + */ |
|
| 137 | + protected function createEntitiesFromStatement(\PDOStatement $statement) |
|
| 138 | + { |
|
| 139 | + $entities = []; |
|
| 140 | + |
|
| 141 | + $entityFactory = new EntityFactory($this->entityClass); |
|
| 142 | + |
|
| 143 | + foreach ($statement as $row) { |
|
| 144 | + $entities[] = $entityFactory->createFromArray($row); |
|
| 145 | + } |
|
| 146 | + |
|
| 147 | + return $entities; |
|
| 148 | + } |
|
| 149 | + |
|
| 150 | + /** |
|
| 151 | + * Create SQLCommand for this Table with provided Entity |
|
| 152 | + * |
|
| 153 | + * @return SQLCommand |
|
| 154 | + */ |
|
| 155 | + protected function createCommand() |
|
| 156 | + { |
|
| 157 | + $reflector = new \ReflectionClass($this->entityClass); |
|
| 158 | + |
|
| 159 | + $entity = $reflector->newInstanceWithoutConstructor(); |
|
| 160 | + |
|
| 161 | + $columns = $entity::getColumns(); |
|
| 162 | + |
|
| 163 | + return new SQLCommand($this->tableName, $columns); |
|
| 164 | + } |
|
| 165 | + |
|
| 166 | + /** |
|
| 167 | + * @param \PDO $pdo |
|
| 168 | + * |
|
| 169 | + * @throws TableNameNotDefinedException If table name is not defined in subclass |
|
| 170 | + * @throws EntityClassNotDefinedException If entity class is not defined in subclass |
|
| 171 | + * @throws EntityClassNonexistentException If entity class is nonexistent |
|
| 172 | + */ |
|
| 173 | + public function __construct(\PDO $pdo) |
|
| 174 | + { |
|
| 175 | + if (is_null($this->tableName)) { |
|
| 176 | + throw new TableNameNotDefinedException([get_class($this)]); |
|
| 177 | + } |
|
| 178 | + |
|
| 179 | + if (is_null($this->entityClass)) { |
|
| 180 | + throw new EntityClassNotDefinedException([get_class($this)]); |
|
| 181 | + } elseif (!class_exists($this->entityClass)) { |
|
| 182 | + throw new EntityClassNonexistentException([get_class($this), $this->entityClass]); |
|
| 183 | + } |
|
| 184 | + |
|
| 185 | + // @todo should check if `$this->entityClass` is subclass of Entity class |
|
| 186 | + |
|
| 187 | + $this->pdo = $pdo; |
|
| 188 | + |
|
| 189 | + $this->sqlCommand = $this->createCommand(); |
|
| 190 | + } |
|
| 191 | 191 | } |
@@ -15,8 +15,7 @@ discard block |
||
| 15 | 15 | * |
| 16 | 16 | * @package Zortje\MVC\Model\Table |
| 17 | 17 | */ |
| 18 | -abstract class Table |
|
| 19 | -{ |
|
| 18 | +abstract class Table { |
|
| 20 | 19 | |
| 21 | 20 | /** |
| 22 | 21 | * @var \PDO Connection |
@@ -43,8 +42,7 @@ discard block |
||
| 43 | 42 | * |
| 44 | 43 | * @return string Table name |
| 45 | 44 | */ |
| 46 | - public function getTableName() |
|
| 47 | - { |
|
| 45 | + public function getTableName() { |
|
| 48 | 46 | return $this->tableName; |
| 49 | 47 | } |
| 50 | 48 | |
@@ -53,8 +51,7 @@ discard block |
||
| 53 | 51 | * |
| 54 | 52 | * @return Entity[] Entities |
| 55 | 53 | */ |
| 56 | - public function findAll() |
|
| 57 | - { |
|
| 54 | + public function findAll() { |
|
| 58 | 55 | $stmt = $this->pdo->prepare($this->sqlCommand->selectFrom()); |
| 59 | 56 | $stmt->execute(); |
| 60 | 57 | |
@@ -71,8 +68,7 @@ discard block |
||
| 71 | 68 | * |
| 72 | 69 | * @return Entity[] Entities |
| 73 | 70 | */ |
| 74 | - public function findBy($key, $value) |
|
| 75 | - { |
|
| 71 | + public function findBy($key, $value) { |
|
| 76 | 72 | /** |
| 77 | 73 | * Check if entity have the property |
| 78 | 74 | */ |
@@ -100,8 +96,7 @@ discard block |
||
| 100 | 96 | * |
| 101 | 97 | * @return int Inserted entity ID |
| 102 | 98 | */ |
| 103 | - public function insert(Entity $entity) |
|
| 104 | - { |
|
| 99 | + public function insert(Entity $entity) { |
|
| 105 | 100 | $stmt = $this->pdo->prepare($this->sqlCommand->insertInto()); |
| 106 | 101 | |
| 107 | 102 | $now = new \DateTime(); |
@@ -117,13 +112,11 @@ discard block |
||
| 117 | 112 | return (int) $this->pdo->lastInsertId(); |
| 118 | 113 | } |
| 119 | 114 | |
| 120 | - public function update(Entity $entity) |
|
| 121 | - { |
|
| 115 | + public function update(Entity $entity) { |
|
| 122 | 116 | // @todo Implement |
| 123 | 117 | } |
| 124 | 118 | |
| 125 | - public function delete(Entity $entity) |
|
| 126 | - { |
|
| 119 | + public function delete(Entity $entity) { |
|
| 127 | 120 | // @todo Implement |
| 128 | 121 | } |
| 129 | 122 | |
@@ -134,8 +127,7 @@ discard block |
||
| 134 | 127 | * |
| 135 | 128 | * @return Entity[] Entities from statement |
| 136 | 129 | */ |
| 137 | - protected function createEntitiesFromStatement(\PDOStatement $statement) |
|
| 138 | - { |
|
| 130 | + protected function createEntitiesFromStatement(\PDOStatement $statement) { |
|
| 139 | 131 | $entities = []; |
| 140 | 132 | |
| 141 | 133 | $entityFactory = new EntityFactory($this->entityClass); |
@@ -152,8 +144,7 @@ discard block |
||
| 152 | 144 | * |
| 153 | 145 | * @return SQLCommand |
| 154 | 146 | */ |
| 155 | - protected function createCommand() |
|
| 156 | - { |
|
| 147 | + protected function createCommand() { |
|
| 157 | 148 | $reflector = new \ReflectionClass($this->entityClass); |
| 158 | 149 | |
| 159 | 150 | $entity = $reflector->newInstanceWithoutConstructor(); |
@@ -170,8 +161,7 @@ discard block |
||
| 170 | 161 | * @throws EntityClassNotDefinedException If entity class is not defined in subclass |
| 171 | 162 | * @throws EntityClassNonexistentException If entity class is nonexistent |
| 172 | 163 | */ |
| 173 | - public function __construct(\PDO $pdo) |
|
| 174 | - { |
|
| 164 | + public function __construct(\PDO $pdo) { |
|
| 175 | 165 | if (is_null($this->tableName)) { |
| 176 | 166 | throw new TableNameNotDefinedException([get_class($this)]); |
| 177 | 167 | } |
@@ -10,75 +10,75 @@ |
||
| 10 | 10 | class HtmlRender |
| 11 | 11 | { |
| 12 | 12 | |
| 13 | - /** |
|
| 14 | - * @var array View variables |
|
| 15 | - */ |
|
| 16 | - protected $variables; |
|
| 13 | + /** |
|
| 14 | + * @var array View variables |
|
| 15 | + */ |
|
| 16 | + protected $variables; |
|
| 17 | 17 | |
| 18 | - /** |
|
| 19 | - * Render files |
|
| 20 | - * |
|
| 21 | - * First file in array is rendered first and key is set in variables array for use by following files |
|
| 22 | - * |
|
| 23 | - * @param array $files Files to render |
|
| 24 | - * |
|
| 25 | - * @return string Output |
|
| 26 | - */ |
|
| 27 | - public function render($files) |
|
| 28 | - { |
|
| 29 | - $output = ''; |
|
| 18 | + /** |
|
| 19 | + * Render files |
|
| 20 | + * |
|
| 21 | + * First file in array is rendered first and key is set in variables array for use by following files |
|
| 22 | + * |
|
| 23 | + * @param array $files Files to render |
|
| 24 | + * |
|
| 25 | + * @return string Output |
|
| 26 | + */ |
|
| 27 | + public function render($files) |
|
| 28 | + { |
|
| 29 | + $output = ''; |
|
| 30 | 30 | |
| 31 | - foreach ($files as $outputName => $file) { |
|
| 32 | - $output = $this->renderFile($file); |
|
| 31 | + foreach ($files as $outputName => $file) { |
|
| 32 | + $output = $this->renderFile($file); |
|
| 33 | 33 | |
| 34 | - $this->variables[$outputName] = $output; |
|
| 35 | - } |
|
| 34 | + $this->variables[$outputName] = $output; |
|
| 35 | + } |
|
| 36 | 36 | |
| 37 | - return $output; |
|
| 38 | - } |
|
| 37 | + return $output; |
|
| 38 | + } |
|
| 39 | 39 | |
| 40 | - /** |
|
| 41 | - * Render file |
|
| 42 | - * |
|
| 43 | - * @param string $file File to render |
|
| 44 | - * |
|
| 45 | - * @return string Output |
|
| 46 | - */ |
|
| 47 | - protected function renderFile($file) |
|
| 48 | - { |
|
| 49 | - if (!is_readable($file)) { |
|
| 50 | - throw new \InvalidArgumentException(sprintf('File "%s" is nonexistent (Working directory: %s)', $file, getcwd())); |
|
| 51 | - } |
|
| 40 | + /** |
|
| 41 | + * Render file |
|
| 42 | + * |
|
| 43 | + * @param string $file File to render |
|
| 44 | + * |
|
| 45 | + * @return string Output |
|
| 46 | + */ |
|
| 47 | + protected function renderFile($file) |
|
| 48 | + { |
|
| 49 | + if (!is_readable($file)) { |
|
| 50 | + throw new \InvalidArgumentException(sprintf('File "%s" is nonexistent (Working directory: %s)', $file, getcwd())); |
|
| 51 | + } |
|
| 52 | 52 | |
| 53 | - /** |
|
| 54 | - * Start the output buffer and require file |
|
| 55 | - */ |
|
| 56 | - ob_start(); |
|
| 53 | + /** |
|
| 54 | + * Start the output buffer and require file |
|
| 55 | + */ |
|
| 56 | + ob_start(); |
|
| 57 | 57 | |
| 58 | - /** |
|
| 59 | - * Prepare set variables for the view |
|
| 60 | - * |
|
| 61 | - * @todo Helpers, `$this->loadHelpers();` |
|
| 62 | - */ |
|
| 63 | - foreach ($this->variables as $variable => $value) { |
|
| 64 | - $$variable = $value; |
|
| 65 | - } |
|
| 58 | + /** |
|
| 59 | + * Prepare set variables for the view |
|
| 60 | + * |
|
| 61 | + * @todo Helpers, `$this->loadHelpers();` |
|
| 62 | + */ |
|
| 63 | + foreach ($this->variables as $variable => $value) { |
|
| 64 | + $$variable = $value; |
|
| 65 | + } |
|
| 66 | 66 | |
| 67 | - require $file; |
|
| 67 | + require $file; |
|
| 68 | 68 | |
| 69 | - /** |
|
| 70 | - * Clean the output buffer and return |
|
| 71 | - */ |
|
| 72 | - $output = ob_get_clean(); |
|
| 69 | + /** |
|
| 70 | + * Clean the output buffer and return |
|
| 71 | + */ |
|
| 72 | + $output = ob_get_clean(); |
|
| 73 | 73 | |
| 74 | - return $output; |
|
| 75 | - } |
|
| 74 | + return $output; |
|
| 75 | + } |
|
| 76 | 76 | |
| 77 | - /** |
|
| 78 | - * @param array $variables |
|
| 79 | - */ |
|
| 80 | - public function __construct($variables) |
|
| 81 | - { |
|
| 82 | - $this->variables = $variables; |
|
| 83 | - } |
|
| 77 | + /** |
|
| 78 | + * @param array $variables |
|
| 79 | + */ |
|
| 80 | + public function __construct($variables) |
|
| 81 | + { |
|
| 82 | + $this->variables = $variables; |
|
| 83 | + } |
|
| 84 | 84 | } |
@@ -7,8 +7,7 @@ discard block |
||
| 7 | 7 | * |
| 8 | 8 | * @package Zortje\View\Render |
| 9 | 9 | */ |
| 10 | -class HtmlRender |
|
| 11 | -{ |
|
| 10 | +class HtmlRender { |
|
| 12 | 11 | |
| 13 | 12 | /** |
| 14 | 13 | * @var array View variables |
@@ -24,8 +23,7 @@ discard block |
||
| 24 | 23 | * |
| 25 | 24 | * @return string Output |
| 26 | 25 | */ |
| 27 | - public function render($files) |
|
| 28 | - { |
|
| 26 | + public function render($files) { |
|
| 29 | 27 | $output = ''; |
| 30 | 28 | |
| 31 | 29 | foreach ($files as $outputName => $file) { |
@@ -44,8 +42,7 @@ discard block |
||
| 44 | 42 | * |
| 45 | 43 | * @return string Output |
| 46 | 44 | */ |
| 47 | - protected function renderFile($file) |
|
| 48 | - { |
|
| 45 | + protected function renderFile($file) { |
|
| 49 | 46 | if (!is_readable($file)) { |
| 50 | 47 | throw new \InvalidArgumentException(sprintf('File "%s" is nonexistent (Working directory: %s)', $file, getcwd())); |
| 51 | 48 | } |
@@ -77,8 +74,7 @@ discard block |
||
| 77 | 74 | /** |
| 78 | 75 | * @param array $variables |
| 79 | 76 | */ |
| 80 | - public function __construct($variables) |
|
| 81 | - { |
|
| 77 | + public function __construct($variables) { |
|
| 82 | 78 | $this->variables = $variables; |
| 83 | 79 | } |
| 84 | 80 | } |