@@ -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 | } |