/classes/Base/DB/ORM/Field/Adaptor/Encryption.php
PHP | 101 lines | 37 code | 7 blank | 57 comment | 5 complexity | bc1fbc045df2aadf0246304d376d6de5 MD5 | raw file
- <?php defined('SYSPATH') OR die('No direct script access.');
- /**
- * Copyright © 2011–2013 Spadefoot Team.
- *
- * Unless otherwise noted, LEAP is licensed under the Apache License,
- * Version 2.0 (the "License"); you may not use this file except in
- * compliance with the License. You may obtain a copy of the License
- * at:
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- /**
- * This class represents a "string" adaptor for handling an encrypted string
- * field in a database table.
- *
- * @package Leap
- * @category ORM
- * @version 2012-12-05
- *
- * @see http://www.iitechs.com/kohana/userguide/api/kohana/Encrypt
- *
- * @abstract
- */
- abstract class Base_DB_ORM_Field_Adaptor_Encryption extends DB_ORM_Field_Adaptor {
- /**
- * This constructor initializes the class.
- *
- * @access public
- * @param DB_ORM_Model $model a reference to the implementing model
- * @param array $metadata the adaptor's metadata
- * @throws Throwable_InvalidArgument_Exception indicates that an invalid field name
- * was specified
- */
- public function __construct(DB_ORM_Model $model, Array $metadata = array()) {
- parent::__construct($model, $metadata['field']);
- $this->metadata['config'] = (isset($metadata['config']))
- ? $metadata['config']
- : NULL;
- }
- /**
- * This function returns the value associated with the specified property.
- *
- * @access public
- * @override
- * @param string $key the name of the property
- * @return mixed the value of the property
- * @throws Throwable_InvalidProperty_Exception indicates that the specified property is
- * either inaccessible or undefined
- */
- public function __get($key) {
- switch ($key) {
- case 'value':
- $value = $this->model->{$this->metadata['field']};
- if (($value !== NULL) AND ! ($value instanceof DB_SQL_Expression)) {
- $value = Encrypt::instance($this->metadata['config'])->decode($value);
- }
- return $value;
- break;
- default:
- if (array_key_exists($key, $this->metadata)) { return $this->metadata[$key]; }
- break;
- }
- throw new Throwable_InvalidProperty_Exception('Message: Unable to get the specified property. Reason: Property :key is either inaccessible or undefined.', array(':key' => $key));
- }
- /**
- * This function sets the value for the specified key.
- *
- * @access public
- * @override
- * @param string $key the name of the property
- * @param mixed $value the value of the property
- * @throws Throwable_InvalidProperty_Exception indicates that the specified property is
- * either inaccessible or undefined
- */
- public function __set($key, $value) {
- switch ($key) {
- case 'value':
- if ($value !== NULL) {
- $value = Encrypt::instance($this->metadata['config'])->encode($value);
- }
- $this->model->{$this->metadata['field']} = $value;
- break;
- default:
- 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));
- break;
- }
- }
- }