Пример #1
0
 /**
  * @param string $filePath
  * @return Config
  */
 public static function load($filePath)
 {
     $contents = file_get_contents($filePath);
     $data = Yaml::parse($contents);
     $config = new static();
     $config->fromArray($data);
     return $config;
 }
Пример #2
0
 /**
  * @param $id
  * @return null|static
  */
 public static function getById($id)
 {
     $db = Connection::getConnection();
     $obj = new static();
     $stmt = $db->prepare("SELECT * FROM {$obj->tableName} WHERE {$obj->keyField} = :id");
     $stmt->bindParam('id', $id);
     $stmt->execute();
     $row = $stmt->fetch(PDO::FETCH_ASSOC);
     if (!$row) {
         return null;
     }
     $obj->fromArray($row);
     return $obj;
 }
Пример #3
0
 /**
  * Returns a list of Object from Array or Json String. It is generally used when you json
  * contains an array of this object
  *
  * @param mixed $data Array object or json string representation
  * @return array
  */
 public static function getList($data)
 {
     if (!is_array($data) && JsonValidator::validate($data)) {
         //Convert to Array if Json Data Sent
         $data = json_decode($data, true);
     }
     if (!ArrayUtil::isAssocArray($data)) {
         $list = array();
         //This means, root element is array
         foreach ($data as $k => $v) {
             $obj = new static();
             $obj->fromArray($v);
             $list[] = $obj;
         }
         return $list;
     }
 }
Пример #4
0
 /**
  * 创建一条短信
  *
  * @param array $data
  * @return static
  */
 public static function create($data = [])
 {
     $message = new static();
     $message->fromArray($data);
     return $message;
 }
 /**
  * Creates a new collection objects and reconstitutes the
  * given database record to the new object.
  *
  * @param array $collectionRecord Database record
  * @param boolean $fillItems Populates the entries directly on load, might be bad for memory on large collections
  * @return \TYPO3\CMS\Core\Collection\CollectionInterface
  */
 public static function create(array $collectionRecord, $fillItems = FALSE)
 {
     $collection = new static();
     $collection->fromArray($collectionRecord);
     if ($fillItems) {
         $collection->loadContents();
     }
     return $collection;
 }
Пример #6
0
 /**
  * 获取所有数据
  * @param array $where
  * @return array:static
  */
 public static function all(array $where = [])
 {
     $result = [];
     $db = static::db_connect();
     $ins = new static();
     $dbResults = $db->query($ins->get_tablename(), $where);
     foreach ($dbResults as $dbResult) {
         $dbIns = new static();
         $dbIns->fromArray($dbResult);
         $result[] = $dbIns;
     }
     return $result;
 }
Пример #7
0
 /**
  * Creates new instance of self and with the same values as $this, except
  * the primary key value is cleared
  * @return static
  */
 function copy()
 {
     $new_object = new static();
     $values = $this->toArray();
     foreach (static::$_primaryKeys as $pk) {
         unset($values[$pk]);
     }
     $new_object->fromArray($values);
     return $new_object;
 }
Пример #8
0
 /**
  * @param array $arr
  * @return static
  */
 public static function create_with_array(array $arr)
 {
     $ins = new static();
     $ins->fromArray($arr);
     return $ins;
 }
Пример #9
0
 /**
  * Create an instance of this model class in the persistence provider.
  *
  * @param array      $data The data to create the Persistent object with.
  * @param Repository $repository A specific Repository to persist the object in.
  *
  * @throws Exception\ModelInsertException
  * @throws \Exception
  * @throws Exception\ModelValidationException
  * @return Persistent|null
  */
 public static function create(array $data = array(), Repository $repository = null)
 {
     try {
         /** @var Persistent $instance */
         $instance = new static($repository);
         $instance->fromArray($data, true);
         $result = $instance->save();
     } catch (ModelValidationException $e) {
         throw $e;
     } catch (\Exception $e) {
         throw new ModelInsertException("Error creating item in collection " . static::collectionName(), $e->getCode(), $e);
     }
     if (false === $result) {
         throw new ModelInsertException("Error creating item in collection " . static::collectionName());
     }
     return $instance;
 }
 /**
  * @param string $fileLocation Which file to load from
  * @throws \Exception If the file can not be loaded.
  * @return $this
  *
  * @SuppressWarnings(PHPMD.StaticAccess)
  */
 public static function load($fileLocation = null)
 {
     $register = new static($fileLocation);
     // Check the file exists. If not, create it
     if (!file_exists($register->fileLocation)) {
         touch($register->fileLocation);
     }
     // Import the data to the object
     $data = (array) Yaml::parse(file_get_contents($register->fileLocation));
     $register->fromArray($data);
     return $register;
 }