Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Test entity
- *
- */
- class Test
- {
- private $id;
- private $string;
- private $string2;
- private $string3;
- private $char;
- private $int;
- private $int2;
- private $int3;
- private $int4;
- private $date;
- private $datetime;
- private $text;
- public function __construct($params)
- {
- $reflection = new ReflectionClass($this);
- foreach ($params as $key => $value) {
- $setter = 'set' . ucfirst($key);
- if ($reflection->hasMethod($setter)) {
- $this->$setter($value);
- } else {
- throw new \Exception("Entity class '" . get_class() . "' doesn't have setter '" . $setter . "'");
- }
- }
- }
- public function getId()
- {
- return (int) $this->id;
- }
- private function setId($value)
- {
- if ($value === null) {
- throw new \Exception('Field \'id\' cannot be null');
- }
- if (is_int($value) !== true) {
- throw new \Exception('Type mismatch in field \'id\'');
- }
- if ($value < 0 or $value > 4294967295) {
- throw new \OutOfBoundsException('Value of field \'id\' is out of range');
- }
- $this->id = $value;
- }
- public function getString()
- {
- return (string) $this->string;
- }
- public function setString($value)
- {
- if ($value === null) {
- throw new \Exception('Field \'string\' cannot be null');
- }
- if (is_string($value) !== true) {
- throw new \Exception('Type mismatch in field \'string\'');
- }
- if (mb_strlen($value) > 20) {
- throw new \OutOfBoundsException('Value of field \'string\' is out of range');
- }
- $this->string = $value;
- }
- public function getString2()
- {
- return (string) $this->string2;
- }
- public function setString2($value)
- {
- if ($value === null) {
- $this->string2 = null;
- return;
- }
- if (is_string($value) !== true) {
- throw new \Exception('Type mismatch in field \'string2\'');
- }
- if (mb_strlen($value) > 255) {
- throw new \OutOfBoundsException('Value of field \'string2\' is out of range');
- }
- $this->string2 = $value;
- }
- public function getString3()
- {
- return (string) $this->string3;
- }
- public function setString3($value)
- {
- if ($value === null) {
- $this->string3 = "hello";
- return;
- }
- if (is_string($value) !== true) {
- throw new \Exception('Type mismatch in field \'string3\'');
- }
- if (mb_strlen($value) > 50) {
- throw new \OutOfBoundsException('Value of field \'string3\' is out of range');
- }
- $this->string3 = $value;
- }
- public function getChar()
- {
- return (string) $this->char;
- }
- public function setChar($value)
- {
- if ($value === null) {
- $this->char = null;
- return;
- }
- if (is_string($value) !== true) {
- throw new \Exception('Type mismatch in field \'char\'');
- }
- if (mb_strlen($value) !== 3) {
- throw new \OutOfBoundsException('Value of field \'char\' is out of range');
- }
- $this->char = $value;
- }
- public function getInt()
- {
- return (int) $this->int;
- }
- public function setInt($value)
- {
- if ($value === null) {
- $this->int = null;
- return;
- }
- if (is_int($value) !== true) {
- throw new \Exception('Type mismatch in field \'int\'');
- }
- if ($value < -32768 or $value > 32767) {
- throw new \OutOfBoundsException('Value of field \'int\' is out of range');
- }
- $this->int = $value;
- }
- public function getInt2()
- {
- return (int) $this->int2;
- }
- public function setInt2($value)
- {
- if ($value === null) {
- $this->int2 = 5;
- return;
- }
- if (is_int($value) !== true) {
- throw new \Exception('Type mismatch in field \'int2\'');
- }
- if ($value < 0 or $value > 65535) {
- throw new \OutOfBoundsException('Value of field \'int2\' is out of range');
- }
- $this->int2 = $value;
- }
- public function getInt3()
- {
- return (int) $this->int3;
- }
- public function setInt3($value)
- {
- if ($value === null) {
- $this->int3 = 0;
- return;
- }
- if (is_int($value) !== true) {
- throw new \Exception('Type mismatch in field \'int3\'');
- }
- if ($value < 0 or $value > 255) {
- throw new \OutOfBoundsException('Value of field \'int3\' is out of range');
- }
- $this->int3 = $value;
- }
- public function getInt4()
- {
- return (int) $this->int4;
- }
- public function setInt4($value)
- {
- if ($value === null) {
- throw new \Exception('Field \'int4\' cannot be null');
- }
- if (is_int($value) !== true) {
- throw new \Exception('Type mismatch in field \'int4\'');
- }
- if ($value < -9223372036854775808 or $value > 9223372036854775807) {
- throw new \OutOfBoundsException('Value of field \'int4\' is out of range');
- }
- $this->int4 = $value;
- }
- public function getDate()
- {
- return $this->date;
- }
- public function setDate($value)
- {
- if ($value === null) {
- $this->date = "2011-04-18";
- return;
- }
- $value = new \DateTime($value); // throws \Exception on type mismatch
- $value = $value->format('Y-m-d');
- $this->date = $value;
- }
- public function getDatetime()
- {
- return $this->datetime;
- }
- public function setDatetime($value)
- {
- if ($value === null) {
- $this->datetime = null;
- return;
- }
- $value = new \DateTime($value); // throws \Exception on type mismatch
- $value = $value->format('Y-m-d h:i:s');
- $this->datetime = $value;
- }
- public function getText()
- {
- return (string) $this->text;
- }
- public function setText($value)
- {
- if ($value === null) {
- throw new \Exception('Field \'text\' cannot be null');
- }
- if (is_string($value) !== true) {
- throw new \Exception('Type mismatch in field \'text\'');
- }
- if (mb_strlen($value) > 65535) {
- throw new \OutOfBoundsException('Value of field \'text\' is out of range');
- }
- $this->text = $value;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment