abstract class Core_Fluent extends ArrayObject {}
$instance->load($entity_id) ->setName('Foo') ->setDescription('Bar') ->setBasePrice(250) ->save();
... // CamelCase __ // StackOverflow, const PREG_CAMEL_CASE = '/(?<=[AZ])(?=[AZ][az])|(?<=[^AZ])(?=[AZ])|(?<=[A-Za-z])(?=[^A-Za-z])/'; // protected $_data = array(); public function __call($method_name, array $arguments = array()) { // , , // (setBasePrice set BasePrice) if(!preg_match('/^(get|set|isset|unset)([A-Za-z0-9]+)$/', $method_name, $data)) { // , throw new Core_Exception('Method '.get_called_class().'::'.$method_name.' was not found'); } // (BasePrice => base_price) $property $property = strtolower(preg_replace(self::PREG_CAMEL_CASE, '_$0', $data[2])); // , switch($data[1]) { case 'get': { // $object->getBasePrice(): return $this->get($property); } break; case 'set': { // $object->setBasePrice(): return $this->set($property, $arguments[0]); } break; case 'unset': { // $object->getBasePrice(): return $this->_unset($property); } break; case 'isset': { // $object->getBasePrice(): , return $this->_isset($property); } break; default: { } } // , , , " " return $this; } ...
... public function get($code) { if($this->_isset($code)) { return $this->_data[$code]; } // , NULL return NULL; } public function set($code, $value) { $this->_data[$code] = $value; return $this; } public function _unset($code) { unset($this->_data[$code]); return $this; } public function _isset($code) { return isset($this->_data[$code]); } ...
... public function offsetExists($offset) { return $this->_isset($offset); } public function offsetUnset($offset) { return $this->_unset($offset); } public function offsetGet($offset) { return $this->get($offset); } public function offsetSet($offset, $value) { return $this->set($offset, $value); } public function getIterator() { return new Core_Fluent_Iterator($this->_data); } ...
class Core_Fluent_Iterator extends ArrayIterator {}
class Some_Class extends Core_Fluent {} $instance = new Some_Class(); $instance->set('name', 'Foo')->setDescription('Bar')->setBasePrice(32.95); echo $instance->getDescription(), PHP_EOL; // Bar echo $instance['base_price'], PHP_EOL; // 32.95 echo $instance->get('name'), PHP_EOL; // Foo // name => Foo // description => Bar // base_price => 32.95 foreach($instance as $key => $value) { echo $key, ' => ', $value, PHP_EOL; } var_dump($instance->issetBasePrice()); // true var_dump($instance->issetFinalPrice()); // false var_dump($instance->unsetBasePrice()->issetBasePrice()); // false
abstract class Core_Model_Abstract extends Core_Fluent {}
... // , protected $_changed_properties = array(); // . save() // , // ( ) public function create() { return $this; } // public function load($id) { $this->_changed_properties = array(); return $this; } // public function loadFromArray(array $array = array()) { $this->_data = $array; return $this; } // public function save() { $this->_changed_properties = array(); return $this; } // public function remove() { return $this->unload(); } // public function unload() { $this->_changed_properties = array(); $this->_data = array(); return $this; } // public function toArray() { return $this->_data; } ...
... public function set($code, $value) { $this->_changed_properties[] = $code; return parent::set($code, $value); } ...
<?php abstract class Core_Fluent extends ArrayObject { const PREG_CAMEL_CASE = '/(?<=[AZ])(?=[AZ][az])|(?<=[^AZ])(?=[AZ])|(?<=[A-Za-z])(?=[^A-Za-z])/'; protected $_data = array(); public function __call($method_name, array $arguments = array()) { if(!preg_match('/^(get|set|isset|unset)([A-Za-z0-9]+)$/', $method_name, $data)) { throw new Core_Exception('Method '.get_called_class().'::'.$method_name.' was not found'); } $property = strtolower(preg_replace(self::PREG_CAMEL_CASE, '_$0', $data[2])); switch($data[1]) { case 'get': { return $this->get($property); } break; case 'set': { return $this->set($property, $arguments[0]); } break; case 'unset': { return $this->_unset($property); } break; case 'isset': { return $this->_isset($property); } break; default: { } } return $this; } public function get($code) { if($this->_isset($code)) { return $this->_data[$code]; } return NULL; } public function set($code, $value) { $this->_data[$code] = $value; return $this; } public function _unset($code) { unset($this->_data[$code]); return $this; } public function _isset($code) { return isset($this->_data[$code]); } /** * Implementation of ArrayIterator */ public function offsetExists($offset) { return $this->_isset($offset); } public function offsetUnset($offset) { return $this->_unset($offset); } public function offsetGet($offset) { return $this->get($offset); } public function offsetSet($offset, $value) { return $this->set($offset, $value); } public function getIterator() { return new Core_Fluent_Iterator($this->_data); } } ?>
<?php class Core_Fluent_Iterator extends ArrayIterator {} ?>
<?php abstract class Core_Model_Abstract extends Core_Fluent { protected $_changed_properties = array(); public function set($code, $value) { $this->_changed_properties[] = $code; return parent::set($code, $value); } public function create() { return $this; } public function load($id) { $this->_changed_properties = array(); return $this; } public function loadFromArray(array $array = array()) { $this->_data = $array; return $this; } public function save() { $this->_changed_properties = array(); return $this; } public function remove() { return $this->unload(); } public function unload() { $this->_changed_properties = array(); $this->_data = array(); return $this; } public function toArray() { return $this->_data; } } ?>
Source: https://habr.com/ru/post/185896/
All Articles