PageRenderTime 50ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/classes/Base/DB/ORM/Field/Adaptor/Encryption.php

http://github.com/spadefoot/kohana-orm-leap
PHP | 101 lines | 37 code | 7 blank | 57 comment | 5 complexity | bc1fbc045df2aadf0246304d376d6de5 MD5 | raw file
  1. <?php defined('SYSPATH') OR die('No direct script access.');
  2. /**
  3. * Copyright © 2011–2013 Spadefoot Team.
  4. *
  5. * Unless otherwise noted, LEAP is licensed under the Apache License,
  6. * Version 2.0 (the "License"); you may not use this file except in
  7. * compliance with the License. You may obtain a copy of the License
  8. * at:
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. /**
  19. * This class represents a "string" adaptor for handling an encrypted string
  20. * field in a database table.
  21. *
  22. * @package Leap
  23. * @category ORM
  24. * @version 2012-12-05
  25. *
  26. * @see http://www.iitechs.com/kohana/userguide/api/kohana/Encrypt
  27. *
  28. * @abstract
  29. */
  30. abstract class Base_DB_ORM_Field_Adaptor_Encryption extends DB_ORM_Field_Adaptor {
  31. /**
  32. * This constructor initializes the class.
  33. *
  34. * @access public
  35. * @param DB_ORM_Model $model a reference to the implementing model
  36. * @param array $metadata the adaptor's metadata
  37. * @throws Throwable_InvalidArgument_Exception indicates that an invalid field name
  38. * was specified
  39. */
  40. public function __construct(DB_ORM_Model $model, Array $metadata = array()) {
  41. parent::__construct($model, $metadata['field']);
  42. $this->metadata['config'] = (isset($metadata['config']))
  43. ? $metadata['config']
  44. : NULL;
  45. }
  46. /**
  47. * This function returns the value associated with the specified property.
  48. *
  49. * @access public
  50. * @override
  51. * @param string $key the name of the property
  52. * @return mixed the value of the property
  53. * @throws Throwable_InvalidProperty_Exception indicates that the specified property is
  54. * either inaccessible or undefined
  55. */
  56. public function __get($key) {
  57. switch ($key) {
  58. case 'value':
  59. $value = $this->model->{$this->metadata['field']};
  60. if (($value !== NULL) AND ! ($value instanceof DB_SQL_Expression)) {
  61. $value = Encrypt::instance($this->metadata['config'])->decode($value);
  62. }
  63. return $value;
  64. break;
  65. default:
  66. if (array_key_exists($key, $this->metadata)) { return $this->metadata[$key]; }
  67. break;
  68. }
  69. throw new Throwable_InvalidProperty_Exception('Message: Unable to get the specified property. Reason: Property :key is either inaccessible or undefined.', array(':key' => $key));
  70. }
  71. /**
  72. * This function sets the value for the specified key.
  73. *
  74. * @access public
  75. * @override
  76. * @param string $key the name of the property
  77. * @param mixed $value the value of the property
  78. * @throws Throwable_InvalidProperty_Exception indicates that the specified property is
  79. * either inaccessible or undefined
  80. */
  81. public function __set($key, $value) {
  82. switch ($key) {
  83. case 'value':
  84. if ($value !== NULL) {
  85. $value = Encrypt::instance($this->metadata['config'])->encode($value);
  86. }
  87. $this->model->{$this->metadata['field']} = $value;
  88. break;
  89. default:
  90. throw new Throwable_InvalidProperty_Exception('Message: Unable to set the specified property. Reason: Property :key is either inaccessible or undefined.', array(':key' => $key, ':value' => $value));
  91. break;
  92. }
  93. }
  94. }