missing() static public method

This is very handy to check for missing user values in a request for example.
static public missing ( array $array, array $required = [] ) : array
$array array The source array
$required array An array of required keys
return array An array of missing fields. If this is empty, nothing is missing.
Ejemplo n.º 1
0
 public function create($uid, $template, $content = array())
 {
     if (empty($template)) {
         throw new Exception(l('pages.add.error.template'));
     }
     $uid = empty($uid) ? str::random(32) : $uid;
     $blueprint = new Blueprint($template);
     $data = array();
     foreach ($blueprint->fields(null) as $key => $field) {
         $data[$key] = $field->default();
     }
     $data = array_merge($data, $content);
     // create the new page and convert it to a page model
     $page = new Page($this->page, parent::create($uid, $template, $data)->dirname());
     if (!$page) {
         throw new Exception(l('pages.add.error.create'));
     }
     kirby()->trigger('panel.page.create', $page);
     // subpage builder
     foreach ((array) $page->blueprint()->pages()->build() as $build) {
         $missing = a::missing($build, array('title', 'template', 'uid'));
         if (!empty($missing)) {
             continue;
         }
         $subpage = $page->children()->create($build['uid'], $build['template'], array('title' => $build['title']));
         if (isset($build['num'])) {
             $subpage->sort($build['num']);
         }
     }
     return $page;
 }
Ejemplo n.º 2
0
 public function testMissing()
 {
     $user = $this->user;
     $required = array('username', 'password', 'website');
     $missing = a::missing($user, $required);
     $this->assertEquals(array('website'), $missing);
 }
Ejemplo n.º 3
0
 public static function createPage($uri, $title, $template, $uid)
 {
     $parent = empty($uri) ? site() : page($uri);
     $uid = empty($uid) ? str::slug($title) : str::slug($uid);
     if (empty($title)) {
         throw new Exception(l('pages.add.error.title'));
     }
     if (empty($template)) {
         throw new Exception(l('pages.add.error.template'));
     }
     $data = pagedata::createByBlueprint($template, array('title' => $title));
     $page = $parent->children()->create($uid, $template, $data);
     $blueprint = blueprint::find($page);
     if (is_array($blueprint->pages()->build())) {
         foreach ($blueprint->pages()->build() as $build) {
             $missing = a::missing($build, array('title', 'template', 'uid'));
             if (!empty($missing)) {
                 continue;
             }
             static::createPage($page->uri(), $build['title'], $build['template'], $build['uid']);
         }
     }
     return $page;
 }
Ejemplo n.º 4
0
 /**
  * Checks if all required data is present to send the form.
  */
 private function dataValid($requiredFields, $validateFields)
 {
     // check if all required fields are there
     $this->erroneousFields = a::missing($this->data, array_keys($requiredFields));
     if (!empty($this->erroneousFields)) {
         $this->message = l::get('sendform-fields-required');
         return false;
     }
     // perform validation for all fields with a given validation method
     foreach ($validateFields as $field => $method) {
         $value = a::get($this->data, $field);
         // validate only if a method is given and the field contains data
         if (!empty($method) && !empty($value) && !call('v::' . $method, $value)) {
             array_push($this->erroneousFields, $field);
         }
     }
     if (!empty($this->erroneousFields)) {
         $this->message = l::get('sendform-fields-not-valid');
         return false;
     }
     return true;
 }
Ejemplo n.º 5
0
 static function load($username)
 {
     // check for an existing user account file
     $file = c::get('root.site') . '/' . c::get('panel.folder') . '/accounts/' . $username . '.php';
     if (!file_exists($file)) {
         return false;
     }
     // load the account credentials
     content::start();
     require $file;
     $account = content::end(true);
     $account = spyc_load($account);
     if (!is_array($account)) {
         return false;
     }
     // check for required fields
     $missing = a::missing($account, array('username', 'password'));
     if (!empty($missing)) {
         return false;
     }
     return $account;
 }
Ejemplo n.º 6
0
 /**
  * Checks if all required keys are in the 'raw' event array. Throws an
  * exception if one is missing.
  *
  * @param array $event a 'raw' event array containing all fields
  */
 private static function validate($event)
 {
     $missingKeys = a::missing($event, self::$requiredKeys);
     if ($missingKeys) {
         $message = "Event creation failed because of the following missing " . "required fields:\n" . a::show($missingKeys, false);
         throw new Exception($message, 1);
     }
 }